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 snapshotdir="./snapshot"
71 snapshotdir="${mountpoint}/.zfs/snapshot"
74 if [ ! -d "$snapshotdir" ]; then
75 echo "-E- Unable to find the snapshotdir: $snapshotdir"
79 # Check to see if this zfs filesystem has a scrub being performed on it now.
80 # If it does, we cannot perform any snapshot create or destroy operations.
81 if [ -z "$SNAP_UNDER_TEST" ]; then
82 zpool status $pool | grep scan: | grep "in progress" > /dev/null 2>&1
84 echo "-W- The zfs pool '$pool' is currently being scrubbed. Skipping all snapshot operations."
90 echo "-I- Creating $1"
91 if [ -z "$SNAP_UNDER_TEST" ]; then
94 mkdir -p snapshot/$(dirname "$(echo "$1" | sed 's,.*@,,')")
95 touch snapshot/"$(echo "$1" | sed 's,.*@,,')"
100 echo "-I- Destroying old $1"
101 if [ -z "$SNAP_UNDER_TEST" ]; then
108 # Get the various components of the date
109 datetime=${ZFSDATETIME:-$(date +%Y-%m-%d.%H.%M)}
111 # Create the snapshot for this minute
112 snapshot "${filesystem}@${datetime}"
114 minutes=$(echo $datetime | datetime_to_minutes)
116 if ! mkdir "$lockdir" >/dev/null 2>&1; then
117 echo "-W- The zfs filesystem has been locked down. Skipping snapshot cleanup."
120 cleanup() { rm -rf "$lockdir"; }
124 snapshots=$(ls -d ${snapshotdir}/????-??-??.??.?? 2>/dev/null)
125 for snapshot in $snapshots; do
126 snapminutes=$(echo "$snapshot" | sed 's,.*/,,' | datetime_to_minutes)
127 snapminutes2=$(echo "$snapshot" | sed 's,.*/,,' | datetime_to_minutes2)
128 age=$((minutes - snapminutes))
131 if [ $age -lt $((window * numsnapshots)) ]; then
132 case $((snapminutes2 % window)) in
135 snapname=${filesystem}$(echo "$snapshot" |
136 sed 's,/\(.*\)/.zfs/snapshot/\(.*\),@\2,')
144 if [ $maxagedays -gt 0 ] && [ $age -gt $((maxagedays * 24 * 60)) ]; then
145 snapname=${filesystem}$(echo "$snapshot" |
146 sed 's,/\(.*\)/.zfs/snapshot/\(.*\),@\2,')