VPS备份至DropBox脚本

Posted by Harid2012 - Apr - 17 留个言

使用VPS,网站的备份就显得非常重要了。由于VPS是在国外,所以不方便将所有东西都往国内备份,速度太慢!我目前使用的备份策略是几个网站的数据库每天备份,以邮件的形式备份至各自的邮箱,网站文件则备份至国外著名网盘DropBox,网站文件每周备份一次。

DropBox客户端的运行需要100MB左右的内存,我的VPS上也就没有安装DropBox客户端了,选择以脚本形式利用网上(传送门》》)放出的API函数进行定时备份。

这个关键函数是:

function dropboxUpload
{
        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/.*
]*>\s* 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 }

欲利用这个函数,仅需要在调用函数的时候提供两个参数给它,第一个参数为将备份的文件的本地绝对路径,第二个参数为DropBox网盘的备份目录。另外,需要在函数前面定义好DropBox的用户名、密码,其变量名称分别为:DROPBOX_USER、DROPBOX_PASS。我的调用示例为:

dropboxUpload "$BackupDir/$Website/Files/$BackupName.tar.gz" "$DROPBOX_DIR"

根据我的VPS的具体情况写的备份脚本如下,它在备份好后发送通知邮件,不过没有做备份成功与否的判断了,因为这是从国外备份到国外,成功率是非常高的。

#!/bin/bash
##
################## Introduction ###################
##                                               ##
## This script will be executed at a known time  ##
## to backup your website's files and upload it  ##
## to dropbox .                                  ##
## Contact: http://www.timepoet.com             ##
###################################################

## Backup Directory && DropBox Info ##
BackupDir="/home/backups"
SourceDir="/home/wwwroot"
DROPBOX_USER="your dropbox account"
DROPBOX_PASS="your dropbox password"
DROPBOX_DIR="/backups"
##################################

## Website && Backup Name ##
Website="timepoet.com"
BackupName="Web-ninthday-$(date +%Y%m%d)"
BackupOld="Web-ninthday-$(date -d -14day +%Y%m%d)"
##################################

## Email Notification ##
ToEmail="*****@163.com"
#################################

## Do not Modify the Following Code ##
function dropboxUpload
{
	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/.*]*>\s* 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
}

echo "[+] Packaging..."
tar -zPcf $BackupDir/$Website/Files/$BackupName.tar.gz $SourceDir/$Website/*
echo "[+] Login DropBox && Sending file @$(date +%F\ %H\:%k)..."
dropboxUpload "$BackupDir/$Website/Files/$BackupName.tar.gz" "$DROPBOX_DIR"

echo "[+] After sending @$(date +%F\ %H\:%k)"
echo "[+] Delete local old files..."
if [ -e $BackupDir/$Website/Files/$BackupOld.tar.gz ] ; then
    rm -f $BackupDir/$Website/Files/$BackupOld.tar.gz
fi

echo "[+] Sending notificationn email...@$(date +%F\ %H\:%k)"
echo "***A website duplicate has been created and uploaded to the host in $(date +%F)." >> $BackupDir/$Website/Logs/Web-success.log
echo "***A website duplicate has been created and uploaded to the host in $(date +%F\ %H\:%k)." > $BackupDir/$Website/Logs/tempf
mutt -s "$Website. Files duplicate.($(date +%Y-%m-%d))" $ToEmail < $BackupDir/$Website/Logs/tempf
rm -f $BackupDir/$Website/Logs/tempf

exit 0

   声明:本文采用 BY-NC-SA 协议进行授权 | 星期九
   原创文章转载请注明:转自《VPS备份至DropBox脚本

Comments(11) Leave comments
  1. Gravatar
    win8迷 Google Chrome Google Chrome 18.0.1025.142 Windows Windows 8
    四月 18th, 2012 at 22:09  | #1

    现在还没用上vps 苦命

  2. Gravatar
    小呆 Google Chrome Google Chrome 16.0.912.63 Windows Windows 7
    四月 18th, 2012 at 20:50  | #2

    表示不会写shell脚本。。。

    • Gravatar Harid  @  四月 24th, 2012 at 10:37 replied.

      @小呆, 我也只是写一些非常非常easy的脚本,能力仅及于此啊,汗颜,惭愧!

  3. Gravatar
    索阳网 Google Chrome Google Chrome 18.0.1025.162 Windows Windows XP
    四月 18th, 2012 at 20:13  | #3

    纯技术文章啊

  4. Gravatar
    beyondme37 Google Chrome Google Chrome 17.0.963.56 Windows Windows 7
    四月 18th, 2012 at 14:16  | #4

    专业啊,我也在用vps啊 😛

    • Gravatar Harid  @  四月 18th, 2012 at 20:11 replied.

      @beyondme37, 你的VPS是在哪一家的?配置怎么样?嘿嘿,分享分享管理经验啊!

      • Gravatar beyondme37  @  四月 19th, 2012 at 16:14 replied.  | #5

        @Harid, burstvps的,性价比很不错!速度也快。

        • Gravatar Harid  @  四月 24th, 2012 at 10:36 replied.

          @beyondme37, 嗯,速度确实不赖。是OpenVZ还是Xen?探针? 😀

  5. Gravatar
    jiechic Google Chrome Google Chrome 18.0.1025.162 Windows Windows 7
    四月 17th, 2012 at 15:42  | #6

    这个是好东西哦。 👿

    • Gravatar Harid  @  四月 18th, 2012 at 20:26 replied.

      @jiechic, 如果用VPS,这个还是有点用的。

51 + 69 =  (required)
comment_ad

 NOTICE1: You should type some Chinese word (like “你好”) in your comment to pass the spam-check, thanks for your patience!

 NOTICE2: 请申请gravatar头像(http://en.gravatar.com),木有头像的会显示为“小怪物”头像,将难以通过审核!

分享按钮