Auto Update Backup
The goal here is to get a network connected Raspberry Pi to periodically update and backup itself up to an external device or network share.
This is heavily based on the developments presented here [1]
Contents
Installation
Copy the script to the home folder on your Raspberry Pi using wget.
cd ~ wget https://raw.github.com/martinrgmiller/RPiAutoBackup/master/AutoUpdateBackup.sh
Create a Backup Location
This location must not be physically on the Pi's SD card, it should be an external USB drive or other share on your network.
My prefered method is to use Autofs to mount a network share.
This can be done automatically Accessing Network Shares or manually Backup SD Image
You must make sure that the location for the backup file is mounted and writeable and NOT physically on the SD card.
My Autofs setup
$tail /etc/auto.master # Include central master map if it can be found using # nsswitch sources. # # Note that if there are entries for /net or /misc (as # above) in the included master map any keys that are the # same will not be seen as the first read key seen takes # precedence. # #+auto.master /media/cifsshares /etc/auto.cifs --ghost $cat /etc/auto.cifs * -fstype=cifs,credentials=${HOME}/cifs.cred,uid=${UID},gid=${UID} ://<cifs server>/& $ cat ~/cifs.cred username=jack password=<jacks cifs password>
to hide the credentials file use (for security).
sudo chmod 600 /home/${UID}/cifs-cred
Alternatively its possible to manually mount the share, add this to the script to run it automatically.
sudo mount -t cifs //hostname/sharename -o username=xxxx,password=yyyy /media/cifshares
Note that the Script will run as root, the set up above covers more than mounting the backup location.
Setup the script
The header of the script contains the user defined parameters
SUBDIR=/media/cifsshares/Public DIR=$SUBDIR/RPI_backups
Modify these to match the location for the backup location.
Install pv dependency
$sudo apt-get install pv
Test the Script
$sudo sh AutoUpdateBackup.sh
Example Output
Updating System Hit http://mirrordirector.raspbian.org wheezy Release.gpg Hit http://archive.raspberrypi.org wheezy Release.gpg Hit http://mirrordirector.raspbian.org wheezy Release Hit http://archive.raspberrypi.org wheezy Release Hit http://mirrordirector.raspbian.org wheezy/main armhf Packages Hit http://archive.raspberrypi.org wheezy/main armhf Packages Hit http://mirrordirector.raspbian.org wheezy/contrib armhf Packages Hit http://mirrordirector.raspbian.org wheezy/non-free armhf Packages Hit http://mirrordirector.raspbian.org wheezy/rpi armhf Packages Ign http://archive.raspberrypi.org wheezy/main Translation-en_GB Ign http://archive.raspberrypi.org wheezy/main Translation-en Ign http://mirrordirector.raspbian.org wheezy/contrib Translation-en_GB Ign http://mirrordirector.raspbian.org wheezy/contrib Translation-en Ign http://mirrordirector.raspbian.org wheezy/main Translation-en_GB Ign http://mirrordirector.raspbian.org wheezy/main Translation-en Ign http://mirrordirector.raspbian.org wheezy/non-free Translation-en_GB Ign http://mirrordirector.raspbian.org wheezy/non-free Translation-en Ign http://mirrordirector.raspbian.org wheezy/rpi Translation-en_GB Ign http://mirrordirector.raspbian.org wheezy/rpi Translation-en Reading package lists... Done Reading package lists... Done Building dependency tree Reading state information... Done The following packages have been kept back: omxplayer 0 upgraded, 0 newly installed, 0 to remove and 1 not upgraded. Starting RaspberryPI backup process! Package 'pv' is installed. Stopping some services before backup. apache2: unrecognized service mysql: unrecognized service [ ok ] Stopping periodic command scheduler: cron. Backing up SD card to USB HDD. This will take some time depending on your SD card size and read performance. Please wait... 1.84GB 0:05:16 [5.97MB/s] [=====================================================================>] 100% 1886+0 records in 1886+0 records out 1977614336 bytes (2.0 GB) copied, 319.996 s, 6.2 MB/s Start the stopped services again. apache2: unrecognized service mysql: unrecognized service [ ok ] Starting periodic command scheduler: cron. Successful backup, previous backup files will be deleted. Backup is being tarred. Please wait... tar: Removing leading `/' from member names RaspberryPI backup process completed! FILE: /media/cifsshares/Public/backup_20130602_005554.img.tar.gz
Move the script to a better location
Assuming you've resolved all the permissions and the scripts runs, copy the script for local admin use only. This isn't strictly necessary as cron can run a script from anywhere, but its 'neater'.
sudo cp /home/jack/AutoUpdateBackup.sh /usr/local/bin/
Modify the permissions of the script to make it executable
sudo chmod +x /usr/local/bin/AutoUpdateBackup.sh
Setup Cron
Add the following line to /etc/crontab [2]
crontab -e
add the following to the end
01 4 * * * root /usr/local/bin/AutoUpdateBackup.sh
Your RPi will now auto update and backup at 04:00 every day.