利用Shell脚本实现自动备份VPS数据到Dropbox

VPS的数据备份是必须的,谁也不能保证哪天VPS主机商跑路,或者宿主机硬盘出问题,导致数据丢失!尤其是海外VPS,有时候忘掉备份,为了以防万一我今天又想去找个脚本让他自动备份到网盘里面!

http://davehope.co.uk/Blog/backup-your-linux-vps-to-dropbox/

dropbox_uploader.sh (上面脚本在此dropbox官方脚本做了优化)

很快就被我找到了参考了这篇blog,经过在本地测试环境测试失败之后~毅然放到了VPS上在测一次,结果成功了!

本地测试报错信息:

[root@localhost home]# ./DropboxBackup.sh

tar: 从成员名中删除开头的“/”

> Logging in...grep: /tmp/du_resp_29003: 没有那个文件或目录

Failed!

VPS测试成功之后,我大概清楚为什么在本地会失败!因为本地网络环境是大陆,而我的VPS是在美国洛杉矶的!应该是本地的网络环境注定了无法使用脚本远程登陆到dropbox账户吧!

此脚本的思路是把要备份的文件夹和mysqldump下来的sql一起打包上传至Dropbox!

首先必须注册一个dropbox账户 https://www.dropbox.com/register ,注册之后进入dropbox网盘创建一个叫做backups文件夹!为什么要注册一个国外网盘呢?那速度快啊~~你试试看把脚本改成115网盘试试看~20KB都是快的了~~另外上传到网盘的速度快了也就大大减少了VPS的负载!

脚本内容如下:

#!/bin/bash
DROPBOX_USER="xxx@xxx.com" //Dropbox的账号
DROPBOX_PASS="12345678" //Dropbox的密码
DROPBOX_DIR="/backups" //Dropbox中的文件夹名称
BACKUP_SRC="/home/wwwroot /data" //需要备份的文件夹路径,可以同时指定多个中间用空格隔开
BACKUP_DST="/home/backup" //用来存放备份的文件夹路径
MYSQL_SERVER="127.0.0.1" //连接本地MySQL
MYSQL_USER="root" //本地MySQL的用户
MYSQL_PASS="12345678" //本地MySQL的密码

#
# 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.
echo "Dumping databases..."
mysqldump -u $MYSQL_USER -h $MYSQL_SERVER -p$MYSQL_PASS --all-databases > "$NOW-Databases.sql"
echo "Packing files..."
tar czf "$DESTFILE" $BACKUP_SRC "$NOW-Databases.sql"

dropboxUpload "$DESTFILE" "$DROPBOX_DIR"

echo "Cleaning the backups..."
rm -f "$NOW-Databases.sql"
find $BACKUP_DST -mtime +31 -delete

注意改完信息之后去掉中文说明!最后给上执行权限并加入计划任务

# chmod 777 DropboxBackup.sh

# vi /etc/crontab 把 0 23 * * 6 root sh /home/DropboxBackup.sh 添加进去,即每週六23:00执行此脚本!也可根据更新习惯自定义时间!

 

转自:http://maxiaolei.com/shell-backup-data-to-dropbox.html

posted @ 2013-01-24 14:51  Monn  阅读(931)  评论(0编辑  收藏  举报