#!/bin/bash # # Copyright (c) 2007 by FR Technologies, LLC All Rights Reserved # # This software is provided `AS IS' and without any express or implied # warranties, including, without limitation, the implied waranties of # merchantability and fitness for a particular purpose. # # Redistribution and use of source are permitted provided that: # # (1) source distributions may not be sold for profit on physical media # such as disks, tapes, and CD-ROMS, without expressed written permission # from the author. # # @(#) FRTech.netident $Id: rsync_backup.sh,v 1.7 2007/01/15 16:34:57 noloc Exp $ # # Author: Gil Colon # Creation date: 2007-01-13 # # Description: rsync backup local directories to disk. Incremental backups. # Day of week to run the fsck. Default is Sunday. Change to match your location # Derived by: uname '+%u'; Mon=1, Tues=2, Wed=3, Thur=4, Fri=5, Sat=6, Sun=7 # # Syntax: rsync_backup.sh has variables that can be set within script # # Changes log # ------------ # # Date Version Author # Chg. Desc.: Added standard FR Technologies copyright header # Chg. Desc.: Added to CVSROOT/modules # Chg. Desc.: Fixed error handling pre-mounted disk to gracefully handle FSCK # Chg. Desc.: Redirected FSCK output to /dev/null # # Description: Original posting into CVS repository # # @(#) FRTechident $Id: rsync_backup.sh,v 1.7 2007/01/15 16:34:57 noloc Exp $ # message() { /bin/mail -s "${SUBJECT}" root <> ${LOG} /usr/bin/rsync ${RSYNCOPTS} /${dir}/ ${BASEDIR}/${dir}/ >> ${LOG} 2>&1 done } checkfs() { /bin/echo "Performing file system consistency check for ${BASEDIR}" >> ${LOG} # # Read the man page for e2fsck for more options on it's use. # /sbin/e2fsck -f -c -n ${RAWDEV} > /dev/null 2>&1 /bin/echo "Consistency check completed, backup beginning...." >> ${LOG} } mntdsk() { /bin/echo "Mounting backup disk drive." >> ${LOG} /bin/mount ${RAWDEV} ${BASEDIR} >> ${LOG} 2>&1 } umntdsk() { /bin/echo "Un-mounting backup disk drive." >> ${LOG} /bin/umount ${BASEDIR} >> ${LOG} 2>&1 } # # List of file systems and/or directories to be backed up # Listing is comprised of all top-level root (/) subdirectories. # Backup details # LIST="bin boot dev etc home lib root sbin usr var" # # Backup logfile # LOG="/tmp/rsync-messages.log" # # Read the man page for rsync for more options on it's use. # RSYNCOPTS="-a -l -v" # # Mount point of the removable media and the raw device name. Change to match your location # BASEDIR="/backup" RAWDEV="/dev/sdb" # # Date used for consistency check frequency of backup device. %u value is day # of week range from 1 - 7, where Monday is equal to 1. Check man page for date # for additional formatting options. # DATE=`/bin/date '+%u'` # # Day of week to run the fsck. Default is Sunday. Change to match your location # MYFSCKDAY="7" WHOAMI=`/usr/bin/whoami` export PATH LOG RSYNCOPTS BASEDIR RAWDEV DATE MYFSCKDAY WHOAMI # # Remove traces of old log files should they exist # if [ -f ${LOG} ] then /bin/rm -f ${LOG} fi # # We want to be sure that this program is only run by the root user. # If you are not the root user, then abort the program. # if [ "${WHOAMI}" != "root" ] then /bin/echo "You must be the root user to use this program" exit 0 fi /bin/mount | /bin/grep backup > ${LOG} 2>&1 if [ $? -eq 0 ] then # # Lets see if we can unmount the backup device # If successful we will proceed, if not, then we # we will exit and send a help message # umntdsk; if [ $? -eq 1 ] then SUBJECT="Backup System Problem" MESSAGE="Problem with ${BASEDIR} and/or ${RAWDEV} on `/bin/date`. Can't unmount disk." message; exit 0 else mntdsk; if [ $? -eq 1 ] then SUBJECT="Backup System Problem" MESSAGE="Problem with ${BASEDIR} and/or ${RAWDEV} on `/bin/date`. Can't re-mount disk." message; exit 0 fi fi fi END="FALSE" while [ "${END}" == "FALSE" ] do # # If ${DATE} is not == ${MYFSCKDAY}, then perform consistency check. # Mount the backup device # if [ "${DATE}" == "${MYFSCKDAY}" ] then # # Perform a consistency check. # Mount the backup device # Perform the rsync backup # Umount the backup device # checkfs; mntdsk; backup; umntdsk # # Send backup log message # SUBJECT="System Backup w/ FSCK" MESSAGE="FSCK performed today `/bin/date`. Log: `cat ${LOG}`" message; END=TRUE; exit 0 else /bin/echo "No consistency check today `/bin/date`" >> ${LOG} /bin/echo "Mounting backup disk drive." >> ${LOG} # # Mount the backup device # Perform the rsync backup # Umount the backup device # mntdsk; backup; umntdsk # # Send backup log message # SUBJECT="System Backup no FSCK" MESSAGE="No FSCK check today `/bin/date`. Log: `cat $LOG`" message; END=TRUE; exit 0 fi done # # Always double check your backups to be sure that they ran successfully. #