VPS数据备份到Dropbox

Dropbox注册链接:点击 (凭此链接注册可获得额外空间)

对于VPS用户来说,什么都要自己动手,也不能指望主机供应商来个免费的每日备份,本来我对备份这个事情也不是很上心,但是上次手贱chmod -R 777 /etc导致ssh不能连接之后,废了好大劲才恢复,因此对这个事情也重视起来。

很久之前就看到说有批处理备份的方法,今天上网搜了一下,发现很多都是错的,我甚至怀疑那些博主是不是单纯的复制粘贴,也没验证过,终于找到了一个,转发过来。

方法如下:

1.# vi dropboxbackup.sh  //这个名字比较长,你也可以叫做a.sh

输入如下代码:

#!/bin/bash
DROPBOX_USER="guosuiyu@gmail.com" //Dropbox的账号
DROPBOX_PASS="12345678" //Dropbox的密码
DROPBOX_DIR="/backups" //Dropbox中的文件夹名称
BACKUP_SRC="/blog/rainbow /home /data" //需要备份的文件夹路径,可以同时指定多个
BACKUP_DST="/home/rainbow/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 //这里的时间可以根据需要进行修改

2、# chmod +x dropboxbackup.sh  //权限设置,这个名字的确挺长的

3、# crontab -e //设置为每个月执行一次备份,具体时间可以根据需要进行修改

00 00 * * * /bin/bash /home/ec2-user/dropboxbackup.sh

4、然后我执行了一次 sudo sh /home/ec2-user/dropboxbackup.sh,结果如下:

本文章技术部分内容转载自:

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

Dropbox注册链接:点击(凭此链接注册可获得额外空间)

发表评论

电子邮件地址不会被公开。 必填项已用*标注

This site uses Akismet to reduce spam. Learn how your comment data is processed.