Created
February 26, 2011 17:09
-
-
Save soul9/845393 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SCRIPT_LOCATION=$(dirname $0) | |
#get common configuration | |
if [ -f ${SCRIPT_LOCATION}/hosts/common/conf ]; then | |
. ${SCRIPT_LOCATION}/hosts/common/conf | |
fi | |
#set per-host configuration | |
if [ -f ${SCRIPT_LOCATION}/hosts/$(hostname --fqdn)/conf ]; then | |
. ${SCRIPT_LOCATION}/hosts/$(hostname --fqdn)/conf | |
fi | |
#run pre-backup hooks | |
if [ -d ${SCRIPT_LOCATION}/hosts/$(hostname --fqdn)/pre-backup ]; then | |
for hook in ${SCRIPT_LOCATION}/hosts/$(hostname --fqdn)/pre-backup/* ; do | |
test -f "$hook" && \ | |
. $hook | |
done | |
fi | |
env | |
echo "Backup start date is $(date)" | |
#Rotate backups | |
echo "Rotating backups, backups currently kept: $NUMBACKUP." | |
echo "Backup is to $BCKIP using account $BCKUSER" | |
rmcmd="rm -rf $BCKHOSTDIR/$(hostname --fqdn)/$NUMBACKUP" | |
ssh $BCKUSER@$BCKIP "$rmcmd" | |
for i in $(seq $NUMBACKUP -1 1); do | |
mvcmd="mv $BCKHOSTDIR/$(hostname --fqdn)/$(echo $i - 1 |bc |tr -d '\n') $BCKHOSTDIR/$(hostname --fqdn)/$i" | |
ssh "${BCKUSER}@${BCKIP}" "$mvcmd" | |
done | |
ssh "${BCKUSER}@${BCKIP}" "cp -al $BCKHOSTDIR/$(hostname --fqdn)/1 $BCKHOSTDIR/$(hostname --fqdn)/0" | |
#Rotate log backups (be careful to escape non-local variables) | |
REMOTECMD=" | |
cd $BCKHOSTDIR/logs/$(hostname --fqdn) | |
last=\$(ls | sort -n|tail -1) | |
for i in \$(seq \$last -1 0); do | |
mv \$i \$(echo \$i + 1 |bc |tr -d '\n') | |
done | |
cp -al 1 0 | |
echo We have \$last log backups | |
" | |
echo "ssh ${BCKUSER}@${BCKIP}" | |
echo "$REMOTECMD" |sed 's,^, ,g' | |
echo "$REMOTECMD" |ssh "${BCKUSER}@${BCKIP}" | |
#disable filename globbing for /foo/** etc.. | |
set -f | |
#build up the list of files/directories to exclude | |
if [ -z $EXCLUDECMD ]; then | |
EXCLUDECMD="" | |
fi | |
for dir in $EXCLUDEDIRS; do | |
EXCLUDECMD="${EXCLUDECMD} --exclude $dir/**" | |
done | |
#make modification date reflect the date of the backup | |
ssh $BCKUSER@$BCKIP "touch $BCKHOSTDIR/$(hostname --fqdn)/0 $BCKHOSTDIR/logs/$(hostname --fqdn)/0" | |
#backup to last snapshot (main) | |
RSYNCMD="rsync $EXTRAARGS $EXCLUDECMD / $BCKDST/$(hostname --fqdn)/0" | |
echo $RSYNCMD | |
ionice -c $ionicelevel nice -n $nicelevel $RSYNCMD | |
#backup to last snapshot (logs) | |
RSYNCMD="rsync $EXTRAARGS /var/log $BCKDST/logs/$(hostname --fqdn)/0" | |
echo $RSYNCMD | |
ionice -c $ionicelevel nice -n $nicelevel $RSYNCMD | |
#re-enable filename globbing | |
set +f | |
#run post-backup hooks | |
if [ -d ${SCRIPT_LOCATION}/hosts/$(hostname --fqdn)/post-backup ]; then | |
for hook in ${SCRIPT_LOCATION}/hosts/$(hostname --fqdn)/post-backup/* ; do | |
test -f "$hook" && \ | |
. $hook | |
done | |
fi | |
echo "Backup end date is $(date)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nicelevel=9 | |
ionicelevel=3 | |
NUMBACKUP=5 | |
BCKIP="ip/host.of.backup.machine" | |
BCKUSER="backup" | |
BCKDST="rsync://$BCKIP/virtual/path/on/rsync/srv" | |
BCKHOSTDIR="/full/path/of/backup/dir" | |
EXCLUDEDIRS="/tmp /proc /dev /sys /root/.cache /var/cache/duplicity /var/cache/apt/archives/" | |
EXTRAARGS="-za --partial --delete" | |
FAKESUPER=--rsync-path="rsync --fake-super" | |
EXTRAARGS="$EXTRAARGS $FAKESUPER" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment