Fix mountpoint after full fs transfer if needed
[zfs-ubuntu/.git] / zfs-autosnap
1 #!/bin/bash
2
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.
7
8 # source our configuration
9 config="${0%/*}/zfs-scripts.conf"
10 [ -e "${config}.dist" ] && . ${config}.dist
11 [ -e "${config}" ] && . ${config}
12
13 if [[ -z "$SNAP_UNDER_TEST" ]]; then
14     exec >> $logdir/zfs-autosnap.log 2>&1
15 fi
16
17 # This script makes the following assumptions/requirements:
18 #  * this script only handles one zfs filesystem, a wrapper should be created
19 #    to handle more
20 #  * this script handles all snapshots that are named in this format:
21 #    YYYY-MM-DD.hh.mm
22 #  * It ignores other snapshots that don't follow this naming convention
23
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"'
27 }
28
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,
31 # etc
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
38               $intvalue=(
39                 1048576 *   $1
40               + 65536   * ( $2 + $monthadj )
41               + 2048    * ( $3 + $dayadj )
42               + 64      * ( $4 + $houradj )
43               +             $5 + $minadj
44               );
45               print $intvalue,"\n"'
46 }
47
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).
52 filesystem=$1
53 mountpoint=${2-/$1}
54 numsnapshots=${3-12}
55 maxagedays=${4-0}
56 pool=`echo "$filesystem" | awk -F '/' '{ print $1 }'`
57
58 if [ -z "$filesystem" ] || [ -z "$mountpoint" ] || [ -z "$numsnapshots" ] || [ -z "$maxagedays" ]; then
59    echo "-E- Usage: $0 <filesystem> <mountpoint> <numsnapshots> <maxagedays>"
60    exit 1
61 fi
62
63 if [ -z "$SNAP_UNDER_TEST" -a ! -d "$mountpoint" ]; then
64    echo "-E- Unable to find the mountpoint: $mountpoint"
65    exit 1
66 fi
67
68 if [ -n "$SNAP_UNDER_TEST" ]; then
69     snapshots=$(ls -d ./snapshot/????-??-??.??.?? 2>/dev/null)
70 else
71     snapshots=$(zfs list -H -t snapshot -o name $filesystem | grep -P "\d+-\d+\.\d+\.\d+")
72 fi
73
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
78     if [ $? == 0 ]; then
79        echo "-W- The zfs pool '$pool' is currently being scrubbed. Skipping all snapshot operations."
80        exit 0
81     fi
82 fi
83
84 snapshot() {
85     echo "-I- Creating $1"
86     if [ -z "$SNAP_UNDER_TEST" ]; then
87         zfs snapshot "$1"
88     else
89         mkdir -p snapshot/$(dirname "$(echo "$1" | sed 's,.*@,,')")
90         touch snapshot/"$(echo "$1" | sed 's,.*@,,')"
91     fi
92 }
93
94 destroy() {
95     echo "-I- Destroying old $1"
96     if [ -z "$SNAP_UNDER_TEST" ]; then
97         zfs destroy "$1"
98     else
99         rm -f "$1"
100     fi
101 }
102
103 # Get the various components of the date
104 datetime=${ZFSDATETIME:-$(date +%Y-%m-%d.%H.%M)}
105
106 # Create the snapshot for this minute
107 snapshot "${filesystem}@${datetime}"
108
109 minutes=$(echo $datetime | datetime_to_minutes)
110
111 if ! mkdir "$lockdir" >/dev/null 2>&1; then
112   echo "-W- The zfs filesystem has been locked down. Skipping snapshot cleanup."
113   exit 0
114 fi
115 cleanup() { rm -rf "$lockdir"; }
116 trap cleanup EXIT
117
118 # Trim them down
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))
123   window=1
124   while true; do
125     if [ $age -lt $((window * numsnapshots)) ]; then
126       case $((snapminutes2 % window)) in
127         0) ;;
128         *) destroy "$snapshot" ;;
129       esac
130       break
131     fi
132     window=$((window*2))
133   done
134   if [ $maxagedays -gt 0 ] && [ $age -gt $((maxagedays * 24 * 60)) ]; then
135     destroy "$snapshot"
136   fi
137 done