Here, we’re assuming that you’re using the default user pi.
If you’re not, you’ll need to adjust the script.
First, we need to create a mount point
for the drive. This is just a folder, and can
be put anywhere – we’re going to spurn
convention and put it in our home folder.
So before running the script, run:
mkdir /home/pi/pic_mount
With this done, we’re ready to go. The script
to mount the drive and get the photos is:
#!/bin/bash
if mount /dev/sda1
/home/pi/pic_mount ; then
echo "Partition mounted"
cd /home/pi/pic_mount
yes 'n' | gphoto2 --
auto-detect --get-all-files
umount /dev/sda1
else
echo "/dev/sda1 could not
be mounted"
fi
yes 'n'
is a command that simply
emits a stream of n characters. This
means that when
Gphoto2
prompts to
overwrite any previously downloaded files,
it will decline. The
umount
is essential,
because it ensures that the drive is
properly synced and can be removed.
We’ve called the script
get-pics.sh
,
and saved it in Pi’s home directory.
To make it executable, run:
chmod +x /home/pi/get-pics.sh
You should now be able to run it
manually. You’ll need to use
sudo
because it needs to mount the drive.
The final piece of the puzzle is to get the
script to run automatically. To do this,
we add it to the file
/etc/rc.local
.
This script runs when you boot up, and
it executes as root, so there’s no need to
worry about permissions. Just open the
file with a text editor as root. For example,
with
sudo nano /etc/rc.local
,
and add the line:
/home/pi/get-pics.sh
just before the line exit 0.
Now all you have to do is plug in your
camera (making sure it’s turned on,
of course) and your USB stick, and
it’ll back up your photographs when
you boot up your Raspberry Pi.
■
In this form, it’s not
very portable, but with
a little bit of judicious
DIY, you should be
able to package it
more conveniently.
24
Raspberry Pi User Guide.indd 24
08/07/2014 14:44