Backing up photos with the Raspberry Pi (Part 2)

Lead Image © Valery Kachaev, 123RF.com

Safe and Sound

Whenever you're traveling, there's always a chance you could lose your camera, so you should save your pictures as you go. The Pi image bank can save images and even display the pictures via a web server.

This project is the second in our series and follows on from the idea of setting up a data bank of your images to keep them safe. Simply connect your digital camera's memory card to the Pi using a USB adapter and it will automatically back up your photos (Figure 1). The status of the backup is signaled using the three LEDs. This sequel to our previous article still allows you to back up images easily while providing you with some extra options.

Figure 1: An image bank based on the Raspberry Pi 3 in the process of backing up.

As promised in Part 1, there's now an updated version of the backup script. In addition, the backup should be accessible from a Windows machine. However, as the data is stored on a partition formatted for a Linux filesystem, attaching a drive to a Windows PC won't work. You can, however, find a workaround for this below. Finally, we set up a small web server so you can view your pictures whenever you want.

Better Backups

The backup script from Part 1 does the job but has a few flaws. For instance, when copying data from a memory card there's a chance it could accidentally overwrite files with identical names in the backup directory. The developer's GitHub page [1] now has an updated version of the copy_img script, imaginatively named copy_img2, which you'll find in the folder /files/usr/local/sbin.

The new script gives each file its own name in the format pimg_1234xxx, assigning it a unique number. It also stores timestamps on each SD card to ensure it only backs up each file once. This means the script now automatically supports multiple cameras. It also sorts files into their subfolders by recording date in the format YYYY-MM-DD. Pictures taken on May 20th 2017, for instance, would be placed in the folder /data/images/20170520.

The revised script also contains an update to the udev rules, located in /etc/udev/rules.d/99-usbcopy.rules. This fixes an error in the previous script whereby the backup script was started away, which caused the program to crash during longer backups as udev only runs for a few seconds. The updated script starts a systemd process, which in turn executes the actual backup script as necessary.

Your new script will require access to the GraphicsMagick program, which you can install using the Raspbian Package Manager or from the terminal. You will also need to install additional packages to get the access point working (Listing 1). The backup script works with GraphicsMagick to generate thumbnails of your images which your system can use to build a photo gallery.

Listing 1

Install packages for thumbnails and AP

$ sudo apt-get install GraphicsMagick
$ sudo apt-get install dnsmasq hostapd

Windows Access

As Linux directly supports the file system used by the image bank, Linux users can simply detach their chosen storage medium from the Raspberry Pi and connect it to their PC or laptop. Windows users can't do this as the OS won't recognize the drive. The solution to this is to set up a small server for network shares using Samba, so you can access your pictures over your local network.

To get started, install the necessary packages (Listing 2, Line 1) on the Raspberry Pi, then replace the text in the configuration file /etc/samba.smb.conf with the text in Listing 3. Open the configuration file using sudo nano /etc/samba.smb.conf, replace the text, then save with [Ctrl]+[O],[Input] then [Ctrl]+[X]. Finally, restart the Samba service (Listing 2, Line 3).

Listing 2

Install and start samba

01 $ sudo apt-get install samba
02 $ sudo nano /etc/samba.smb.conf
03 $ sudo systemctl restart smbd.service

Listing 3

samba.smb.conf

[global]
workgroup = IMAGETANK
security = user
map to guest = Bad User
guest account = pi
[images]
comment = Image backup
path = /data/images
browseable = yes
writable = yes
guest ok = yes
guest only = yes

To access the images from your Windows machine, open Explorer and use the address <ipaddressofyourpi>\images. This may also work on Linux depending on your desktop environment; simply go to smb://<ipaddressofyourpi>/images. By default, the server can be accessed by any device on your local network and no password is required.

If you don't want to install and configure the Samba server yourself, there's a small program named install-samba that automates the process for you in the 'tools' directory of the GitHub page. In order for it to work, you must run it with root privileges.

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content