将VPS数据备份到Dropbox

对于多数便宜的VPS,有可能会出现数据丢失而又没有备份的情况,所以就需要将重要的数据定期备份到其他地方。网上能提供这种服务的不是很多,除了GoDaddy为每一个注册的域名提供的备份空间,我们还可以利用Dropbox这一免费网络空间进行数据备份。注意,Dropbox在国内无法访问,所以国内VPS是不用想备份到上面了,除非手动设置代理。

具体备份流程也很简单,将下面的代码存为脚本,比如DropboxBackup.sh,然后加上可执行权限。

#!/bin/bash
DROPBOX_USER="Your Dropbox username"
DROPBOX_PASS="Your Dropbox password"
DROPBOX_DIR="Directory in your dropbox account to store the backups, e.g. /backups"
BACKUP_SRC="/home /var/www /var/git /etc /root"
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/.*
]*>\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 } # 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"

注意:代码中你需要修改的部分,就是“# Stop editing here.”这行之前。

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数据库地址
MYSQL_USER="root"  #mysql数据库用户名
MYSQL_PASS="Your MySQL password" #mysql数据库密码

指定DROPBOX_DIR之前需要在你的Dropbox空间里建立相应目录。

加可执行权限:

chmod +x DropboxBackup.sh

然后可以设为每天凌晨1:00自动进行备份:

$ crontab -e
0 1 * * *       /bin/bash /root/DropboxBackup.sh

本文为悠然居(https://wordpress.youran.me/)的原创文章,转载请注明出处!

Leave a Reply

Your email address will not be published. Required fields are marked *