192 lines
5.8 KiB
Bash
Executable file
192 lines
5.8 KiB
Bash
Executable file
#!/bin/bash
|
|
#
|
|
# egrdbackup
|
|
#
|
|
# (c) 2006 Egressive Limited
|
|
# Backup script using rdiff-backup
|
|
# by rob@egressive.com
|
|
# modified by dave@egressive.com 20060920
|
|
# modified rob@egressive.com 20061206 moved config items into seperate .conf file
|
|
#
|
|
##
|
|
# Description - egrdbackup performs one task:
|
|
#
|
|
# Performs incremental backups of designated directory
|
|
# hierarchy or backup set (minus any exclusions) into a designated
|
|
# backup directory.
|
|
#
|
|
# The current view of the data at any time will reflect the most
|
|
# recent state of all the files in the backup set. Diffs for
|
|
# older versions of all files are stored in hidden directories.
|
|
# Older versions of any file can be recovered using rdiff-backup directly.
|
|
#
|
|
# For example, to recover a backup of the directory (and contents) of
|
|
# /home/operations from 4 days ago, where the backup directory is /backups, run:
|
|
#
|
|
# rdiff-backup -r 4D /backups/home/operations/ /home/operations/recovered-$(date "+%Y-%m-%d")
|
|
#
|
|
##
|
|
#
|
|
# Default Settings
|
|
#
|
|
DEBUG=false
|
|
#
|
|
if [ $DEBUG == "true" ] ; then
|
|
echo "Default Settings"
|
|
fi
|
|
#
|
|
RDIFF_COMMAND=`which rdiff-backup`
|
|
MAIL_COMMAND=`which mail`
|
|
DF_COMMAND=`which df`
|
|
GREP_COMMAND=`which grep`
|
|
TAIL_COMMAND=`which tail`
|
|
NICE_COMMAND=`which nice`
|
|
HEAD_COMMAND=`which head`
|
|
CAT_COMMAND=`which cat`
|
|
DIRNAME_COMMAND=`which dirname`
|
|
BASENAME_COMMAND=`which basename`
|
|
MAIL_TO=root # temporary - overridden in CONF_FILE
|
|
#
|
|
#
|
|
#EGRD_ROOT=/etc/egscripts/egrdbackup
|
|
EGRD_ROOT=`$DIRNAME_COMMAND $0`
|
|
EGRD_SCRIPT=`$BASENAME_COMMAND $0`
|
|
# config file
|
|
CONF_FILE=$EGRD_ROOT/$EGRD_SCRIPT.conf
|
|
# name of file to run before and after rdiff-backup runs - for example an initial rsync or usb mount
|
|
INITIAL_CMD_FILE="initial.sh"
|
|
POST_CMD_FILE="postrun.sh"
|
|
#
|
|
INITIAL_CMD_FILE_OK="true"
|
|
#
|
|
# initialise end status
|
|
BACKUP_STATUS="[SUCCESS]"
|
|
#
|
|
# Get local configuration
|
|
#
|
|
if [ $DEBUG == "true" ] ; then
|
|
echo "Get Local Settings"
|
|
fi
|
|
if [ -x $CONF_FILE ] ; then
|
|
. $CONF_FILE
|
|
else
|
|
$CAT_COMMAND $LOGFILE | $MAIL_COMMAND -s "[ERROR] Could not find config file for $SERVERNAME backup" $MAIL_TO
|
|
exit 1
|
|
fi
|
|
# logfile
|
|
LOGFILE=/var/log/$EGRD_LOGNAME.log # use logrotate to manage logs
|
|
#
|
|
#
|
|
##
|
|
if [ $DEBUG == "true" ] ; then
|
|
echo "Remove Older versions"
|
|
fi
|
|
#
|
|
# You will need a --force the first time you run, or if you clear out the $RDIFF_DEST
|
|
# directory at any stage. Probably safer not to use it normally.
|
|
#
|
|
# check to see if the default rdiff revision directory is in place - if so, don't force
|
|
#
|
|
if [ "$RDIFF_FORCE" == "true" ] ; then
|
|
RDIFF_FORCE_ARG=" --force "
|
|
fi
|
|
#
|
|
#
|
|
# If an initial command file exists (say to do an rsync first)
|
|
#
|
|
if [ -x $EGRD_ROOT/$INITIAL_CMD_FILE ] ; then
|
|
. $EGRD_ROOT/$INITIAL_CMD_FILE
|
|
fi
|
|
#
|
|
if ! [ $INITIAL_CMD_FILE_OK == "true" ]
|
|
then
|
|
BACKUP_STATUS="[ERROR]"
|
|
BACKUP_ERROR=" | Error during initial setup"
|
|
echo "Fatal error while running initial.sh" >> $LOGFILE 2>> $LOGFILE
|
|
$CAT_COMMAND $LOGFILE | $MAIL_COMMAND -s "$BACKUP_STATUS $SERVERNAME backup $BACKUP_ERROR" $MAIL_TO >> $LOGFILE 2>> $LOGFILE
|
|
echo "Email sent to $MAIL_TO" >> $LOGFILE
|
|
echo "Script complete" >> $LOGFILE 2>> $LOGFILE
|
|
echo "" >> $LOGFILE
|
|
exit 1
|
|
fi
|
|
|
|
if test -d $RDIFF_DEST/rdiff-backup-data ; then
|
|
if test -n $RDIFF_DAYS_OF_REVISIONS ; then
|
|
#
|
|
echo "++++++++++++++++++++++++++++++++++++++++++" >> $LOGFILE
|
|
echo " Deleting revisions older than $RDIFF_DAYS_OF_REVISIONS days " >> $LOGFILE
|
|
echo "++++++++++++++++++++++++++++++++++++++++++" >> $LOGFILE
|
|
# run delete command...
|
|
echo "Running command: $NICE_COMMAND $RDIFF_COMMAND --force --remove-older-than ${RDIFF_DAYS_OF_REVISIONS}D $RDIFF_DEST" >> $LOGFILE
|
|
if ! $NICE_COMMAND $RDIFF_COMMAND --force --remove-older-than ${RDIFF_DAYS_OF_REVISIONS}D $RDIFF_DEST >> $LOGFILE 2>> $LOGFILE ; then
|
|
if ! $TAIL_COMMAND -2 $LOGFILE | $GREP_COMMAND "No increments older" > /dev/null 2>> $LOGFILE ; then
|
|
BACKUP_STATUS="[ERROR]"
|
|
BACKUP_ERROR=" | Error deleting revisions"
|
|
fi
|
|
fi
|
|
fi
|
|
fi
|
|
#
|
|
##
|
|
ALL_RDIFF_EXCLUDES=""
|
|
if [ -n "$RDIFF_EXCLUDES" ] ; then
|
|
for i in $RDIFF_EXCLUDES ; do
|
|
ALL_RDIFF_EXCLUDES=$ALL_RDIFF_EXCLUDES" --exclude $i "
|
|
done
|
|
fi
|
|
#
|
|
# Start Rdiff-backup!
|
|
#
|
|
# make sure log file exists
|
|
if [ $DEBUG == "true" ] ; then
|
|
echo "Start backup"
|
|
fi
|
|
touch $LOGFILE
|
|
DATE=`date`
|
|
echo "+++++++++++++++++++++++++++++++++++++++++++" >> $LOGFILE
|
|
echo " Starting backup ($DATE) " >> $LOGFILE
|
|
echo "+++++++++++++++++++++++++++++++++++++++++++" >> $LOGFILE
|
|
echo "Running command: $NICE_COMMAND $RDIFF_COMMAND $RDIFF_ARGS $ALL_RDIFF_EXCLUDES $RDIFF_SRC $RDIFF_DEST " >> $LOGFILE
|
|
if ! $NICE_COMMAND $RDIFF_COMMAND $RDIFF_FORCE_ARG $RDIFF_ARGS $ALL_RDIFF_EXCLUDES $RDIFF_SRC $RDIFF_DEST \
|
|
>> $LOGFILE 2>> $LOGFILE ; then
|
|
echo "Error Code - rdiff-backup returned: $?" >> $LOGFILE
|
|
fi
|
|
if ! $TAIL_COMMAND -3 $LOGFILE | $GREP_COMMAND "Errors 0" > /dev/null 2>> $LOGFILE
|
|
then
|
|
BACKUP_STATUS="[ERROR]"
|
|
BACKUP_ERROR=$BACKUP_ERROR" | Error during backup"
|
|
fi
|
|
#
|
|
if [ $DEBUG == "true" ] ; then
|
|
echo "============================="
|
|
tail -10 $LOGFILE
|
|
echo "============================="
|
|
fi
|
|
#
|
|
if [ $DEBUG == "true" ] ; then
|
|
echo "Log and email out results"
|
|
fi
|
|
#
|
|
echo "File system usage:" >> $LOGFILE
|
|
$DF_COMMAND -h >> $LOGFILE 2>> $LOGFILE
|
|
#
|
|
if [ $DEBUG == "true" ] ; then
|
|
echo "Finish"
|
|
fi
|
|
#
|
|
DATE=`date`
|
|
echo "+++++++++++++++++++++++++++++++++++++++++++" >> $LOGFILE
|
|
echo " Completed backup ($DATE) " >> $LOGFILE
|
|
echo "+++++++++++++++++++++++++++++++++++++++++++" >> $LOGFILE
|
|
#
|
|
#
|
|
# If a post command file exists (say to unmount a usb drive)
|
|
#
|
|
if [ -x $EGRD_ROOT/$POST_CMD_FILE ] ; then
|
|
. $EGRD_ROOT/$POST_CMD_FILE
|
|
fi
|
|
#
|
|
$CAT_COMMAND $LOGFILE | $MAIL_COMMAND -s "$BACKUP_STATUS $SERVERNAME backup $BACKUP_ERROR" $MAIL_TO >> $LOGFILE 2>> $LOGFILE
|
|
echo "Email sent to $MAIL_TO" >> $LOGFILE
|
|
echo "Script complete" >> $LOGFILE 2>> $LOGFILE
|
|
echo "" >> $LOGFILE
|