From bdf4d2380dc57ed1960f70bb5e4216681cae4b91 Mon Sep 17 00:00:00 2001 From: Giovanni Di Grezia Date: Fri, 17 Apr 2020 04:03:41 +0200 Subject: [PATCH] added threshold --- README.md | 5 +++++ src/zfs-auto-snapshot.sh | 29 +++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f79bc7f..e41594d 100644 --- a/README.md +++ b/README.md @@ -26,3 +26,8 @@ make uninstall # NOTE # uninstalling the script doesn't delete made snapshots +in src folder, before installing, you can customize some parameters +--keep indicates the number of snapshots to save. Default to 10 +--label indicates the label of snapshots. Default to defaultLabel +--threshold indicates how many bytes need to be written in a dataset after the last snapshot to allow new snapshots. Default 0 + diff --git a/src/zfs-auto-snapshot.sh b/src/zfs-auto-snapshot.sh index b4ca4ac..0d4104a 100755 --- a/src/zfs-auto-snapshot.sh +++ b/src/zfs-auto-snapshot.sh @@ -1,6 +1,9 @@ #!/bin/sh opt_prefix='zfs-auto-snap' +opt_keep=10 +opt_label="defaultLabel" +opt_threshold=0 do_snapshots () { @@ -8,10 +11,21 @@ do_snapshots () for ii in $TARGETS do - zfs snapshot "$ii@$SNAPNAME" - KEEP="$opt_keep" + if [ "$opt_threshold" -eq 0 ] + then + zfs snapshot "$ii@$SNAPNAME" + else + bytes_written=`zfs get -Hp -o value written $ii` + if [ "$bytes_written" -ge "$opt_threshold" ] + then + zfs snapshot "$ii@$SNAPNAME" + else + KEEP=$(( $KEEP + 1 )) + fi + fi + # ASSERT: The old snapshot list is sorted by increasing age. for jj in $SNAPSHOTS_OLD do @@ -33,12 +47,14 @@ do_snapshots () # main () # { + while [ "$#" -gt '0' ] do case "$1" in --keep) if [ ! "$2" -gt '0' ]; then + echo "keep parameters need to be greater than 0" exit 1 fi opt_keep="$2" @@ -48,6 +64,15 @@ do opt_label="$2" shift 2 ;; + --threshold) + if [ ! "$2" -ge '0' ]; + then + echo "threshold parameters need to be equal or greater than 0" + exit 1 + fi + opt_threshold="$2" + shift 2 + ;; esac done