Search This Blog

Monday, October 4, 2010

Tarsnap cleanup on FreeBSD

The simple script to preform tarsnap cleanup on FreeBSD:


#!/bin/sh

keyfile="/path/to/rw_keyfile"

# man(1) date will help (keep files for last 7 days in this example)
# we prefer to keep files in this format 2009-10-18+06:15-database as mentioned in earlier post
rd=`date -v-7d -v0H -v0M -v0S`
pd=`date -j -f "%a %b %d %T %Z %Y" "$rd" "+%s"`

for file in `tarsnap --keyfile $keyfile --list-archives | sort`; do
fd=`echo -n $file | cut -d'+' -f 1`
fd=`date -j -f "%Y-%m-%d %T" "$fd 00:00:00" "+%s"`
if [ $fd -lt $pd ]; then
echo Deleting the $file archive
tarsnap --keyfile $keyfile -d -f $file
fi
done

Script to perform tarsnapping of data on FreeBSD

Here is the script to perform tarsnapping on FreeBSD:


#!/bin/sh

# Directories to backup
DIRS=/usr/local/etc/tarsnap-backup.conf

#EXTRA_FLAGS=--dry-run

BACKUP=`date "+%Y-%m-%d+%H:%M"`
TARSNAP="/usr/local/bin/tarsnap"

# Do backups
IFS='
'
for dirs in `grep -v -e '^#.' $DIRS`; do
# Needs to set IFS to space to allow passing arguments to tarsnap
IFS=' '
echo ${dirs}
$TARSNAP $EXTRA_FLAGS -c -f $BACKUP-${dirs}
done


The conf file should look like:
archive_name1 /list /of /directories1
archive_name2 /list /of /directories2
misc /root /etc /boot/loader.conf /usr/local/etc /usr/local/opt /var/cron/tabs /var/services /var/mail /var/qmail/control /var/qmail/alias