#!/bin/bash # Author: Carl Baldwin & Alan Pippin # Description: This script takes a snapshot of the given zfs filesystem. # It also employs an intelligent algorithm to roll off, # or destroy, old snapshots. test=0 [ $test == 0 ] && exec >> /var/log/zfs-autosnap.log 2>&1 # This script makes the following assumptions/requirements: # * this script only handles one zfs filesystem, a wrapper should be created # to handle more # * this script handles all snapshots that are named in this format: # YYYY-MM-DD.hh.mm # * It ignores other snapshots that don't follow this naming convention # This converts the YYYY-MM-DD.hh.mm format to an integer. datetime_to_minutes() { perl -n -e '/(\d+)-(\d+)-(\d+)\.(\d+)\.(\d+)/; print $1 * 527040 + $2 * 44640 + $3 * 1440 + $4 * 60 + $5,"\n"' } datetime_to_minutes2() { perl -n -e '/(\d+)-(\d+)-(\d+)\.(\d+)\.(\d+)/; $monthadj=int(($2-1)/3)-1; $minadj=int($5/15); $houradj=int($4/3); print $1 * 1048576 + ( $2 + $monthadj ) * 65536 + $3 * 2048 + ( $4 + $houradj ) * 64 + $5 + $minadj,"\n"' } # test: if set to 1, no zfs snapshot commands will be run, they will only be echoed # filesystem: This is the zfs filesystem to snapshot # mountpoint: This is the mountpoint of the zfs filesystem to snapshot # numsnapshots: This number is the number of equally spaced snapshots that should exist over any given period in the past # maxagedays: This is the maximum number of days to keep any snapshot around for (0=infinite) (default=0). filesystem=$1 mountpoint=$2 numsnapshots=${3-20} maxagedays=${4-0} lockdir="/tmp/zfs-admin-lock" pool=`echo "$filesystem" | awk -F '/' '{ print $1 }'` if [ -z "$filesystem" ] || [ -z "$mountpoint" ] || [ -z "$numsnapshots" ] || [ -z "$maxagedays" ]; then echo "-E- Usage: $0 " exit 1 fi if [ ! -d "$mountpoint" ]; then echo "-E- Unable to find the mountpoint: $mountpoint" exit 1 fi snapshotdir="${mountpoint}/.zfs/snapshot" if [ ! -d "$snapshotdir" ]; then echo "-E- Unable to find the snapshotdir: $snapshotdir" exit 1 fi # Check to see if this zfs pool has a scrub being performed on it now. # If it does, we cannot perform any snapshot create or destroy operations. zpool status $pool | grep scrub: | grep "in progress" > /dev/null 2>&1 if [ $? == 0 ]; then echo "-W- The zfs pool '$pool' is currently being scrubbed. Skipping all snapshot operations." exit 0 fi # Get the various components of the date datetime=${ZFSDATETIME:-$(date +%Y-%m-%d.%H.%M)} # Create the snapshot for this minute echo "-I- Creating ${filesystem}@${datetime}" [ $test == 0 ] && zfs snapshot "${filesystem}@${datetime}" minutes=$(echo $datetime | datetime_to_minutes) # Check to ensure the zfs filesystem has not been locked down. # If it has, we cannot perform any snapshot destroy operations. if [ -d "$lockdir" ]; then echo "-W- The zfs filesystem has been locked down. Skipping snapshot cleanup." exit 0 fi # Trim them down snapshots=$(ls -d ${snapshotdir}/????-??-??.??.?? 2>/dev/null) for snapshot in $snapshots; do snapminutes=$(echo "$snapshot" | sed 's,.*/,,' | datetime_to_minutes) snapminutes2=$(echo "$snapshot" | sed 's,.*/,,' | datetime_to_minutes2) age=$((minutes - snapminutes)) window=1 while true; do if [ $age -lt $((window * numsnapshots)) ]; then case $((snapminutes2 % window)) in 0) ;; *) snapname=${filesystem}$(echo "$snapshot" | sed 's,/\(.*\)/.zfs/snapshot/\(.*\),@\2,') echo "-I- Destroying $snapname" [ $test == 0 ] && zfs destroy "$snapname" ;; esac break fi window=$((window*2)) done if [ $maxagedays -gt 0 ] && [ $age -gt $((maxagedays * 24 * 60)) ]; then snapname=${filesystem}$(echo "$snapshot" | sed 's,/\(.*\)/.zfs/snapshot/\(.*\),@\2,') echo "-I- Destroying old $snapname" [ $test == 0 ] && zfs destroy "$snapname" fi done true