initial setup
authorRalph Ronnquist <ralph.ronnquist@gmail.com>
Fri, 4 Dec 2020 23:06:21 +0000 (10:06 +1100)
committerRalph Ronnquist <ralph.ronnquist@gmail.com>
Fri, 4 Dec 2020 23:06:21 +0000 (10:06 +1100)
sss.sh [new file with mode: 0755]

diff --git a/sss.sh b/sss.sh
new file mode 100755 (executable)
index 0000000..d1c581c
--- /dev/null
+++ b/sss.sh
@@ -0,0 +1,92 @@
+#!/bin/bash
+#
+# Snapshot Snapping Scheme
+#
+# This is a cron bot to manage the snapshots of an nilfs2 file system,
+# so as to preserve 7 most recent daily, 4 most recent weekly and 12
+# most recent monthly snapshots, in addition to labelled ones.
+#
+# The cron bot is expected to run once an hour
+
+# hourly = every hour
+# daily = .. keep one of the hourly yesterday
+# weekly = .. keep one of the daily last week
+# monthly = .. keep one of the daily last month
+
+DEV=/dev/sda5
+KEEPHH=12
+KEEPDD=15
+
+FIVELY="$(date -d '-5 minutes' '+%Y-%m-%d %H:%M:%S')"
+HOURLY="$(date -d '-1 hour' '+%Y-%m-%d %H:%M:%S')"
+DAILY="$(date -d '-1 days' '+%Y-%m-%d %H:%M:%S')"
+MONTHLY="$(date -d '-1 months' '+%Y-%m-%d %H:%M:%S')"
+WEEKLY="$(date -d '-7 days' '+%Y-%m-%d %H:%M:%S')"
+YEARLY="$(date -d '-1 years' '+%Y-%m-%d %H:%M:%S')"
+
+date "+%Y-%m-%d %H:%M:%S ---- checking" >&2
+lscp -s | while read CNO DATE TIME REST ; do
+    TS="$DATE $TIME"
+    [ "$CNO" = "CNO" ] && continue
+    if [[ "$TS" < "$YEARLY" ]] ; then
+       if [ "$KEEPYY" = "${DATE:0:4}" ] ; then
+           echo "# discard year duplicate $CNO $DATE $TIME" >&2
+           chcp cp $CNO
+       else
+           KEEPYY="${DATE:0:4}"
+       fi
+       echo "# keep first of year $CNO $DATE $TIME" >&2
+       continue
+    fi
+    # Within last year
+    if [[ "$TS" < "$MONTHLY" ]] ; then
+       if [ "$KEEPMM" = "${DATE:0:7}" ] ; then
+           echo "# discard month duplicate $CNO $DATE $TIME" >&2
+           chcp cp $CNO
+       else
+           KEEPMM="${DATE:0:7}"
+       fi
+    fi
+    # Within last month
+    if [[ "$TS" < "$WEEKLY" ]] ; then
+       if [[ "$KEEPDD" = "${DATE:8:2}" ]] ; then
+           if [ -z "$KEEPWW" ] ; then
+               KEEPWW="${DATE:8:2}"
+               continue
+           fi
+           echo "# discard duplicate day $KEEPDD: $CNO $DATE $TIME" >&2
+       else 
+           echo "# discard not day $KEEPDD: $CNO $DATE $TIME" >&2
+       fi
+       chcp cp $CNO
+       continue
+    fi
+    # Within last week
+    if [[ "$TS" < "$DAILY" ]] ; then
+       [[ "$KEEPHH" = "${TIME:0:2}" ]] && continue
+       echo "# discard not hour $KEEPHH: $CNO $DATE $TIME" >&2
+       chcp cp $CNO
+       continue
+    fi
+    # Within last day
+    if [[ "$TS" < "$HOURLY" ]] ; then
+       if [ "$KEEP55" = "${TIME:0:2}" ] ; then
+           echo "# discard duplicate for hour $KEEP55: $CNO $DATE $TIME" >&2
+           chcp cp $CNO
+       else
+           KEEP55="${TIME:0:2}"
+           #echo "# keep first of hour $KEEP55: $CNO $DATE $TIME" >&2
+       fi
+       continue
+    fi
+    # Within last hour
+    #echo "# keep within last hour $CNO $DATE $TIME" >&2
+    [[ "$TS" < "$FIVELY" ]] && continue
+    echo "$CNO $DATE $TIME is within last five minutes"
+    #echo "# $CNO is within last five minutes " >&2
+done | if read X ; then
+    :
+else
+    date "+# new snapshot at %Y-%m-%d %H:%M:%S" >&2
+    mkcp -s $DEV
+fi