All pastes #2118331 Raw Edit

Untitled

public text v1 · immutable
#2118331 ·published 2012-02-17 04:12 UTC
rendered paste body
[Feb 16 22:12 ajs@jupiter:/etc]$ cat backup_script.sh 
#!/bin/bash

# Paths to backup
# do weekly snapshots with latest 7 day incremental
#/media/archive/pictures
#/media/archive/music
#/home/ajs
#/var/lib/vmware
#/etc
#/media/svn

unset PATH	

# ------------- system commands used by this script --------------------
ID=/usr/bin/id;
ECHO=/bin/echo;

MOUNT=/bin/mount;
RM=/bin/rm;
MV=/bin/mv;
CP=/bin/cp;
TOUCH=/bin/touch;

RSYNC=/usr/bin/rsync;


# ------------- file locations -----------------------------------------

MOUNT_DEVICE=d4316ba7-0b8c-4731-a7e7-1711439f9dd1;
SNAPSHOT_RW=/media/backup;
EXCLUDES=/home/ajs/.backup_exclude;

BACKUP_PATHS="/media/archive/pictures /media/archive/music /home/ajs /etc /media/svn /media/archive/flac /media/archive/books /media/archive/educational";
WEEKLY_PATHS="/var/lib/vmware";

# ------------- the script itself --------------------------------------

# make sure we're running as root
if (( `$ID -u` != 0 )); then { $ECHO "Sorry, must be root.  Exiting..."; exit; } fi

# attempt to remount the RW mount point as RW; else abort
$MOUNT -o remount,rw -U $MOUNT_DEVICE ;
if (( $? )); then
{
	$ECHO "snapshot: could not remount $SNAPSHOT_RW readwrite";
	exit;
}
fi;



# rotating daily snapshots

# step 1: delete the oldest snapshot, if it exists:
if [ -d $SNAPSHOT_RW/daily.6 ] ; then			\
$RM -rf $SNAPSHOT_RW/daily.6 ;				\
fi ;

# step 2: shift the middle snapshots(s) back by one, if they exist
if [ -d $SNAPSHOT_RW/daily.5 ] ; then			\
$MV $SNAPSHOT_RW/daily.5 $SNAPSHOT_RW/daily.6 ;	\
fi;
if [ -d $SNAPSHOT_RW/daily.4 ] ; then			\
$MV $SNAPSHOT_RW/daily.4 $SNAPSHOT_RW/daily.5 ;	\
fi;
if [ -d $SNAPSHOT_RW/daily.3 ] ; then			\
$MV $SNAPSHOT_RW/daily.3 $SNAPSHOT_RW/daily.4 ;	\
fi;
if [ -d $SNAPSHOT_RW/daily.2 ] ; then			\
$MV $SNAPSHOT_RW/daily.2 $SNAPSHOT_RW/daily.3 ;	\
fi;
if [ -d $SNAPSHOT_RW/daily.1 ] ; then			\
$MV $SNAPSHOT_RW/daily.1 $SNAPSHOT_RW/daily.2 ;	\
fi;
if [ -d $SNAPSHOT_RW/daily.0 ] ; then			\
$MV $SNAPSHOT_RW/daily.0 $SNAPSHOT_RW/daily.1 ;	\
fi;

# step 3: make a hard-link-only (except for dirs) copy of the latest snapshot,
# if that exists
$RSYNC -va --delete --delete-excluded --exclude-from="$EXCLUDES" --log-file=$SNAPSHOT_RW/changelist \
	--link-dest=$SNAPSHOT_RW/daily.1 $BACKUP_PATHS $SNAPSHOT_RW/daily.0/

$MV $SNAPSHOT_RW/changelist $SNAPSHOT_RW/daily.0

# step 4: update the mtime of hourly.0 to reflect the snapshot time
$TOUCH $SNAPSHOT_RW/daily.0 ;

if [ "$1" = "weekly" ] ; then
{
	# step 1: delete the oldest snapshot, if it exists:
	if [ -d $SNAPSHOT_RW/weekly.2 ] ; then			\
	$RM -rf $SNAPSHOT_RW/weekly.2 ;				\
	fi ;

	if [ -d $SNAPSHOT_RW/weekly.1 ] ; then			\
	$MV $SNAPSHOT_RW/weekly.1 $SNAPSHOT_RW/weekly.2 ;	\
	fi;
	if [ -d $SNAPSHOT_RW/weekly.0 ] ; then			\
	$MV $SNAPSHOT_RW/weekly.0 $SNAPSHOT_RW/weekly.1 ;	\
	fi;

	# step 3: make a hard-link-only (except for dirs) copy of the latest snapshot,
	# if that exists
	$RSYNC -va --delete --delete-excluded --exclude-from="$EXCLUDES" --log-file=$SNAPSHOT_RW/changelist \
		--link-dest=$SNAPSHOT_RW/daily.0 $BACKUP_PATHS $SNAPSHOT_RW/weekly.0/

	$RSYNC -va --delete --delete-excluded --exclude-from="$EXCLUDES" --log-file=$SNAPSHOT_RW/changelist \
		--link-dest=$SNAPSHOT_RW/weekly.1 $WEEKLY_PATHS $SNAPSHOT_RW/weekly.0/

	$MV $SNAPSHOT_RW/changelist $SNAPSHOT_RW/weekly.0

	# step 4: update the mtime of hourly.0 to reflect the snapshot time
	$TOUCH $SNAPSHOT_RW/weekly.0 ;
}
fi;

# now remount the RW snapshot mountpoint as readonly

$MOUNT -o remount,ro -U $MOUNT_DEVICE ;
if (( $? )); then
{
	$ECHO "snapshot: could not remount $SNAPSHOT_RW readonly";
	exit;
} fi;