利用 dropbox 备份网站

这篇文章主要讲解如何使用 Dropbox 在 Linux 上进行文件备份。需要声明一点的是该方案只适合 vps 在国外的用户使用,因为 Dropbox 在国内被墙该方案无法使用。

配置应用密钥

登录Dropbox后,打开Apps页面,点击 Create App 按钮,创建一个App。安装下图操作:

点击 create app 按钮 创建一个 app

选择 Dropbox api 然后其他的根据如图所示配置,最后输入 app 名称

点击创建后复制 App secret的值,需要点击Generated access token的按钮生成 token,然后复制该值

下载脚本

1.下载脚本

wget https://raw.github.com/andreafabrizi/Dropbox-Uploader/master/dropbox_uploader.sh

2.然后我们把脚本放到/usr/local/bin 目录去

mv dropbox_uploader.sh /usr/local/bin/dropbox_uploader
chmod +x /usr/local/bin/dropbox_uploader

3.配置 drpobox,执行如下命令然后输入密钥值就可以绑定到 app

dropbox_uploader info   

[root@aliyun ~]# dropbox_uploader info

 This is the first time you run this script, please follow the instructions:

 1) Open the following URL in your Browser, and log in using your account: https://www.dropbox.com/developers/apps
 2) Click on "Create App", then select "Dropbox API app"
 3) Now go on with the configuration, choosing the app permissions and access restrictions to your DropBox folder
 4) Enter the "App Name" that you prefer (e.g. MyUploader120623263730051)

 Now, click on the "Create App" button.

 When your new App is successfully created, please click on the Generate button
 under the 'Generated access token' section, then copy and paste the new access token here:

 # Access token: Y5s0FzVaOFAAAAAAAAAG5J-2Wwsbn74fPd4Q_OUygtWXs2wtTW40iZgL75ddO9oU

 > The access token is Y5s0FzVaOFAAAAAAAAAG5J-2Wwsbn74fPd4Q_OUygtWXs2wtTW40iZgL75ddO9oU. Looks ok? [y/N]: y
 The configuration has been saved.

5.然后执行下面的命令上传一个文件 如果 ok 说明没问题

dropbox_uploader upload frp_0.13.0_linux_amd64.tar.gz frp_0.13.0_linux_amd64.tar.gz

备份脚本

#!/bin/bash
SCRIPT_DIR="/usr/local/bin" #这个改成你存放刚刚下载下来的dropbox_uploader.sh的文件夹位置
DROPBOX_DIR="/backup" #这个改成你的备份文件想要放在Dropbox下面的文件夹名称,如果不存在,脚本会自动创建
BACKUP_SRC="/www /usr/local/openresty/ /root/.acme.sh/" #这个是你想要备份的本地VPS上的文件,不同的目录用空格分开
BACKUP_DST="/tmp" #这个是你暂时存放备份压缩文件的地方,一般用/tmp即可

# 下面的一般不用改了
NOW=$(date +"%Y.%m.%d")
DESTFILE="$BACKUP_DST/$NOW.tar.gz"
echo "正在打包文件"
tar cfzP "$DESTFILE" $BACKUP_SRC
echo "所有数据打包完成,准备上传..."
# 用脚本上传到dropbox
$SCRIPT_DIR/dropbox_uploader upload "$DESTFILE" "$DROPBOX_DIR/$NOW.tar.gz"
if [ $? -eq 0 ];then
     echo "上传完成"
else
     echo "上传失败,重新尝试"
fi
# 删除本地的临时文件
rm -f "$NOW-Databases.sql" "$DESTFILE"

配置计划任务

crontab -e

加一条

0 3 * * * /bin/bash /usr/local/bin/backup

ok,到此搞定了备份!