Blog·Tanky WooABOUTRSS

自动备份VPS数据到Dropbox

21 Jan 2012
这篇博客是从旧博客 WordPress 迁移过来,内容可能存在转换异常。

最近一直在考虑备份的问题,因为随着站越来越大,付出了两年的心血,如果真因为没有备份而导致数据丢失,那就真是欲哭无泪了,而对于那么多的站点及数据,如果要时常备份,肯定会烦躁的,在网上找到了一个脚本,专门可以把VPS上的数据和数据库中的数据自动备份到dropbox中。

dropbox是什么?不解释,美丽的度娘,万能的谷哥,等着你去找他们。

脚本来至这个网站:http://davehope.co.uk/Blog/backup-your-linux-vps-to-dropbox/

#!/bin/bash
DROPBOX_USER="Your Dropbox username"   #dropbox账号
DROPBOX_PASS="Your Dropbox password"   #dropbox密码
DROPBOX_DIR="Directory in your dropbox account to store the backups, e.g. /backups" #dropbox中存放备份的文件夹
BACKUP_SRC="/home /var/www /var/git /etc /root" #VPS中需要备份的目录
BACKUP_DST="/tmp" #备份存放的地方
MYSQL_SERVER="127.0.0.1"  #本地数据库服务器
MYSQL_USER="root"  #数据库用户名
MYSQL_PASS="Your MySQL password"  #数据库密码
#下面的就不需要修改了

#
# Stop editing here.
NOW=$(date +"%Y.%m.%d")
DESTFILE="$BACKUP_DST/$NOW.tgz"

#
# Upload a file to Dropbox.
# $1 = Source file
# $2 = Destination file.
function dropboxUpload
{
    #
    # Code based on DropBox Uploader 0.6 from http://www.andreafabrizi.it/?dropbox_uploader
    LOGIN_URL="https://www.dropbox.com/login"
    HOME_URL="https://www.dropbox.com/home"
    UPLOAD_URL="https://dl-web.dropbox.com/upload"
    COOKIE_FILE="/tmp/du_cookie_$RANDOM"
    RESPONSE_FILE="/tmp/du_resp_$RANDOM"

    UPLOAD_FILE=$1
    DEST_FOLDER=$2

    # Login
    echo -ne " > Logging in..."
    curl -s -i -c $COOKIE_FILE -o $RESPONSE_FILE --data "login_email=$DROPBOX_USER&login_password=$DROPBOX_PASS&t=$TOKEN" "$LOGIN_URL"
    grep "location: /home" $RESPONSE_FILE > /dev/null

    if [ $? -ne 0 ]; then
        echo -e " Failed!"
        rm -f "$COOKIE_FILE" "$RESPONSE_FILE"
        exit 1
    else
        echo -e " OK"
    fi

    # Load home page
    echo -ne " > Loading Home..."
    curl -s -i -b "$COOKIE_FILE" -o "$RESPONSE_FILE" "$HOME_URL"

    if [ $? -ne 0 ]; then
        echo -e " Failed!"
        rm -f "$COOKIE_FILE" "$RESPONSE_FILE"
        exit 1
    else
        echo -e " OK"
    fi

    # Get token
    TOKEN=$(cat "$RESPONSE_FILE" | tr -d '\n' | sed 's/.*<form action="https:\/\/dl-web.dropbox.com\/upload"[^>]*>\s*<input type="hidden" name="t" value="\([a-z 0-9]*\)".*/\1/')

    # Upload file
    echo -ne " > Uploading '$UPLOAD_FILE' to 'DROPBOX$DEST_FOLDER/'..."
    curl -s -i -b $COOKIE_FILE -o $RESPONSE_FILE -F "plain=yes" -F "dest=$DEST_FOLDER" -F "t=$TOKEN" -F "file=@$UPLOAD_FILE"  "$UPLOAD_URL"
    grep "HTTP/1.1 302 FOUND" "$RESPONSE_FILE" > /dev/null

    if [ $? -ne 0 ]; then
        echo -e " Failed!"
        rm -f "$COOKIE_FILE" "$RESPONSE_FILE"
        exit 1
    else
        echo -e " OK"
        rm -f "$COOKIE_FILE" "$RESPONSE_FILE"
    fi
}

# Backup files.
mysqldump -u $MYSQL_USER -h $MYSQL_SERVER -p$MYSQL_PASS --all-databases > "$NOW-Databases.sql"
tar cfz "$DESTFILE" $BACKUP_SRC "$NOW-Databases.sql"

dropboxUpload "$DESTFILE" "$DROPBOX_DIR"

rm -f "$NOW-Databases.sql" "$DESTFILE"

代码只需要在几个地方修改,已在上面的代码中注释出来。

直接:

#随便找个地方,存放backup.sh,比如我:
#cd /home/wwwroot/到这里来放置这个shell script
vi backup.sh
#粘贴上代码后
chmod +x backup.sh
#因为授予了权限,所以直接运行即可:
./backup.sh
#接着设置定时任务,可以man crontab自己看看具体用法
$ crontab -e
# m h  dom mon dow   command
#这里分别对应minute hour 'day of month' month 'day of week'
#下面这行,意思就是每月第一天备份
0 0 1 * *       /bin/bash /home/wwwroot/backup.sh

OK,玩网站,就一句话,勤备份,靠别人不如靠自己,要想玩得好,首先要勤劳!