etc/periodic: fix off-by-one error in 220.snapshot-hammer2
[dragonfly.git] / etc / periodic / daily / 220.snapshot-hammer2
1 #!/bin/sh
2 #
3 # create snapshots of hammer2 directories
4
5 if [ -r /etc/defaults/periodic.conf ]
6 then
7     . /etc/defaults/periodic.conf
8     source_periodic_confs
9 fi
10
11 case "${PERIODIC}" in
12         weekly)
13         snap_dirs=${weekly_snapshot_hammer2_dirs:-"auto"}
14         snap_capacity=${weekly_snapshot_hammer2_capacity:-"90"}
15         snap_keep=${weekly_snapshot_hammer2_keep:-"auto"}
16         snap_keep_auto=6
17         snap_tag=${weekly_snapshot_hammer2_tag:-"weekly"}
18         snap_enable="${weekly_snapshot_hammer2_enable}"
19         ;;
20         monthly)
21         snap_dirs=${monthly_snapshot_hammer2_dirs:-"auto"}
22         snap_capacity=${monthly_snapshot_hammer2_capacity:-"90"}
23         snap_keep=${monthly_snapshot_hammer2_keep:-"auto"}
24         snap_keep_auto=12
25         snap_tag=${monthly_snapshot_hammer2_tag:-"monthly"}
26         snap_enable="${monthly_snapshot_hammer2_enable}"
27         ;;
28         *)
29         snap_dirs=${daily_snapshot_hammer2_dirs:-"auto"}
30         snap_capacity=${daily_snapshot_hammer2_capacity:-"90"}
31         snap_keep=${daily_snapshot_hammer2_keep:-"auto"}
32         snap_keep_auto=15
33         snap_tag=${daily_snapshot_hammer2_tag:-"daily"}
34         snap_enable="${daily_snapshot_hammer2_enable}"
35         ;;
36 esac
37
38 rc=0
39
40 case "${snap_enable}" in
41         [Yy][Ee][Ss])
42         echo ""
43         echo "HAMMER2 snapshots:"
44
45         if [ "${snap_dirs}" = "auto" ]; then
46                 snap_dirs="$(mount -v -t hammer2 | awk '{ print $3; }')"
47         fi
48
49         for dir in ${snap_dirs}; do
50                 echo -n "${dir}: "
51                 if [ ! -d "${dir}" ]; then
52                         echo "omitted, not a directory"
53                         continue
54                 fi
55                 _df=$(df ${dir} | grep -oE "[0-9]+%")
56                 _pcnt=${_df%%%}
57                 if [ ${_pcnt} -gt ${snap_capacity} ]; then
58                         echo "omitted, not enough free space"
59                         continue
60                 fi
61                 hammer2 snapshot ${dir} ${dir}.${snap_tag}.$(date +%Y%m%d.%H:%M) \
62                         || rc=1
63                 _keep=${snap_keep}
64                 if [ "${_keep}" = "auto" ]; then
65                         # adjust number of snapshots dynamically, according to free space
66                         _keep=$(echo "scale=2;inc=((${snap_capacity}-${_pcnt})/50* \
67                                 ${snap_keep_auto});scale=0;${snap_keep_auto}+inc/1" | bc)
68                 fi
69                 _keep=$((_keep+1))
70                 for _stale in $(hammer2 -s ${dir} pfs-list | grep -v "^Type" | \
71                                 grep -oE "${dir}\.${snap_tag}\..*$" | \
72                                 sort -r | tail -n+${_keep}); do
73                         hammer2 -s ${dir} pfs-delete ${_stale} || rc=1
74                 done
75                 echo "${dir}: $(hammer2 -s ${dir} pfs-list | grep -v "^Type" | \
76                                 grep -coE "${dir}\.${snap_tag}\..*$") snapshot(s)"
77         done
78         ;;
79 esac
80
81 exit $rc