#!/bin/bash
# simple backup script. intended to run daily from crontab
# called "biedacula" after "bieda", which is Polish word for "poor".
# implements poor man's GFS scheme, hence the name:)
# requires GNU tar, GNU gzip and ncftp
# these files must contain file/dir paths (one a line)
PATHFILE=/etc/backup-defs
SKIPFILE=/etc/backup-excludes
# this is a snapshot file auto-created by GNU tar
SNAPSHOT=/etc/backup-snapshot
# FTP host to send backups ( must allow anonymous RW access for me )
FTPHOST=192.168.1.118
FTPPORT=21
# when to do full, monthly/weekly backups
FULL_MONTHDAY=1 # 1st day of month
FULL_WEEKDAY=7 # Sunday
# how many "tapes" for monthly backups
KEEP_MONTHLY=3
# how many "tapes" for weekly backups
KEEP_WEEKLY=4
# in total you will have ( KEEP_MONTHLY + KEEP_WEEKLY + 6 ) "tapes"
function biedump {
local TYPE=$1
local LABEL=$2
local start=`date +%Y%m%d%H%M`
echo "$start: Starting $TYPE dump to label $LABEL"
if [ "x$TYPE" == "xfull" ] ; then
rm -rf $SNAPSHOT
fi
tar -c -T$PATHFILE -X$SKIPFILE -g$SNAPSHOT -P -f - \
| gzip \
| ncftpput -c -S.tmp -P $FTPPORT $FTPHOST $LABEL
local res=$?
local end=`date +%Y%m%d%H%M`
if [ $res -eq 0 ]; then
echo "$end: $TYPE dump OK."
else
echo "$end: $TYPE dump FAILED with exit code $res."
fi
}
host=`hostname -f`
yyyy=`date +%Y`
mm=`date +%m`
dd=`date +%d`
ww=`date +%V`
day_of_week=`date +%u`
dayofweek=`date +%a`
echo "Hello. This is biedacula backup running at $host."
echo "Today is $yyyy/$mm/$dd, day $day_of_week ($dayofweek) of week $ww."
if [ $dd -eq $FULL_MONTHDAY ]; then
let " n = ( mm % $KEEP_MONTHLY ) + 1 "
biedump 'full' "$host-M-$n.tgz"
elif [ $day_of_week -eq $FULL_WEEKDAY ] ; then
let " n = ( ww % $KEEP_WEEKLY ) + 1 "
biedump 'full' "$host-W-$n.tgz"
else
biedump 'incr' "$host-D-$dayofweek.tgz"
fi