Script to backup server using rsync over ssh, deployed on ticket:763 to wiki:ParrotServer, wiki:PenguinServer and wiki:PuffinServer. {{{ #!sh #!/usr/bin/env bash # List of local directories to be backed up # is contained in this file, one per line BACKUP_LIST="/etc/agile/backup" # The hostname of this server, used to create # a directory on the remote server HOSTNAME=$(hostname) # The server to backup to, this is set in # /root/.ssh/config SSH_SERVER="backup" # This script uses lockfile which comes with procmail LOCKFILE_BINARY="/usr/bin/lockfile" LOCKFILE="/var/run/lock/$(basename $0).lock" # Write the outcome to this log LOGFILE="/var/log/$(basename $0)" # Check that the script is being run by root if [[ "$(id -u)" != "0" ]] ; then echo "You must run '$0' as root or via sudo" exit 1 fi # Test if the lockfile binary can be found if [[ ! -e "$LOCKFILE_BINARY" ]]; then echo "$LOCKFILE_BINARY not found, please install the procmail package." exit 1 fi # if the $LOCKFILE exists then exit # the lockfile is read only # the timeout is set to 2 hours (7200 secs) # if the lockfile is older than this it will be # removed, this need to be improved so the log is uupdated $LOCKFILE_BINARY -r 1 -l 7200 $LOCKFILE || exit 23 # Do the backup, first test that the file # with the list of files to be backed up # exists if [[ -f ${BACKUP_LIST} ]]; then for dir in $(<${BACKUP_LIST}); do # Make the directory on the backup server ssh ${SSH_SERVER} mkdir -p ${HOSTNAME}/${dir} # Copy the files to the backup server # Test for the scripts has been run with -v # for verbose output if [[ $1 == "-v" ]]; then rsync -av --delete ${dir}/ ${SSH_SERVER}:${HOSTNAME}/${dir}/ else rsync -aq --delete ${dir}/ ${SSH_SERVER}:${HOSTNAME}/${dir}/ 2>&1 fi # catch errors # http://idolinux.blogspot.co.uk/2008/08/bash-script-error-handling.html if [ $? != 0 ]; then { echo "Backup Error at $(date +%c)" >> $LOGFILE rm -vf $LOCKFILE exit 23 } fi done else echo "$BACKUP_LIST doesn't exist" echo "Backup Error at $(date +%c)" >> $LOGFILE rm -vf $LOCKFILE exit 23 fi # Remove the lock file and update the log rm -vf $LOCKFILE echo "Backup Success at $(date +%c)" >> $LOGFILE }}}