From a5056198ab84d911cf8e242a8886855c81326531 Mon Sep 17 00:00:00 2001 From: Julien Lutran Date: Mon, 8 Dec 2025 16:14:46 +0100 Subject: [PATCH] Add zfs-auto-snapshot.sh script --- README.md | 1 + zfs-auto-snapshot.sh | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100755 zfs-auto-snapshot.sh diff --git a/README.md b/README.md index 53ec007..710f3bc 100644 --- a/README.md +++ b/README.md @@ -6,3 +6,4 @@ Some helpful bash scripts for [incus](https://linuxcontainers.org/incus) - incus-backup: Backup instances FS and DBs to a remote location using rsync over ssh. - incus-copy: Run a differential copy of all running instances to a remote incus server. - incus-snapshot: Take a snapshot on all running instances. +- zfs-auto-snapshot: Create and cleanup expired zfs snapshots. diff --git a/zfs-auto-snapshot.sh b/zfs-auto-snapshot.sh new file mode 100755 index 0000000..a63d2f9 --- /dev/null +++ b/zfs-auto-snapshot.sh @@ -0,0 +1,29 @@ +#!/bin/bash +# +# Create zfs snapshots for $DATASET +# https://github.com/bahamas10/zfs-prune-snapshots +# + +#set -x + +while getopts d:e: flag +do + case "${flag}" in + d) DATASET=${OPTARG};; + e) EXPIRATION=${OPTARG};; + esac +done + +DATE=$(date '+%Y-%m-%d %H:%M:%S') + +TS=$(date '+%s') +ZFS=$(which zfs) +CMD="$ZFS snapshot ${DATASET}@snap${TS}" +echo "[${DATE}] $CMD" +$CMD 2>&1 > /dev/null + +ZFS_PRUNE_SNAPSHOTS=$(which zfs-prune-snapshots) +CMD="$ZFS_PRUNE_SNAPSHOTS -p 'snap' $EXPIRATION $DATASET" +echo "[${DATE}] $CMD" +$CMD 2>&1 > /dev/null +