#!/bin/sh

################################################################################
# Variables
################################################################################

# Configuration
#
# KEEP_DAYS : number of hourly snapshots to keep
# KEEP_DAYS : number of daily snapshots to keep
# KEEP_DAYS : number of weekly snapshots to keep
KEEP_HOURS=25
KEEP_DAYS=8
KEEP_WEEKS=5
KEEP_MONTHS=13

# Datasets to backup (recursively)
DATASETS="
tank/home/users
tank/jails
tank/local
tank/log
tank/logs
tank/mails
tank/obj
"

# Common vars
SCRIPTNAME=$0

################################################################################
# Functions
################################################################################

# usage functions
usage() {
cat << EOF
usage: $SCRIPTNAME command [options]
 command could be one of : backup clean help
EOF
exit 1
}

usage_backup() {
cat << EOF
usage: $SCRIPTNAME backup basis
 basis could be one of : hourly daily weekly monthly
EOF
exit 1
}

# backup function
backup() {
case $BASIS in
        hourly)
                for DATASET in $DATASETS
                do
                        /sbin/zfs snapshot -r $DATASET@AutoH`date '+%Y%m%d_%H:%M'`
                done
        ;;
        daily)
                for DATASET in $DATASETS
                do
                        /sbin/zfs snapshot -r $DATASET@AutoD`date '+%Y%m%d'`
                done
        ;;
        weekly)
                for DATASET in $DATASETS
                do
                        /sbin/zfs snapshot -r $DATASET@AutoW`date '+%Y%m%d'`
                done
        ;;
        monthly)
                for DATASET in $DATASETS
                do
                        /sbin/zfs snapshot -r $DATASET@AutoM`date '+%Y%m'`
                done
        ;;
        *)
                echo "$BASIS : Not supported" && usage_backup
        ;;
esac
}

# cleaning function
# removes all expired snapshots according to KEEP_* variables
clean() {
for BASIS in HOURLY DAILY WEEKLY MONTHLY
do
case $BASIS in
        HOURLY)
                /sbin/zfs list -t snapshot\
                | grep AutoH\
                | grep `date -v-${KEEP_HOURS}H '+%Y%m%d_%H:%M'`\
                | awk '{print $1}'\
                | while read OLDFS; do /sbin/zfs destroy $OLDFS ;done
        ;;
        DAILY)
                /sbin/zfs list -t snapshot\
                | grep AutoD\
                | grep `date -v-${KEEP_DAYS}d '+%Y%m%d'`\
                | awk '{print $1}'\
                | while read OLDFS; do /sbin/zfs destroy $OLDFS ;done
        ;;
        WEEKLY)
                /sbin/zfs list -t snapshot\
                | grep AutoW\
                | grep `date -v-${KEEP_WEEKS}w '+%Y%m%d'`\
                | awk '{print $1}'\
                | while read OLDFS; do /sbin/zfs destroy $OLDFS ;done
        ;;
        MONTHLY)
                /sbin/zfs list -t snapshot\
                | grep AutoM\
                | grep `date -v-${KEEP_MONTHS}m '+%Y%m'`\
                | awk '{print $1}'\
                | while read OLDFS; do /sbin/zfs destroy $OLDFS ;done
        ;;
esac
done
}

case $1 in
        backup)
                BASIS=$2
                [ -z "$BASIS" ] && usage_backup
                backup
        ;;
        clean)  clean;;
        *)
                usage
        ;;
esac

