论坛有实时备份的介绍,不过一般用不到。定时备份足够了,也节约内存,详细步骤(超简单):
至少你要有个dropbox帐号吧,dropbox国内是被封的,这个你懂的,不过hostloc的用户用下面的地址注册保证直连,不需要挂 代 理:
http://db.tt/DpMXz2jt(手动复制吧,发链接不太好)(没有功劳,也有苦劳吧,不喜勿喷)(记得注册后下载安装dropbox客户端到电脑,后面用的到)
版本一:
新建一个脚本:
vi dropbox.sh
复制下面的内容,中文注释的地方注意自定义:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
#!/bin/bash DROPBOX_USER="dropbox邮箱帐号" DROPBOX_PASS="dropbox密码" #dropbox上传目录 默认是dropbox文件夹 DROPBOX_DIR="/dropbox" #要备份的文件路径 BACKUP_SRC="/home /var/www /var/git /etc /root" #临时目录 BACKUP_DST="/tmp" MYSQL_SERVER="127.0.0.1" #mysql用户名 MYSQL_USER="root" #mysql用户密码 MYSQL_PASS="Your MySQL password" # based on http://davehope.co.uk/Blog/backup-your-linux-vps-to-dropbox/ # based on https://github.com/andreafabrizi/Dropbox-Uploader 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 } #mysql数据库备份 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" |
然后修改下脚本权限:
chmod 755 dropbox.sh
然后./dropbox.sh 运行测试,如果有下面提示表示成功:
> Logging in… OK
> Loading Home… OK
> Uploading ‘/tmp/2012.03.25.tgz’ to ‘DROPBOX/dropbox/’… OK
这时登录你的dropbox帐号,发现dropbox文件夹内已经有了2012.03.25.tgz文件。
然后再来个crontab定时执行脚本。
版本二(一般版本一够用了,下面的可以忽略)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#!/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_USER="Your MySQL username" MYSQL_PASS="Your MySQL password" # Stop editing here. NOW=$(date +"%Y.%m.%d") DESTFILE="$BACKUP_DST/$NOW.tgz" # Backup files. mysqldump -u $MYSQL_USER -p$MYSQL_PASS --all-databases > "$NOW-Databases.sql" tar cfz "$DESTFILE" $BACKUP_SRC dropbox_uploader.sh -u "$DROPBOX_USER" -p "$DROPBOX_PASS" -f "$DESTFILE" -d "$DROPBOX_DIR" rm -f "$NOW-Databases.sql" "$DESTFILE" |
dropbox_uploader.sh下载链接见github
https://github.com/andreafabrizi/Dropbox-Uploader
备份到自己的电脑上只需要安装dropbox客户端,然后可以把dropbox的同步文件夹放到金山网盘同步文件夹内,这样也能备份到金山网盘上。这样就是3次备份了,这样数据安全性提升300%