#!/bin/sh # # backup-servers # John Simpson 2005-09-12 # # Documentation: http://www.jms1.net/docs/rsync-backup.shtml # ############################################################################### # # Copyright (C) 2005,2006,2007,2008 John Simpson. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 or version 3 of the # license, at your option. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # ############################################################################### PATH="/usr/bin:/bin" ############################################################################## # # configuration BACKUPDIR="/backup" LOGFILE="$BACKUPDIR/backup.log" # in addition to syslog SERVERS="server1.domain.abc server2.domain.xyz" # servers to be backed up KEY="/root/.ssh/id_dsa_backup" # ssh key file EXCLUDES="/dev /proc /sys /tmp /var/tmp" EXCLUDES="$EXCLUDES /var/lib/pgsql/data /var/lib/mysql" EXCLUDES="$EXCLUDES /var/qmail/queue /var/qmail/simscan" # make sure cron's output gets mailed to the right place MAILTO="postmaster@domain.xyz" export MAILTO ############################################################################## function log() { logger -t backup -p local5.info "$@" if [ -n "${LOGFILE:-}" ] then echo `date` "$@" >> $LOGFILE fi } ############################################################################## ############################################################################## ############################################################################## # make rsync use ssh with the correct key RSYNC_RSH="/usr/bin/ssh -i $KEY" export RSYNC_RSH # make sure agent keys don't override the backup key unset SSH_AUTH_SOCK ######################################## # if we were called with "-v", pass the "-v" along to rsync if [ "$1" == "-v" ] then OPTV="-v" else OPTV="" fi ######################################## # build list of excludes EXC="" for e in $EXCLUDES do EXC="$EXC --exclude=$e" done ######################################## # let's do it log '/====================' log "backup-server $OPTV starting" for SERVER in $SERVERS do ######################################## # make sure target directory exists TARGET="$BACKUPDIR/$SERVER" if [ ! -d $TARGET ] then mkdir -p -m 0755 $TARGET fi ######################################## # make sure an earlier backup isn't already running if egrep -q "^rsync.*${TARGET}" /proc/*/cmdline > /dev/null 2>&1 then log "$SERVER already in progress" continue fi ######################################## # do the deed log "$SERVER starting" rsync -aS $OPTV --delete $EXC --bwlimit=1024 $SERVER:/ ${TARGET}/ rv=$? log "$SERVER done rv=$rv" done log "backup complete" log '\===================='