Difference between revisions of "Auto Update Backup"

From SingletonMillerWiki
Jump to: navigation, search
m
m
Line 126: Line 126:
 
== References ==
 
== References ==
 
#[http://www.raspberrypi.org/phpBB3/viewtopic.php?p=136912 The Raspberry Pi Backup Thread]
 
#[http://www.raspberrypi.org/phpBB3/viewtopic.php?p=136912 The Raspberry Pi Backup Thread]
 +
#[http://raspberrypi.stackexchange.com/questions/5427/can-a-raspberry-pi-be-used-to-create-a-backup-of-itself can-a-raspberry-pi-be-used-to-create-a-backup-of-itself]

Revision as of 10:39, 19 May 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. The step to getting this work are.

  1. installed the script
  2. create a backup location
  3. enable the script in cron.


Installation

Copy the script below to a file on you raspberry pi

wget 

Create a Backup Location

This location must not be on the Pi's SD card, it should be an external USB drive or other share on you network

CIFS share

Backup SD Image Accessing Network Shares


create a directory

sudo mkdir /media/cifshares

Secondly, mount the location that will receive the backup:

sudo mount -t cifs //hostname/sharename -o username=xxxx,password=yyyy /media/cifshares


Update and Backup Cron Script

Based on the excellent work [1] the following script does the majority of theheavy lifiting.

    #!/bin/bash

    # Setting up directories
    SUBDIR=raspberrypi_backups
    DIR=/hdd/$SUBDIR

    echo "Updating System"

    apt-get update
    apt-get upgrade

    echo "Starting RaspberryPI backup process!"

    # First check if pv package is installed, if not, install it first
    PACKAGESTATUS=`dpkg -s pv | grep Status`;

    if dpkg -s pv | grep -q Status; then
       then
          echo "Package 'pv' is installed."
       else
          echo "Package 'pv' is NOT installed."
          echo "Installing package 'pv'. Please wait..."
          apt-get -y install pv
    fi

    # Check if backup directory exists
    if [ ! -d "$DIR" ];
       then
          echo "Backup directory $DIR doesn't exist, creating it now!"
          mkdir $DIR
    fi

    # Create a filename with datestamp for our current backup (without .img suffix)
    OFILE="$DIR/backup_$(date +%Y%m%d_%H%M%S)"

    # Create final filename, with suffix
    OFILEFINAL=$OFILE.img

    # First sync disks
    sync; sync

    # Shut down some services before starting backup process
    echo "Stopping some services before backup."
    service apache2 stop
    service mysql stop
    service cron stop

    # Begin the backup process, should take about 1 hour from 8Gb SD card to HDD
    echo "Backing up SD card to USB HDD."
    echo "This will take some time depending on your SD card size and read performance. Please wait..."
    SDSIZE=`blockdev --getsize64 /dev/mmcblk0`;
    pv -tpreb /dev/mmcblk0 -s $SDSIZE | dd of=$OFILE bs=1M conv=sync,noerror iflag=fullblock

    # Wait for DD to finish and catch result
    RESULT=$?

    # Start services again that where shutdown before backup process
    echo "Start the stopped services again."
    service apache2 start
    service mysql start
    service cron start

    # If command has completed successfully, delete previous backups and exit
    if [ $RESULT = 0 ];
       then
          echo "Successful backup, previous backup files will be deleted."
          rm -f $DIR/backup_*.tar.gz
          mv $OFILE $OFILEFINAL
          echo "Backup is being tarred. Please wait..."
          tar zcf $OFILEFINAL.tar.gz $OFILEFINAL
          rm -rf $OFILEFINAL
          echo "RaspberryPI backup process completed! FILE: $OFILEFINAL.tar.gz"
          exit 0
    # Else remove attempted backup file
       else
          echo "Backup failed! Previous backup files untouched."
          echo "Please check there is sufficient space on the HDD."
          rm -f $OFILE
          echo "RaspberryPI backup process failed!"
          exit 1
    fi

References

  1. The Raspberry Pi Backup Thread
  2. can-a-raspberry-pi-be-used-to-create-a-backup-of-itself