Backup your Linux system to Dropbox
As an adovate of Dropbox I’ve been keen to find a simple way to backup by webserver/VPS to Dropbox. With up to 10gB of storage space for free there’s plenty of capacity for me to backup some MySQL databases and webroot. Unfortunately the Linux command line dropbox client doesn’t provide any way to synchronise a specific directory.
Using Andrew Fabrizi’s Dropbox Uploader script I cobbled together a quick BASH script to backup key directories to my Dropbox account.
#!/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/.*<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. 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"
Save the script as “DropboxBackup.sh”, chmod +x it and run. Maybe now’s a good time to update your crontab so that the backup runs once a month without intervention:
$ crontab -e # m h dom mon dow command 0 0 1 * * /bin/bash /root/DropboxBackup.sh
If you don’t have SSH access to your system, you can probably use your web control panel to execute a shell script. Sit back and smile whenever you get a notification of the backup.
[…] This post was mentioned on Twitter by Neil Robbins, davehope. davehope said: Backup you Linux system to #dropbox http://davehope.co.uk/Blog/backup-your-linux-vps-to-dropbox/ […]
I assume you used an older version of the dropbox script, since your copy doesn’t handle cleanups?
Rather, yours does do cleanups, just not as well.
Sorry to spam, but you don’t have an edit button. Your comments indicate you used 0.6? Why would you remove the cleanup function in such a moronic way? Clearly you realize that if an error occurs (in your version), that no cleanup takes place, right?
For anyone having trouble, I found my problem (despite the rather terrible hacking performed on this script)… Max file size: 300MB.
Hi Michael,
Sorry to hear you had some problems. You can of course, contribute any cleanup improvements you made so that it helps out others. Glad you identified your problem in the end.
Any idea on how to keep only the xx latest backup files on the Dropbox account? This would be a great addon to this script.
Hi Guillaume,
You could add a fine command at the end.
Something like:
find ./ -name “*.tgz” -type f -mtime +1 -exec rm {} \;
Would remove files of type “tgz” that are older than 48hrs.
[…] 原文连接:http://davehope.co.uk/Blog/backup-your-linux-vps-to-dropbox/ 原创文章如转载,请注明:转载自深度VPS [ http://www.deepvps.com ] [复制本文链接发送给您的好友] Tags: dropbox 一键脚本 备份数据 […]
[…] 原文连接:http://davehope.co.uk/Blog/backup-your-linux-vps-to-dropbox […]
Error:
/root/DropboxBackup.sh: line 10:
: command not found
/root/DropboxBackup.sh: line 15:
: command not found
/root/DropboxBackup.sh: line 21: syntax error near unexpected token `{
‘
/root/DropboxBackup.sh: line 21: `{
How to fix it?
How did you execute the file? chmod +x it and then do ./DropboxBackup.sh ?
[…] 我找到了1个根据上面脚本改写的备份脚本,转载自http://davehope.co.uk/Blog/backup-your-linux-vps-to-dropbox/ #!/bin/bash […]
Find the reasons
I copy the code in window, but put it linux,
More ^M per line
Hi,
That sounds like some kind of locale issue? Try removing the ^M’s from your document.
Hi,
Great script by the way.
It would be awesome if this script was updated so it removes backups older than 5 days or something.
I know your talk about it, but the purposed doesnt seem to work.
find ./ -name “*.tgz” -type f -mtime +1 -exec rm {} \;
Does anyone have a working method to remove old backups?
Hi Adam,
The find command would only work if you installed the full Dropbox linux client, and set it up as a cron job to remove the files from the directory. It might be possible to write a script to remove old files using the dropbox web interface, but I don’t have the time to do that – sorry.
Thanks
Dave
Hi, here is my way to keep only the 7 latest backup files on the Dropbox account:
I use the “day of week” format
#NOW=$(date +”%Y.%m.%d”)
NOW=$(date +”%u”)
and files older than 1 week are then overwritten on dropbox.
Very very good!
It works!
Thank you very much Dave!
Could we have the blogroll for each other?
My blog website is: heylinux.com