Recently, I have been working a lot on Raspberry Pi server and as it had more functionality and useful data, problem of automatic backup became quite critical. As I am an active user of SkyDrive (OneDrive) I wanted to do a backup there. But after quick search it appears quite hard to do (if not impossible), so I decided to use Dropbox instead.
The first tutorial I found was this one from Dave Hope. Quite a nice script, doesn’t use any dependencies, but unfortunately I haven’t been able to run it (probably because of my lack of experience with Linux in general). So I decided to use Andrew Fabrizi’s Dropbox Uploader and write my own script.
So there are three steps we have to do:
1. Ok, so first ssh to your Raspberry Pi and download Dropbox uploader script to your home directory.
sudo wget https://github.com/andreafabrizi/Dropbox-Uploader/blob/master/dropbox_uploader.sh -P ~
The only dependency this script is using is Curl which is installed on Raspbian by default, but just to be 100% sure run this
sudo apt-get update sudo apt-get install curl
Then make it executable and run the script
sudo chmod +x ~/dropbox_uploader.sh bash ~/dropbox_uploader.sh
Follow on screen tutorial to get the App key, App Secret and Permission type from your Dropbox account and type them into script. Then finally copy link the script gives you into browser, click Allow button and your Dropbox uploader script should work fine ! You can check it by trying to lauch command like this
~/dropbox_uploader.sh upload ~/dropbox_uploader.sh /
Script should successfully upload itself into Dropbox.
2. Next step is to write script that would archive the files/folders you want to backup into a single file and than upload it to Dropbox. My script currently looks like this:
#!/usr/bin/env bash BACKUP_SRC="/home /var/www /etc /root /tmp/databases.sql" BACKUP_EXCLUDE="~/Downloads" BACKUP_DST="/var/backups/$(date +"%Y")" DROPBOX_DST="/Backup/$(date +"%Y")" MYSQL_SERVER="127.0.0.1" MYSQL_USER="root" MYSQL_PASS="your MySQL pass" NOW=$(date +"%Y.%m.%d") DESTFILE="$BACKUP_DST/$NOW.tgz" echo -e "Backing up MySQL databases" mysqldump -u $MYSQL_USER -h $MYSQL_SERVER -p$MYSQL_PASS --all-databases > "/tmp/databases.sql" echo -e "Archiving folders" sudo mkdir -p $BACKUP_DST sudo tar -zcf "$DESTFILE" --exclude="$BACKUP_EXСLUDE" $BACKUP_SRC echo -e "Uploading archive to DropBox" ~/dropbox_uploader.sh upload "$DESTFILE" "$DROPBOX_DST/$NOW.tgz" echo -e "Finished"
Create text file and copy/paste this script
Now it is important to change some variables so this script better suits you.
First of all, if you don’t have MySQL database or don’t want to backup it, you have to remove /tmp/databases.sql from BACKUP_SRC and comment out this line by putting # in front of it.
mysqldump -u $MYSQL_USER -h $MYSQL_SERVER -p$MYSQL_PASS --all-databases > "/tmp/databases.sql"
If you have WordPress website on your Pi, than all posts/comments/etc are stored in MySQL databases, so you better to backup it.
BACKUP_SRC contain all folders you want to backup. I have put folders where by default apache websites and most OS settings are stored.
BACKUP_EXCLUDE contains folders which are inside BACKUP_SRC folders but you wont to back it up. For example I want to backup my home folder /home/pi but don’t want to backup /home/pi/Downloads folder which could be quite big.
BACKUP_DST contains folder where backup would be stored locally. If you don’t want store all backups locally, than add this line into the end of the script.
Note that I am creating folder with the number of the year. This is to help me manage all backups in the future. If you want you can also add month folder like this.
BACKUP_DST="/var/backups/$(date +"%Y")/$(date +"%m")"
DROPBOX_DST is similar to BACKUP_DST and tells where backup would be stored in DropBox.
MYSQL_SERVER, MYSQL_USER and MYSQL_PASS are needed for MySQL database backup.
NOW is the file name for backup archive we want to create. It is good idea to use current year, month and date for it.
Finally rest of the script is straightforward. It would create sql script with all databases, archive all folders and files and finally send it to dropbox.
After you edit variables, make script executable and try to run it.
sudo chmod +x ~/backupme.sh bash ~/backupme.sh
3. If everything works fine the last thing we have to do is to setup scheduled task to run the script. We would do so using crontab. Open crontab editor…
…and add this line into it.
0 5 * * 1 ~/backupme.sh
This would run our script every Monday 5am. I don’t feel it is necessary for me to explain crontab format, as there is already a lot of good tutorials for this, for example this one from The Geek Stuff.
Well, I hope it works and you have your scheduled script running regularly to backup your precious data.