Difference between revisions of "Auto Update Backup"

From SingletonMillerWiki
Jump to: navigation, search
m (CIFS share)
m (Move the script to a better location)
 
(3 intermediate revisions by the same user not shown)
Line 10: Line 10:
 
<pre>
 
<pre>
 
cd ~
 
cd ~
wget https://raw.github.com/martinrgmiller/RPiAutoBackup/master/AutoUpdateBackup.sh
+
wget https://raw.github.com/martinrgmiller/RPiAutoBackup/master/AutoUpdateBackup
 
</pre>
 
</pre>
  
Line 71: Line 71:
  
 
<pre>
 
<pre>
$sudo sh AutoUpdateBackup.sh
+
$sudo sh AutoUpdateBackup
 
</pre>
 
</pre>
  
== Move the script to a better location ==
+
== Move the script to cron ==
 
Assuming you've resolved all the permissions and the scripts runs, copy the script to cron.daily.
 
Assuming you've resolved all the permissions and the scripts runs, copy the script to cron.daily.
  
 
<pre>
 
<pre>
sudo cp /home/jack/AutoUpdateBackup.sh /etc/cron.daily
+
sudo cp /home/jack/AutoUpdateBackup.sh /etc/cron.daily/AutoUpdateBackup
 
</pre>
 
</pre>
  
 
Modify the permissions of the script to make it executable
 
Modify the permissions of the script to make it executable
 
<pre>
 
<pre>
sudo chmod +x /etc/cron.daily/AutoUpdateBackup.sh
+
sudo chmod +x /etc/cron.daily/AutoUpdateBackup
 
</pre>
 
</pre>
  
The script will now run once per day.
+
The script will now run once per day. Note if you change the file name don't add an extension because  script filenames in cron.daily/, cron.hourly/, etc., should not contain dot(.), otherwise <code>run-parts</code> will skip them.
 +
'''Note. as of 2013.06.09 for some reason the script doesn't work in cron'''
 +
 
 +
=== debugging cron ===
 +
By default debian on which Raspian is based doesn't have the log enabled by default [http://askubuntu.com/questions/36971/at-what-time-does-cron-execute-daily-scripts].
 +
To enable logs edit <code>/etc/rsyslog.conf</code>
 +
<pre>
 +
sudo nano /etc/rsyslog.conf
 +
</pre>
 +
uncomment the the line
 +
<pre>
 +
# cron.*                          /var/log/cron.log
 +
</pre>
 +
The logs for cron can be found <code>/var/log/cron.log</code>
 +
 
 +
To determine when on your system cron.daily will run read <code>/etc/crontab</code>
 +
<pre>
 +
17 *    * * *  root    cd / && run-parts --report /etc/cron.hourly
 +
25 6    * * *  root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
 +
47 6    * * 7  root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
 +
52 6    1 * *  root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
 +
</pre>
 +
This shows cron.daily running at 06:25 each day. If you're having problems with cron.daily its possible to modify the time to aid with debugging.
  
 
== Logfile ==
 
== Logfile ==

Latest revision as of 08:52, 9 June 2013


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]

Installation

Copy the script to the home folder on your Raspberry Pi using wget.

cd ~
wget https://raw.github.com/martinrgmiller/RPiAutoBackup/master/AutoUpdateBackup

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

The script supports temporary and direct mounting of a network share. Alternatively you can auto mount the shares. Accessing Network Shares.

The method fo authentication for the CIFS shares is a credentials file. This file must be accessable and populated an example of a typical cred file can be found Accessing_Network_Shares#User_Specific_Actions

You must make sure that the location for the backup file is mounted and writeable.

Setup the script

The header of the script header documents the user defined parameters

    # EXTDIR must be external i.e. not on the SD card for example a usb stick or network share
    # BACKUPDIR is the location to write the backup on EXTDIR
    # PURGE determines if old backups are deleted before zipping the latest sucessful backup, PURGE=false
    # CIFSHOST is the optional network share host name or address, comment out to disable mounting a CIFSSHARE
    # CIFSSHARE is the share on the CIFSHOST, required if CIFSHOST is enabled
    # CIFSCRED is the local path to the CIFS credentials, required if CIFSHOST is enabled
    # CIFSUID is the users who has write permissions to CIFSSHARE, required if CIFSHOST is enabled

Edit these as required

CIFS example

    EXTDIR=/media/cifstemp/Public
    BACKUPDIR=$EXTDIR/RPi_autobuild_backup
    PURGE=false
    CIFSHOST=//192.168.0.8
    CIFSSHARE=Public
    CIFSCRED=/home/raspberrypi/cifs.cred
    CIFSUID=raspberrypi

External HDD example

Manually mount your USB drive first.

sudo mkdir /media/usbdrive
sudo mount /dev/sda1 /media/usbdrive

Edit the script settings to match

    EXTDIR=/media/usbdrive
    BACKUPDIR=$EXTDIR/RPi_autobuild_backup
    PURGE=true
    #CIFSHOST=//192.168.0.8
    CIFSSHARE=Public
    CIFSCRED=/home/raspberrypi/cifs.cred
    CIFSUID=raspberrypi

Here, CIFS mounting is disabled and PURGE has optionally been set to true to delete old backup off the usb drive due to capacity limitations.

Test the Script

$sudo sh AutoUpdateBackup

Move the script to cron

Assuming you've resolved all the permissions and the scripts runs, copy the script to cron.daily.

sudo cp /home/jack/AutoUpdateBackup.sh /etc/cron.daily/AutoUpdateBackup

Modify the permissions of the script to make it executable

sudo chmod +x /etc/cron.daily/AutoUpdateBackup

The script will now run once per day. Note if you change the file name don't add an extension because script filenames in cron.daily/, cron.hourly/, etc., should not contain dot(.), otherwise run-parts will skip them. Note. as of 2013.06.09 for some reason the script doesn't work in cron

debugging cron

By default debian on which Raspian is based doesn't have the log enabled by default [2]. To enable logs edit /etc/rsyslog.conf

sudo nano /etc/rsyslog.conf

uncomment the the line

# cron.*                          /var/log/cron.log

The logs for cron can be found /var/log/cron.log

To determine when on your system cron.daily will run read /etc/crontab

17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

This shows cron.daily running at 06:25 each day. If you're having problems with cron.daily its possible to modify the time to aid with debugging.

Logfile

To redirect the output the the script to a logfile uncomment the following line in the script header

#exec &> /var/log/AutoUpdateBackup.log

to have the log rotate modify /etc/logrotate.d/rsyslog and add the following entry after /var/log/messages

/var/log/AutoUpdateBackup.log


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