Last active
August 4, 2019 00:17
-
-
Save yacoob/5b2d7dc5c98f682e0af7 to your computer and use it in GitHub Desktop.
quick & tasty zfs snapshots
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/zsh | |
| set -eu | |
| LAST_DAYS=30 | |
| LAST_MONDAYS=26 | |
| VOLS=(lcl/geofront lilith/sys magi/sys) | |
| for vol (${VOLS}); do | |
| # Make a daily snapshot. | |
| /sbin/zfs snapshot -r -o nerv.local:timestamp=$(date '+%s') ${vol}@$(date '+%Y-%m-%d') | |
| # Keep last LAST_DAYS days worth of snapshots. | |
| xda=$(/bin/date -d "${LAST_DAYS} days ago" +%s) | |
| snaps=($(/sbin/zfs list -rHp -d1 -t snapshot -o nerv.local:timestamp,name -S nerv.local:timestamp ${vol} | grep -v '^-' | awk "\$1 < ${xda} { print \$2 }")) | |
| # Keep last LAST_MONDAYS Mondays. | |
| for i in $(/usr/bin/seq 1 ${LAST_MONDAYS}); do | |
| monday=$(/bin/date -d "Monday ${i} weeks ago" +%Y-%m-%d) | |
| snaps=(${snaps:#*@${monday}}) | |
| done | |
| # Delete unnecessary snapshots. | |
| for s in $snaps; do | |
| case ${s} in | |
| ${vol}@20??-??-??) | |
| /sbin/zfs destroy -v -r ${s} | |
| ;; | |
| *) | |
| continue | |
| ;; | |
| esac | |
| done | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment