Backup Scripts

#!/bin/sh
# filename: backup.sh
# location: /root/backup/backup.sh
# modified : Andrew Stringer 25/04/2004 onwards
# modified : AJS 21/11/06 to backup files to nfs mounted directory

#my name
HOST='ns0'
BACKUPLIST='/etc/backup/backuplist.txt'
MAILRESULT='an.st@xxx.gov.uk'
BACKUPHOST='10.1.1.1'
BACKUPHOSTPATH='/vol/dnsbackup'
LOCALWRKDIR='/tmp/dnsbackup'
MOUNTPATH='/mnt/dnsbackup'
LOGFILE="/root/log.txt"



#set file to just day number, this will create up to 31 unique 
#backup files and allow overwriting to stop files getting too many
DATE=`date +%d`
#today is in format 01-01-2006 
TODAY=`date +%d-%m-%Y`


#Check if remote file system is mounted
printf "%s\n" "Checking if nfs is mounted"
if  mount|grep ${BACKUPHOSTPATH} >/dev/null 2>&1
then
        printf "\t%s\n" "${BACKUPHOSTPATH} mounted"
else
        printf "\t%s\n" "${BACKUPHOSTPATH} not mounted, trying to mount"
        mount ${BACKUPHOST}:${BACKUPHOSTPATH} ${MOUNTPATH}

#check mount, bale out if not.
if  mount|grep dnsbackup >/dev/null 2>&1
then
        printf "\t%s\n" "${BACKUPHOSTPATH} mounted"
else
#mail error
	printf "\t%s\n" "Still not mounted, sending mail & baling out."
	echo "Unable to mount nfs directory." |mailx  -r "backup" -s "ns0.brm.pri - unable to mount dnsbackup, ${TODAY}" ${MAILRESULT}
	exit 1
fi
fi


#check if backup location exists, as it is on tmpfs it will be destroyed
#each server reboot
if [ -d ${LOCALWRKDIR} ]; then
        printf "\t%s\n" "${LOCALWRKDIR} exists"
else
        printf "\t%s\n" "${LOCALWRKDIR} does not exist, creating"
        mkdir ${LOCALWRKDIR}
fi



#remove previous backup
printf "%s" "Remove old backup from local server"
rm ${LOCALWRKDIR}/* >/dev/null 2>&1
printf "%s\n" "....Done"


printf "%s" "Start tar of files"
tar cvf ${LOCALWRKDIR}/backup.tar `cat ${BACKUPLIST}` > ${LOGFILE} 2>&1
printf "%s\n" "....Done"
printf "%s\n" "tar file is `du -h ${LOCALWRKDIR}/*`"

#gzip file
printf "%s" "gzip'ing tar file"
gzip ${LOCALWRKDIR}/backup.tar
printf "%s\n" "....Done"
printf "%s\n" "gzipped file is `du -h ${LOCALWRKDIR}/*`"

#copy file to todays date
printf "%s" "Appending date"
mv ${LOCALWRKDIR}/backup.tar.gz ${LOCALWRKDIR}/backup-${HOST}-${DATE}.tar.gz
printf "%s\n" "....Done"

#move backed up file to nfs mount
printf "%s" "Moving file to remote host"
mv ${LOCALWRKDIR}/backup-${HOST}-${DATE}.tar.gz ${MOUNTPATH}/${HOST}
printf "%s\n" "....Done"
printf "%s\n" "Remote file is `du -h  ${MOUNTPATH}/${HOST}/*`"

#send confirmation
printf "%s" "Sending mail confirmation"
echo "Backup successful." | mailx -r "backup" -s "$HOST - Backup Success ${TODAY}" ${MAILRESULT}
printf "%s\n" "....Done"

#umount nfs directory
printf "%s" "umount'ing nfs directory"
umount ${BACKUPHOST}:${BACKUPHOSTPATH}
printf "%s\n" "....Done"


exit 0

rb/backup.txt · Last modified: 10/10/2013 15:21 (external edit)