3 # Snapshot Snapping Scheme
5 # This is a cron bot to manage the snapshots of an nilfs2 file system,
6 # so as to preserve 7 most recent daily, 4 most recent weekly and 12
7 # most recent monthly snapshots, in addition to labelled ones.
9 # The cron bot is expected to run once a minute
11 # The hour-of-day to preserve as daily snapshot
14 # The day-of-month to preserve as monthly snapshot
18 if [ -z "$DEV" ] ; then
19 DEVS=( $( mount | grep nilfs2 | sed 's/ .*//' ) )
21 if [ -z "$DEV" ] ; then
22 echo "Error* cannot find a mounted nilfs2 partition" >&2
27 FIVELY="$(date -d '-5 minutes' '+%Y-%m-%d %H:%M:%S')"
28 HOURLY="$(date -d '-1 hour' '+%Y-%m-%d %H:%M:%S')"
29 DAILY="$(date -d '-1 day' '+%Y-%m-%d %H:%M:%S')"
30 MONTHLY="$(date -d '-1 month' '+%Y-%m-%d %H:%M:%S')"
31 WEEKLY="$(date -d '-7 days' '+%Y-%m-%d %H:%M:%S')"
32 YEARLY="$(date -d '-1 year' '+%Y-%m-%d %H:%M:%S')"
34 function older_than_a_year() {
35 #echo "--- $CNO $DATE $TIME" >&2
36 [[ ! "$TS" < "$YEARLY" ]] && return 1
37 #echo "# keep the first snapshot of older years: $CNO $DATE $TIME" >&2
38 if [ "$KEEPYY" = "${DATE:0:4}" ] ; then
39 echo "$DEV: discard year duplicate $CNO $DATE $TIME" >&2
47 function older_than_a_month() {
48 [[ ! "$TS" < "$MONTHLY" ]] && return 1
49 #echo "# keep the first snapshot of older months" >&2
50 if [ "$KEEPMM" = "${DATE:0:7}" ] ; then
51 echo "$DEV: discard month duplicate $CNO $DATE $TIME" >&2
59 function older_than_a_week() {
60 [[ ! "$TS" < "$WEEKLY" ]] && return 1
61 if [[ "${DATE:8:2}" < "$KEEPDD" ]] ; then
62 #echo "# keep latest when before choice date: $CNO $DATE $TIME" >&2
63 if [ -n "$KEEPWW0" ] ; then
64 echo "$DEV: discard outed $KEEPWW0" >&2
67 KEEPWW0=( $CNO $DATE $TIME )
69 #echo "# keep first when at or after choice date: $CNO $DATE $TIME" >&2
70 if [ -z "$KEEPWW" ] ; then
71 if [ -n "$KEEPWW0" ] ; then
72 echo "$DEV: discard outed $KEEPWW0" >&2
75 KEEPWW=( $CNO $DATE $TIME )
78 echo "$DEV: discard monthly extras: $CNO $DATE $TIME" >&2
85 function older_than_a_day() {
86 [[ ! "$TS" < "$DAILY" ]] && return 1
87 if [[ "${TIME:0:2}" < "$KEEPHH" ]] ; then
88 #echo "# keep latest when before choice hour: $CNO $DATE $TIME" >&2
89 if [ -n "$KEPTHH0" ] ; then
90 echo "$DEV: discard outed $KEPTHH0" >&2
93 KEPTHH0=( $CNO $DATE $TIME )
95 #echo "# keep first when at/after choice hour: $CNO $DATE $TIME" >&2
96 if [ -z "$KEPTHH" ] ; then
97 if [ -n "$KEPTHH0" ] ; then
98 echo "$DEV: discard outed $KEPTHH0" >&2
102 KEPTHH=( $CNO $DATE $TIME )
104 echo "$DEV: discard daily extras: $CNO $DATE $TIME" >&2
110 function older_than_an_hour() {
111 [[ ! "$TS" < "$HOURLY" ]] && return 1
112 #echo "# keep first of each hour: $CNO $DATE $TIME" >&2
113 if [[ "$KEPT55" != "${TIME:0:2}" ]] ; then
116 echo "$DEV: discard hourly duplicate: $CNO $DATE $TIME" >&2
122 function older_than_5_minutes() {
123 if [[ ! "$TS" < "$FIVELY" ]] ; then
124 echo "$CNO $DATE $TIME is within last five minutes"
127 if [[ "$TS" < "$NEXT" ]] ; then
128 echo "$DEV: discard 5-minutely duplicate: $CNO $DATE $TIME" >&2
131 T=${TS:0:15}$(( ( ${TS:15:1} / 5 ) * 5 )):00
132 NEXT="$(date -d "5 minutes $T" '+%Y-%m-%d %H:%M:%S')"
133 #echo "NEXT=$NEXT" >&2
140 date "+$DEV: %Y-%m-%d %H:%M:%S ---- checking" >&2
141 lscp -s $DEV | while read CNO DATE TIME REST ; do
143 [ "$CNO" = "CNO" ] ||
145 older_than_a_month ||
148 older_than_an_hour ||
150 done | if read X ; then
152 elif [[ $(( $(date +%M|sed 's/^0//') % 5 )) = 0 ]] ; then
153 date "+$DEV: new snapshot at %Y-%m-%d %H:%M:%S" >&2
156 } 9> /var/run/lock/sss.lock