标签 dropbox 下的文章

今年4月份,Google drive的接口更新了,导致grive不可用。由于grive的作者已经N久没更新了,于是VPS的日常备份要做修改了。

一开始的方案是换个客户端。找来找去,就只有这个drive项目了:
https://github.com/odeke-em/drive
由于是用go写的,需要装go相关包。感觉很麻烦,于是放弃安装。

最后改为用Dropbox来做同步。优点是可选客户端比Google drive多,且安装方便。缺点就是免费空间小,默认只有2GB,目前还算够用。

Dropbox的Shell脚本客户端,只需要dropbox_uploader.sh文件就够了:
https://github.com/andreafabrizi/Dropbox-Uploader

参考了以下两个教程:
1)自动备份网站并同步到 Dropbox
http://www.lovelucy.info/backup-website-and-sync-to-dropbox.html
2)Linux VPS备份网站数据到Dropbox
https://maoxian.de/2013/01/linux-vps%E5%A4%87%E4%BB%BD%E7%BD%91%E7%AB%99%E6%95%B0%E6%8D%AE%E5%88%B0dropbox/755.html

简单总结一下步骤:
1)下载 dropbox_uploader.sh
2)登录 dropbox.com 并创建一个应用
3)初始化客户端,运行以下命令,并按提示输入App key和App secret,以及相关设置:

./dropbox_uploader.sh info

4)编写备份脚本。其实跟备份到Google drive的脚本差不多。
5)设置自动运行备份脚本,命令是:

crontab -e

备份脚本参考:

#!/bin/bash

# begin settings
# MySQL settings
MYSQL_USER=user #MySQL用于备份数据的用户
MYSQL_PASS=password #用户密码
MYSQL_BIN=/usr/bin
# backup directory and files
CUR_DIR=/opt/backup
TMP_DIR=$CUR_DIR/tmp
BAK_DIR=/opt/dropbox_bak/$(date +%Y%m%d)
BAK_DB_NAME=bak_$(date +"%Y%m%d")_mysql.tar.gz
BAK_FILE_NAME=bak_$(date +%Y%m%d)_file.tar.gz
# delete local backup files 20 days ago
BAK_EXPIRED=/opt/dropbox_bak/$(date -d -20day +"%Y%m%d")
# dropbox client
DROPBOX=$CUR_DIR/dropbox_uploader.sh
DROPBOX_DEL=/$(date -d -30day +%Y%m%d)
# website directory
WEB_DATA="/opt/www/"
# end settings

# Create the directory when not existed
if [ ! -d $BAK_DIR ]; then
    mkdir -p $BAK_DIR
fi
if [ ! -d $TMP_DIR ]; then
    mkdir -p $TMP_DIR
else
    rm -Rf $TMP_DIR/*
fi
cd $TMP_DIR

# begin backup database
DATABASES=`$MYSQL_BIN/mysql -h "127.0.0.1" -u$MYSQL_USER -p$MYSQL_PASS -Bse 'show databases'`

for DB in $DATABASES; do
    # ignore system tables
    if [ "$DB" == "information_schema" -o "$DB" == "performance_schema" -o "$DB" == "mysql" ]; then
        continue
    fi

    # export data to backup file
    $MYSQL_BIN/mysqldump -h "127.0.0.1" -u$MYSQL_USER -p$MYSQL_PASS $DB > $DB.sql
done

# compress and merge all the database backup files
tar zcf $BAK_DB_NAME *.sql
mv -f $BAK_DB_NAME $BAK_DIR/$BAK_DB_NAME
# end backup database

# backup website files
tar zcpf $BAK_FILE_NAME $WEB_DATA
mv -f $BAK_FILE_NAME $BAK_DIR/$BAK_FILE_NAME

# sync files
$DROPBOX upload -s $BAK_DIR /
$DROPBOX delete $DROPBOX_DEL

# delete local old files
rm -Rf $TMP_DIR/*
if [ -d $BAK_EXPIRED ]; then
    rm -Rf $BAK_EXPIRED
fi

dropbox_uploader.sh的操作跟一般的文件操作类似,可以上传指定的文件或文件夹。

好了,剩下的工作就是想办法免费扩容。