summaryrefslogtreecommitdiff
path: root/snap.sh
diff options
context:
space:
mode:
Diffstat (limited to 'snap.sh')
-rwxr-xr-xsnap.sh45
1 files changed, 45 insertions, 0 deletions
diff --git a/snap.sh b/snap.sh
new file mode 100755
index 0000000..36f32e6
--- /dev/null
+++ b/snap.sh
@@ -0,0 +1,45 @@
1#!/bin/sh -euf
2
3MOUNT_DIRECTORY="/var/backups/dataset-snapshots"
4SNAPSHOT_LABEL="snap"
5
6usage() (
7 echo "snap.sh <snap|free> <dataset>"
8 echo
9 echo "Creates or destroys a snapshot of a ZFS dataset, labelled with @$SNAPSHOT_LABEL."
10 echo "Snapshots are automatically mounted in $MOUNT_DIRECTORY."
11 echo "If a snapshot already exists, it is replaced."
12 echo
13)
14
15create_snapshot() (
16 DATASET="$1"
17
18 SNAPSHOT_NAME="$DATASET@$SNAPSHOT_LABEL"
19 SNAPSHOT_DIRECTORY="$MOUNT_DIRECTORY/$DATASET"
20
21 if zfs list "$SNAPSHOT_NAME" >/dev/null 2>/dev/null; then
22 echo "Snapshot already exists. Overwriting."
23 destroy_snapshot "$DATASET"
24 fi
25
26 zfs snapshot "$SNAPSHOT_NAME"
27 mkdir -p "$SNAPSHOT_DIRECTORY"
28 mount -t zfs "$SNAPSHOT_NAME" "$SNAPSHOT_DIRECTORY"
29)
30
31destroy_snapshot() (
32 DATASET="$1"
33
34 SNAPSHOT_NAME="$DATASET@$SNAPSHOT_LABEL"
35
36 umount "$SNAPSHOT_NAME" 2>/dev/null || true
37 zfs destroy "$SNAPSHOT_NAME"
38)
39
40case "${1:-help}" in
41 "help") usage;;
42 "snap") create_snapshot "$2";;
43 "free") destroy_snapshot "$2";;
44esac
45