Skip to content

Instantly share code, notes, and snippets.

@lastcanal
Last active August 31, 2024 19:43
Show Gist options
  • Select an option

  • Save lastcanal/63e5c4d3e6ea771f5009 to your computer and use it in GitHub Desktop.

Select an option

Save lastcanal/63e5c4d3e6ea771f5009 to your computer and use it in GitHub Desktop.
FreeBSD Tarsnap Hourly Backup
#!/bin/sh
# Tarsnap backup script
# Written by Tim Bishop, 2009.
# Directories to backup
DIRS="/etc /usr/local/etc /home"
# Number of hourly backups to keep
HOURLY=24
# Number of daily backups to keep
DAILY=7
# Which hour to do daily backups on
# 01-24
DAILY_HOUR=14
# Number of weekly backups to keep
WEEKLY=4
# Which day to do weekly backups on
# 1-7, Monday = 1
WEEKLY_DAY=1
# Number of monthly backups to keep
MONTHLY=24
# Which day to do monthly backups on
# 01-31 (leading 0 is important)
MONTHLY_DAY=01
# Directory for writing logfiles
LOGDIR=/var/log/spiped
# Path to tarsnap
#TARSNAP="/home/tdb/tarsnap/tarsnap.pl"
TARSNAP="/usr/local/bin/tarsnap"
# end of config
# hour of day: 01-24
HOD=`date +%H`
# day of week: 1-7, monday = 1
DOW=`date +%u`
# day of month: 01-31
DOM=`date +%d`
# month of year: 01-12
MOY=`date +%m`
# year
YEAR=`date +%Y`
# time
TIME=`date +%H%M%S`
# Backup timeframe
if [ X"$HOD" = X"$DAILY_HOUR" ]; then
# daily backup
TIMEFRAME="daily"
elif [ X"$DOM" = X"$MONTHLY_DAY" ]; then
# monthly backup
TIMEFRAME="monthly"
elif [ X"$DOW" = X"$WEEKLY_DAY" ]; then
# weekly backup
TIMEFRAME="weekly"
else
# hourly backup
TIMEFRAME="hourly"
fi
LOGFILE=$LOGDIR/$TIMEFRAME.log
BACKUP="$YEAR$MOY$DOM-$TIME-$TIMEFRAME"
# Do backups
for dir in $DIRS; do
# nasty bodge for my large /home :-)
EXTRA_FLAGS=
if [ X"$dir" = X"/home" ]; then
EXTRA_FLAGS="--lowmem"
fi
echo "==> create $BACKUP-$dir" >> $LOGFILE 2>&1
$TARSNAP $EXTRA_FLAGS -c -f $BACKUP-$dir $dir >> $LOGFILE 2>&1
done
# Backups done, time for cleaning up old archives
# using tail to find archives to delete, but its
# +n syntax is out by one from what we want to do
# (also +0 == +1, so we're safe :-)
DAILY=`expr $DAILY + 1`
WEEKLY=`expr $WEEKLY + 1`
MONTHLY=`expr $MONTHLY + 1`
# Do deletes
TMPFILE=/tmp/tarsnap.archives.$$
$TARSNAP --list-archives > $TMPFILE
DELARCHIVES=""
for dir in $DIRS; do
for i in `grep -E "^[[:digit:]]{8}-[[:digit:]]{6}-daily-$dir$" $TMPFILE | sort -rn | tail -n +$DAILY`; do
echo "==> delete $i" >> $LOGFILE 2>&1
DELARCHIVES="$DELARCHIVES -f $i"
done
for i in `grep -E "^[[:digit:]]{8}-[[:digit:]]{6}-weekly-$dir$" $TMPFILE | sort -rn | tail -n +$WEEKLY`; do
echo "==> delete $i" >> $LOGFILE 2>&1
DELARCHIVES="$DELARCHIVES -f $i"
done
for i in `grep -E "^[[:digit:]]{8}-[[:digit:]]{6}-monthly-$dir$" $TMPFILE | sort -rn | tail -n +$MONTHLY`; do
echo "==> delete $i" >> $LOGFILE 2>&1
DELARCHIVES="$DELARCHIVES -f $i"
done
done
if [ X"$DELARCHIVES" != X ]; then
echo "==> delete $DELARCHIVES" >> $LOGFILE 2>&1
$TARSNAP -d $DELARCHIVES >> $LOGFILE 2>&1
fi
rm $TMPFILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment