Skip to content

Instantly share code, notes, and snippets.

@dokipen
Created September 20, 2010 18:26
Show Gist options
  • Save dokipen/588378 to your computer and use it in GitHub Desktop.
Save dokipen/588378 to your computer and use it in GitHub Desktop.
#!/bin/sh
# Ideas taken from http://www.mikerubel.org/computers/rsync_snapshots/
#
# This increments all backup numbers and deletes or moves the last one
# to a monthly backup depending on the date.
#
# WARNING: This code breaks badly if you try and backup to dirs with the
# same basename! There is no safety mechanism.
# where backups are stored
BACKUP_BASE="/backup"
# dirs that get backed up
DIRS="/home"
# days to keep backups
BACKUP_COUNT=7
# day of the month that monthly backup is created
MONTHLY_DAY=5
logger -tnightly_oforge_backup starting...
for dir in ${DIRS}; do
logger -tnightly_backup "Creating backups for ${dir}"
base="${BACKUP_BASE}/`basename ${dir}`"
lastsrc="${base}.daily.$(($BACKUP_COUNT-1))"
if [ -a "${lastsrc}" ]; then
if [ `printf %01d ${MONTHLY_DAY}` -eq `date +%d` ]; then
logger -tnightly_backup "Performing monthly snapshot"
# mark monthly backup with date of backup
datepostfix=`date -d "-${BACKUP_COUNT} day" +%y%m%d`
mv "${lastsrc}" "${base}.monthly.${datepostfix}" && \
logger -tnightly_backup "Snapshot complete"
else
logger -tnightly_backup "Deleting oldest backup"
rm -Rf "${base}.daily.6" && \
logger -tnightly_backup "Backup deleted"
fi
fi
# minus 2 here because we already took care of the last one and we start
# with 0
logger -tnightly_backup "Rotating backups"
for i in `echo $(seq 0 $((${BACKUP_COUNT}-2))) | rev`; do
src="${base}.daily.${i}"
if [ -a "${src}" ]; then
logger -tnightly_backup "rotating ${src}"
mv "${src}" "${base}.daily.$((${i}+1))" && \
logger -tnightly_backup "${src} rotated"
fi
done
logger -tnightly_backup "Creating nightly backup ${dir}"
rsync -a --exclude='.snapshot' --delete --link-dest="../`basename ${dir}`.daily.1" "${dir}/" "${base}.daily.0/" && \
logger -tnightly_backup "complete"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment