diff options
Diffstat (limited to 'snap.sh')
-rwxr-xr-x | snap.sh | 45 |
1 files changed, 45 insertions, 0 deletions
@@ -0,0 +1,45 @@ | |||
1 | #!/bin/sh -euf | ||
2 | |||
3 | MOUNT_DIRECTORY="/var/backups/dataset-snapshots" | ||
4 | SNAPSHOT_LABEL="snap" | ||
5 | |||
6 | usage() ( | ||
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 | |||
15 | create_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 | |||
31 | destroy_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 | |||
40 | case "${1:-help}" in | ||
41 | "help") usage;; | ||
42 | "snap") create_snapshot "$2";; | ||
43 | "free") destroy_snapshot "$2";; | ||
44 | esac | ||
45 | |||