July 8, 2013 · backup bash debian hacks linux newbie raspberrypi raspbian rsync ssh

The Importance of Backups

Few years ago, blogging service JournalSpace has been completely wiped out after the drives that housed their entire database were overwritten. The problem was that their backups weren't actually backups at all. The servers were set up with a mirrored RAID system so that if the primary drive should fail the secondary drive would be used to recover the primary.

This is an example of a big fish in trouble, but this could as well happen to you. Especially if you rely on your Rapsberry Pi's SD card alone. The SD cards are known to have a very limited lifespan (write/read cycles). If you use your Pi extensively (as a webserver, media centre), i.e., switching your machine on/off constantly, not stopping it properly, messing with config files, using it as just another PC in your home, you do risk losing your precious data.

In fact, I myself was in the middle of writing this post, when something went wrong. I prepared my SD card data partition to serve as testing backup device (I've moved my system onto more reliable USB drive), when my system halted. Even unplugging and plugging the power again did not work. Fortunately, it was just a minor error in fstab file, but i was calm, as I have my data backed up. So, what can be done? There are several solutions, all of them incorporate the idea of making a backup of your data. Here are some of my ideas.


Personally, I use rsync tool for my backups (if you don't have it installed, try sudo apt-get install rsync in your terminal). Whether they're local or remote backups, this tool lets me synchronize my data. As I'm using my Pi as a headless web server, I also need to have openssh configured on my machine, to enable remote login to start the process from the host I want to copy things to. This means, I will be backing up files from /pi/dir/ on pi_host, as pi_user, to /destination/dir/ on destinationhost, as destinationuser. There are various ways of configuring your rsync. Some involve using private keys for ssh connection. Others to input passowrd each time you rsync your files. Finally, some allow incoming rsync connection only from a given host. I personally prefer the last one, as it allows me to quickly configure backup service and, as both my Pi and my destination host are in the same network, there is less probability I might be hacked. This also ensures the destination host accepts only the pi_host rsync without the need of having to open ssh port.

Here's how it works:

On the destination host we have to enable rsync deamon:

$ sudo nano /etc/default/rsync

On line 8 change:


Next, we create the rsyncd.conf file:

$ sudo nano /etc/rsyncd.conf
# define any name you like 
[site] # Destination Directory 
path = /home/backup # Hosts you allow to copy (specify source Host) 
hosts allow = #put your pi_host IP here 
hosts deny = * 
list = true 
uid = root 
gid = root 
read only = false

Next we want to create the backup directory:

$ mkdir /home/backup

and start the rsync daemon:

$ /etc/init.d/rsync start

Starting rsync daemon: rsync.

After that, lets move to our pi_host. The only thing we have to do here is to install the rhost and create a file with directories we want to exclude from our backup (this is highly recommended if we want to backup the whole system):

$ sudo nano /etc/rsync_exclude.lst

Here we put any files or directories we want to be omitted. If you ever want to make a full backup, I recommend to put there the following:


Now let's try if it works, by performing a test sync. This all is done from our pi_host:

$ rsync -avz -e --exclude-from=/etc/rsync_exclude.lst /dir/to/sync/

The is the IP address of our destination host and 'site' is the thing we put into rsyncd.conf file.

If everything is fine (you might have to add sudo command before rsync code), we can perform the real backup by removing the -e function from our line:

$ rsync -avz --delete --exclude-from=/etc/rsync_exclude.lst /dir/to/sync/

You can also automate the process, providing your destination host machine is always switched on by adding a cron task:

$ sudo crontab -e

At the end of the file add:

00 02 * * * sudo rsync -avz --delete --exclude-from=/etc/rsync_exclude.lst /dir/to/sync/

This tells your Pi to perform the backup every day at 2 AM. I won't write much about cron here, but if you want to play with cron tasks, visit this place.

Local backup

Using rsync you may also perform a local backup, e.g. to a USB drive. You most definitely have a spare USB drive (or, in fact, any drive) lying somewhere in your house, so why not use it as a backup device for your Pi? As long as it's enough space for your data, it should be ok. To do this, you have to mount the drive. The best practice is to install **usbmount **package which will automate the process:

$ sudo apt-get install usbmount

Now any time you plug a drive into USB port, it will automatically mount itself. To perform local rsync write in terminal:

$ rsync -avz --delete --exclude-from=/etc/rsync_exclude.lst /dir/to/sync/ /media/usb/

where /media/usb is where the drive was mounted. And we're done!


I have also come across a shell script created by bill2w to clone a running Raspberry Pi SD card to a USB mounted SD card. I haven't tried it yet, but it might be worth looking at: rpi-clone.

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket