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.
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:
#!/bin/bash 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 get-all-files.sh
name. Now you can use the code in Listing 2 to create a Python script that does several things.
Listing 2
fetch.py 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('./get-all-files.sh') 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('./get-all-files.sh')
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 fetch.py
name and make it executable using
chmod +x fetch.py
Then, run the nano .bash_profile
command and replace the sudo ./get-all-files.sh
entry added previously with sudo ./fetch.py
. 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.
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].
Infos
- gPhoto2: http://gphoto.sourceforge.net
- gphoto2-updater Bash shell script: http://github.com/dmpop/gphoto2-updater
- Code and files from the article: http://github.com/dmpop/rpi-photo
« Previous 1 2 Next »
Buy this article as PDF
Pages: 4
(incl. VAT)