自动备份 VPS 网站数据到 Dropbox 折腾记录

in cn •  2 years ago  (edited)

网上教程一堆,看着简单,不过自己技术太菜,折腾到符合自己需求要费不少功夫,简单记录一下。

Dropbox 准备


登录 Dropbox,打开 Apps 页面,点击 Create App 按钮,创建一个 App

 549-dropbox-app

获取 Access token,点击 Generate 获取备用。

 549-dropbox-app-token

接着 SSH 登录 VPS 获取最新的 Dropbox Uploader 脚本

git clone https://github.com/andreafabrizi/Dropbox-Uploader.git
chmod +x dropbox_uploader.sh
./dropbox_uploader.sh info


根据提示输入 Access token,Dropbox 绑定成功。再次运行 ./dropbox_uploader.sh info 可以看到绑定的 Dropbox 账户信息。

 549-dropbox-app-info

创建备份脚本


参考脚本,需要根据实际环境修改,如文件路径、备份目录等。

#!/bin/bash
script_dir="/root" #这是 dropbox_uploader.sh 所在目录
dropbox_dir="/" #Dropbox 存放备份文件目录
mysql_server="localhost" #数据库服务器地址,一般填 localhost 即可
mysql_user="数据库帐号" #数据库帐号
mysql_pass="数据库密码" #数据库密码
backup_dst="/home/backup" #本地备份文件存放目录,手动创建

if [ $(date +%w) -eq 0 ];then #周日备份整站数据
    backup_src="/data/www/cyhour.com /usr/local/apache/conf /usr/local/php/etc" #VPS上要备份的文件/目录,不同目录用空格分开
else                         #每天备份重要文件、数据
    find /data/www/cyhour.com -maxdepth 1 -type f -exec tar cvf $backup_dst/important_files.tar.gz {} + #网站根目录文件
    backup_src="/data/www/cyhour.com/wp-content/themes /usr/local/apache/conf /usr/local/php/etc" #VPS上要备份的文件/目录,不同目录用空格分开
fi

#压缩好的备份文件
dstfile="$backup_dst/$(date +"%Y-%m-%d").tar.xz" 

#定义 Dropbox 旧数据的名字(30天前)
dropbox_old_file=$(date -d -30day +%Y-%m-%d).tar.xz

cd $backup_dst

#导出SQL数据库,并且按数据库分个压缩
for db in `mysql -u $mysql_user -h $mysql_server -p$mysql_pass -B -N -e 'SHOW DATABASES' | xargs`; do
    (mysqldump -u $mysql_user -h $mysql_server -p$mysql_pass --skip-lock-tables ${db} | gzip -9 - > ${db}.sql.gz)
done

#压缩数据库文件合并为一个压缩文件,并删除单独数据库文件
tar zcf sql_all_$(date +%Y-%m-%d).tar.gz *.sql.gz
rm -rf *.sql.gz

#数据库、文件压缩到一起
tar cfzP "$dstfile" $backup_src $backup_dst/*.*.gz

# 将文件上传到 Dropbox
$script_dir/dropbox_uploader.sh upload "$dstfile" "$dropbox_dir/$(date +"%Y-%m-%d").tar.xz"

# 删除 VPS 本地临时文件
rm -rf $backup_dst/*.*

#开始删除远程30天前的文件
$script_dir/dropbox_uploader.sh delete $dropbox_old_file


脚本修改完,上传至 /root/ 目录下,并赋予执行权限。

chmod +x /root/backup.sh

crontab 定时执行脚本

44 4 * * * /root/backup.sh

每天 4:44 自动执行脚本

参考文章


KeenWon - 自动定时备份 VPS

TENNFY WU - 定时备份 VPS 数据至 Dropbox 教程

Unix & Linux Stack Exchange- tar files only, no directories


Centos 7 x86_64 bbr + LAMP 一键安装包环境下实测通过。


Originally Posted on Steemit & Blog by @zhijun.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

good info

Thanks.

Thanks for sharing ,

Upvoted !

By the way ,I started following you and I would appreciate your follow back too .
please visit my blog and vote me too

Thanks.

Congratulations @zhijun! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes received

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!