Build a Raspberry Pi-based backup device

Lead Image © Daniel Villeneuve,

Tiny Backup Box

With some creativity and a little scripting, you can easily turn your Raspberry Pi into an effective backup device.

Backup is crucial, especially when you are traveling. This is particularly true for photos: To keep your snaps safe, you ought to have at least one backup set of your precious photos. That's why a light, flexible, and inexpensive backup tool can be an indispensable tool in your travel bag. Several backup solutions are available on the market, but with a Raspberry Pi, you can build your own backup device and learn a few useful tricks and skills in the process.

Required Components

To begin, you will need a Raspberry Pi along with the Raspbian Linux distro installed on an SD card. Although you can use a standard micro-USB charger to power Raspberry Pi, you might want to invest in an external battery pack to make the solution more portable. Finally, you need a high-capacity USB key for storing backups. In theory, you could use a USB hard disk, but, because it must be connected to Raspberry Pi through a powered USB hub, this approach would make the setup unwieldy.

Going the Software Route

The easiest way to transform Raspberry Pi into a backup device is to use the excellent gPhoto2 [1] software available in the Raspbian software repository. To install it, use the

sudo apt-get install gphoto2

command. Although the older version from the repository should do the trick, you might want to compile the latest release of gPhoto2 from the source code because it provides support for new camera models along with a handful of fixes and improvements.

Compiling from the source code can be a daunting task, but the gphoto2-updater Bash shell script automates the entire process [2]. Grab the script from the project's GitHub repository by clicking on the ZIP button, unpack the downloaded archive, and then make the script executable with the

chmod +x

command. Next, issue the


command to run the script. You also need to install two additional packages on Raspberry Pi: usbmount and ExifTool. The former allows Raspberry Pi to mount and unmount USB devices automatically, whereas the latter is necessary for processing downloaded files. To install both packages, enter:

sudo apt-get install usbmount libimage-exiftool-perl

With all the pieces in place, you can proceed. The first step is to write a Bash shell script that transfers photos from the camera connected to the Raspberry Pi. Run the nano command to create an empty file, open it in the nano text editor, then enter the following:

mkdir "`date --iso-8601`" && cd $_
gphoto2 --get-all-files
exiftool -r -d %Y%m%d-%H%M%S.%%e "-FileName<DateTimeOriginal" .

The script is not particularly complicated. First it creates a directory using the current date in the ISO format as its name and then switches to the created folder. The script then pulls photos from the camera with the gphoto2 --get-all-files command and uses ExifTool to rename them on the basis of date and time info from Exif metadata.

Save the script and make it executable with chmod +x Next, connect the camera to Raspberry Pi, turn the camera on, and run the script with the command ./ to transfer photos.

Although this simple script does the job, it's far from ready. To run the script, you must be able to control Raspberry Pi either directly or via an SSH connection. Ideally, it should kick in automatically as soon as it detects the connected camera. To make this happen, you need to add code that constantly checks for the camera and transfers photos as soon as it detects it:

DEVICE=$(gphoto2 --auto-detect | grep usb | cut -b 36-42 | sed 's/,/\//')while [ -z ${DEVICE} ]
        sleep 1
        DEVICE=$(gphoto2 --auto-detect | grep usb | cut -b 36-42 | sed 's/,/\//')

By default, the script performs backup in the current working path (i.e., on the SD card). If you prefer to use a USB key as a backup destination, you need to tweak the script so it switches to the appropriate mountpoint. This is where the usbmount tool comes into the picture: It automatically mounts a connected USB device (in this case, the USB key) at the first available /media/usb mountpoint.

If the USB key is the only storage device connected to Raspberry Pi, usbmount mounts it at the /media/usb0 point, so you need to add the cd /media/usb0/ command to the script. Finally, you might want to add the halt command, so the script automatically shuts down Raspberry Pi after the photos have been transferred and processed. You can see the script in its entirety in Listing 1.

Listing 1 Shell Script

01 #!/bin/bash
02 DEVICE=$(gphoto2 --auto-detect | grep usb | cut -b 36-42 | sed 's/,/\//')
03 while [ -z ${DEVICE} ]
04         do
05         sleep 1
06         DEVICE=$(gphoto2 --auto-detect | grep usb | cut -b 36-42 | sed 's/,/\//')
07 done
08 cd /media/usb0/
09 mkdir "`date --iso-8601`" && cd $_
10 gphoto2 --get-all-files
11 exiftool -r -d %Y%m%d-%H%M%S.%%e "-FileName<DateTimeOriginal" .
12 halt

One more step is needed, which is to configure Raspberry Pi to run the script automatically. To do this, open the inittab file in nano using the sudo nano /etc/inittab command and locate the following:

1:2345:respawn:/sbin/getty  --noclear 38400 tty1

Add the --autologin pi option to it as follows and save the file:

1:2345:respawn:/sbin/getty --autologin pi --noclear 38400 tty1

Next, run the nano .bash_profile command and add the sudo ./ line to the opened .bash_profile file. Save the changes, and you are done. Reboot Raspberry Pi, plug in, and turn on the camera, and the script should do the rest.

If your Raspberry Pi is connected to the Internet, you can modify the script to perform off-site backup. To do this, install the Rsync tool on Raspberry Pi and the remote backup server. Next, you need to configure a passwordless SSH login for the remote server.

On Raspberry Pi, run the following command to generate a key pair:

ssh-keygen -t dsa

Don't enter any password when prompted, and confirm the default choices by pressing the Enter key. Then, copy the generated public key to the remote server with the following command (replace <user> with the actual user name and <remotehost> with the IP address or domain name of the remote server):

ssh-copy-id -i .ssh/  <user>@<remotehost>

Finally, add the following command to the script:

rsync -avhe ssh --delete /<path/to/source/dir> \

Remember to replace the placeholders with actual paths.

When running on its own, Raspberry Pi provides practically no feedback, so you might have trouble telling when the script is done. Of course, Raspberry Pi shuts down automatically after the backup has been performed, but figuring out whether the device is still running can be tricky. One way to solve this problem is to tweak the script, so it plays an MP3 file before issuing the halt command.

To add this functionality, you can install the mpg123 player with the sudo apt-get install mpg123 command. Next, insert the mpg123 sound.mp3 line before the halt command in the script (replace sound.mp3 with the actual name of the sound file). Plug earphones or a speaker into Raspberry Pi's audio jack, and you should hear the sound before the system shuts down.

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content