3 # Author: Carl Baldwin & Alan Pippin
4 # Description: This script takes a snapshot of the given zfs filesystem.
5 # It also employs an intelligent algorithm to roll off,
6 # or destroy, old snapshots.
8 # source our configuration
9 config="${0%/*}/zfs-scripts.conf"
10 [ -e "${config}.dist" ] && . ${config}.dist
11 [ -e "${config}" ] && . ${config}
13 if [[ -z "$SNAP_UNDER_TEST" ]]; then
14 exec >> $logdir/zfs-autosnap.log 2>&1
17 # This script makes the following assumptions/requirements:
18 # * this script only handles one zfs filesystem, a wrapper should be created
20 # * this script handles all snapshots that are named in this format:
22 # * It ignores other snapshots that don't follow this naming convention
24 # This converts the YYYY-MM-DD.hh.mm format to an integer.
25 datetime_to_minutes() {
26 perl -n -e '/(\d+)-(\d+)-(\d+)\.(\d+)\.(\d+)/; print $1 * 527040 + $2 * 44640 + $3 * 1440 + $4 * 60 + $5,"\n"'
29 # This converts date/time from YYYY-MM-DD.hh.mm to an integer that aligns
30 # things to prefer certain times such as the first of the month or midnight,
32 datetime_to_minutes2() {
33 perl -n -e '/(\d+)-(\d+)-(\d+)\.(\d+)\.(\d+)/;
34 $monthadj=int(($2-1)/3)-1; # Prefer months numbered 1,4,7 and 10
35 $dayadj=$3 == 1 ? -1 : 0; # Make sure day 1 is prefered
36 $minadj=int($5/15); # Prefer multiples of 15 minutes
37 $houradj=int($4/3); # Prefer midnight,noon,etc
40 + 65536 * ( $2 + $monthadj )
41 + 2048 * ( $3 + $dayadj )
42 + 64 * ( $4 + $houradj )
48 # filesystem: This is the zfs filesystem to snapshot
49 # mountpoint: This is the mountpoint of the zfs filesystem to snapshot
50 # numsnapshots: This number is the number of equally spaced snapshots that should exist over any given period in the past
51 # maxagedays: This is the maximum number of days to keep any snapshot around for (0=infinite) (default=0).
56 pool=`echo "$filesystem" | awk -F '/' '{ print $1 }'`
58 if [ -z "$filesystem" ] || [ -z "$mountpoint" ] || [ -z "$numsnapshots" ] || [ -z "$maxagedays" ]; then
59 echo "-E- Usage: $0 <filesystem> <mountpoint> <numsnapshots> <maxagedays>"
63 if [ -z "$SNAP_UNDER_TEST" -a ! -d "$mountpoint" ]; then
64 echo "-E- Unable to find the mountpoint: $mountpoint"
68 if [ -n "$SNAP_UNDER_TEST" ]; then
69 snapshots=$(ls -d ./snapshot/????-??-??.??.?? 2>/dev/null)
71 snapshots=$(zfs list -H -t snapshot -o name $filesystem | grep -P "\d+-\d+\.\d+\.\d+")
74 # Check to see if this zfs filesystem has a scrub being performed on it now.
75 # If it does, we cannot perform any snapshot create or destroy operations.
76 if [ -z "$SNAP_UNDER_TEST" ]; then
77 zpool status $pool | grep scan: | grep "in progress" > /dev/null 2>&1
79 echo "-W- The zfs pool '$pool' is currently being scrubbed. Skipping all snapshot operations."
85 echo "-I- Creating $1"
86 if [ -z "$SNAP_UNDER_TEST" ]; then
89 mkdir -p snapshot/$(dirname "$(echo "$1" | sed 's,.*@,,')")
90 touch snapshot/"$(echo "$1" | sed 's,.*@,,')"
95 echo "-I- Destroying old $1"
96 if [ -z "$SNAP_UNDER_TEST" ]; then
103 # Get the various components of the date
104 datetime=${ZFSDATETIME:-$(date +%Y-%m-%d.%H.%M)}
106 # Create the snapshot for this minute
107 snapshot "${filesystem}@${datetime}"
109 minutes=$(echo $datetime | datetime_to_minutes)
111 if ! mkdir "$lockdir" >/dev/null 2>&1; then
112 echo "-W- The zfs filesystem has been locked down. Skipping snapshot cleanup."
115 cleanup() { rm -rf "$lockdir"; }
119 for snapshot in $snapshots; do
120 snapminutes=$(echo "$snapshot" | sed 's,.*/,,' | datetime_to_minutes)
121 snapminutes2=$(echo "$snapshot" | sed 's,.*/,,' | datetime_to_minutes2)
122 age=$((minutes - snapminutes))
125 if [ $age -lt $((window * numsnapshots)) ]; then
126 case $((snapminutes2 % window)) in
128 *) destroy "$snapshot" ;;
134 if [ $maxagedays -gt 0 ] && [ $age -gt $((maxagedays * 24 * 60)) ]; then