Build a Raspberry Pi-based backup device

Adding Some Hardware

The backup solution described above does the trick, but it can be improved even further. For example, you can add buttons to trigger the backup script and shut down Raspberry Pi, and you can install an LED for visual feedback.

All this is possible thanks to Raspberry Pi's GPIO pins, which can control a variety of inputs and outputs. For this project, you'll need a few additional components, including a breadboard, a handful of jumper wires (both male-male and female-male), two 10K resistors and a single 10-ohm resistor, two push buttons, and an LED.

Start by wiring the components as shown in Figure 1. Two separate circuits contain a pull-up 10K resistor and a push button connected to pins 17 and 23 and a circuit with a 68-ohm resistor and an LED connected to pin 25. Next, you can install the RPi.GPIO Python module using the sudo apt-get install python-dev python-rpi.gpio command.

Figure 1: Wiring diagram.

Before you start working on a Python script that reads GPIO inputs and performs the required actions, you should write a simple Bash shell script that does the actual backup:

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

Save the script under the name. Now you can use the code in Listing 2 to create a Python script that does several things.

Listing 2 Python Script

01 #!/usr/bin/env python
02 from time import sleep
03 import os
04 import RPi.GPIO as GPIO
05 GPIO.setmode(GPIO.BCM)
06 GPIO.setwarnings(False)
07 GPIO.setup(17, GPIO.IN)
08 GPIO.setup(23, GPIO.IN)
09 GPIO.setup(25, GPIO.OUT)
10 GPIO.output(25, True)
11 while True:
12         if (GPIO.input(17) == False ):
13                 os.system('./')
14                 GPIO.output(25, False)
15         if (GPIO.input(23) == False):
16                 os.system('sudo halt')
17         sleep(0.1);

First, it initializes pins 17 and 23 for input and pin 25 for output; then, it enables pin 25, thus turning on the LED (this indicates that the script is running and ready to accept user input). The script then waits for input on pins 17 and 23. When the user presses the push button wired to pin 17, this triggers the os.system('./') call, which executes the Bash shell backup script and turns off the LED. When the script detects a signal on pin 25, it shuts down the system. Save the script under the name and make it executable using

chmod +x

Then, run the nano .bash_profile command and replace the sudo ./ entry added previously with sudo ./ Save the changes, reboot Raspberry Pi, and wait until the LED turns on. Then, plug in and turn on the camera, and press the first push button to perform backup (Figure 2). When the LED turns off, press the second push button to shut down the system.

Figure 2: Raspberry Pi photo backup prototype in action.

Final Word

I've described two ways to transform a Raspberry Pi into a backup device. With some creative thinking and hacking, you can easily tweak and improve the described recipes, for example, by writing a script that processes the transferred photos and publishes them on the web. Also, you can configure Raspberry Pi to send email notification when the backup operation has completed.

All code and files mentioned in this article ares available on GitHub [3].


  1. gPhoto2:
  2. gphoto2-updater Bash shell script:
  3. Code and files from the article:

The Author

Dmitri Popov holds a degree in Russian language and computer linguistics. He has been writing exclusively about Linux and open source software for several years, and his articles have appeared in Danish, British, North American, German, and Russian magazines and websites.

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