#! /bin/bash # # Manage backup file aging # received from a remote server by scp # Full documentation on www.fridu.org -> documentation section (wiki) # Author Dominique Le Foll dominig@fridu.co.uk # License GNU # July 2006 # V 1.0 # #------------------------------------------------------------------------- # # Constant and Variables # #------------------------------------------------------------------------- # Site name as printed on reports readonly SITENAME="www.fridu.org www.agnescreations.co.uk" # space separated list of receiver of mail report readonly REPORT_TO="backupadmin@mydomain.eu" # Number of day to keep archived data readonly DAYS_ARCHIVED="15" # path to the backup history to maintain readonly TAR_PATH="/export/my-large-disk/www.backup" # Tag for syslog # NOTE this tag must be present in the archive file name to clean as it is used in the find command later readonly LOGGER_TAG="Bakup" # commands Path inclusion is advised to overcome issue with $PATH in cron job readonly LOGGER="/usr/bin/logger" readonly MAIL="/usr/bin/mail" readonly FIND="/usr/bin/find" readonly RM="/bin/rm" #--------------------------------------------------------------------- # # You should NOT have to change anything beyond this point # # except if you really know what you do don't change the code # Validating and debugging complex script is tougher than it seems # #--------------------------------------------------------------------- declare PidValue="" declare FileListToClean="" declare NumFileToClean="" #----------------------------------------------------------------------- # # Functions # #----------------------------------------------------------------------- # Sanity check function checkExec() { if ! [ -x $1 ]; then echo >&2 $0 Error $1 is not a valid check exec path in $0. # try to log even if logger may not be available or in the PATH logger -p 0 -i $PidValue -t $LOGGER_TAG "$SITENAME backup failled $0 Error $1 is not a valid check exec path in $0" exit 5 fi } function checkDir() { if ! [ -d $1 ]; then echo >&2 $0 Error $1 is not a valid directory check path in $0. # log logger -p 0 -i $PidValue -t $LOGGER_TAG "$SITENAME backup failled $0 Error $1 is not a valid directory check path in $0" exit 5 fi } function sanityCheck() { for Exec in "$LOGGER $MAIL $FIND $RM" do checkExec $Exec done for Dirs in "$TAR_PATH" do for Dir in $Dirs do checkDir $Dir done done } # Syslog function log() { $LOGGER -i $PidValue -t $LOGGER_TAG $* } # stderr + syslog function warn() { log "-s $*" } # clean old backups and logs function cleanOldBackup() { local FileToClean="$TAR_PATH/fileToClean.lst" # ATTENTION smart quoting rules required to path all special characters (check bash reference guide) # direct user of -exec is problematic because of patern {} $FIND $TAR_PATH -atime +$DAYS_ARCHIVED -name \*$LOGGER_TAG\* > $FileToClean NumFileToClean=`wc --lines < $FileToClean` if [ $NumFileToClean != "0" ]; then FileListToClean=`cat $FileToClean` for File in $FileListToClean do $RM $File if [ $? != 0 ]; then warn "Error deleting $File" break fi done fi } #----------------------------------------------------------------------------- # # Entry point # #----------------------------------------------------------------------------- PidValue=`pidof $0` # Sanity check sanityCheck # Clean up log and old tar cleanOldBackup if [ $? != 0 ]; then warn "$SITENAME backup major error : $* mail sent to $REPORT_TO" $MAIL -s "$*" $REPORT_TO << END_OF_MAIL_BODY Check $REMOTE_DIR files backup received from $SITENAME did not cleaned out File list to clean is : $FileListToClean END_OF_MAIL_BODY exit 1 fi # exit clean log "$SITENAME file backup clean out done $NumFileToClean file(s) deleted" exit 0