Auto Update Backup

From SingletonMillerWiki
Revision as of 18:07, 1 June 2013 by Martin (Talk | contribs)

Jump to: navigation, search


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]

The step to getting this work are.

  1. download the script
  2. create a backup location
  3. setup the script for your backup location
  4. enable the script in cron

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.

CIFS share

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} ://192.168.0.8/&

$ 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

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.

References

  1. The Raspberry Pi Backup Thread
  2. how-do-i-add-jobs-to-cron-under-linux
  3. can-a-raspberry-pi-be-used-to-create-a-backup-of-itself