#!/bin/bash # This script does backups of the files in /, /boot, and /opt, using # rsync into time-specific directories, using hard links to the previous # directory to reduce space. #### Configuration: Edit the next few lines to set this script up. # BUILDEXCLUDE: Full path the the build.exclude script that creates the # exclude list for rsync. # CHECKMOUNT: Full path the the isbakmounted script that verifies the # backup drive is mounted # MOUNTDIR: The backup drive to mount (IP:DIR). # BACKUPDIR: Your mount point for the backup drive. # OPTEXCLUDEDIR: Backup drive relative to your /opt. For instance, if you # mount your backup drive at /opt/backups, enter backups here. # ROOTDIR: Subdirectory of BACKUPDIR where you store / (root) backups. # BOOTDIR: Subdirectory of BACKUPDIR where you store /boot backups. # OPTDIR: Subdirectory of BACKUPDIR where you store /opt backups. BUILDEXCLUDE=/YOURSCRIPTDIR/build.exclude CHECKMOUNT=/YOURSCRIPTDIR/is.bak.mounted MOUNTDIR="YOURIPADDRESS:YOURNFSDIR" BACKUPDIR=/opt/WHEREYOUMOUNTIT OPTEXCLUDEDIR=WHEREYOUMOUNTIT ROOTDIR=rootbackups BOOTDIR=bootbackups OPTDIR=optbackups #### End of configuration DATESTR=`date '+%Y-%m-%d-h%Hm%Ms%S'` # Mount backup drive and verify it is mounted. echo Mounting backup drive $BACKUPDIR mount $MOUNTDIR $BACKUPDIR if !(`$CHECKMOUNT`) then echo "Backup drive is not mounted or dir is missing, aborting backup" umount $BACKUPDIR exit 1 fi # Actually run the backups. # Find the previous backup directory, if any, for hard links. ROOTPREV=`ls -drt $BACKUPDIR/$ROOTDIR/root-* | tail -1` BOOTPREV=`ls -drt $BACKUPDIR/$BOOTDIR/boot-* | tail -1` OPTPREV=`ls -drt $BACKUPDIR/$OPTDIR/opt-* | tail -1` if [ $ROOTPREV ] then if (test -d $ROOTPREV) then echo Found previous root backup at $ROOTPREV ROOTPREV="--link-dest=$ROOTPREV" fi fi if [ $BOOTPREV ] then if (test -d $BOOTPREV) then echo Found previous boot backup at $BOOTPREV BOOTPREV="--link-dest=$BOOTPREV" fi fi if [ $OPTPREV ] then if (test -d $OPTPREV) then echo Found previous opt backup at $OPTPREV OPTPREV="--link-dest=$OPTPREV" fi fi # Make the exclude list for rsync. $BUILDEXCLUDE /tmp/exclude_bu.list # Create the target directories. echo Making new backup directories $DATESTR NEWROOTDIR=$BACKUPDIR/$ROOTDIR/root-$DATESTR NEWBOOTDIR=$BACKUPDIR/$BOOTDIR/boot-$DATESTR NEWOPTDIR=$BACKUPDIR/$OPTDIR/opt-$DATESTR mkdir $NEWROOTDIR mkdir $NEWBOOTDIR mkdir $NEWOPTDIR # Run the rsyncs. echo `/bin/df | /bin/grep $BACKUPDIR` echo Starting rsync for root /usr/bin/rsync -a --delete $ROOTPREV --exclude-from=/tmp/exclude_bu.list / $NEWROOTDIR/ echo `/bin/df | /bin/grep $BACKUPDIR` echo Starting rsync for boot /usr/bin/rsync -a --delete $BOOTPREV /boot/ $NEWBOOTDIR/ echo `/bin/df | /bin/grep $BACKUPDIR` echo Starting rsync for opt /usr/bin/rsync -a --delete $OPTPREV --exclude=$OPTEXCLUDEDIR /opt/ $NEWOPTDIR/ echo `/bin/df | /bin/grep $BACKUPDIR` # Unmount backup dir, after waiting a short time to let the file system # calm down. echo Waiting 20 seconds... sleep 20s echo Unmounting file system $BACKUPDIR umount $BACKUPDIR