diff options
author | root | 2017-05-17 04:06:27 +0200 |
---|---|---|
committer | root | 2017-05-17 04:06:27 +0200 |
commit | 5bdbcea22a55979bcd4aa21aa4f76c9b818e1511 (patch) | |
tree | 06ab275a2bf501768b0bdf4a9f843c4b2c1c3bd0 | |
parent | 118ff790598aebf3c3f11a2ed73f0c842ae83e9c (diff) | |
download | server-utilities-5bdbcea22a55979bcd4aa21aa4f76c9b818e1511.tar.gz |
Add backup scripts
-rwxr-xr-x | backup-all.sh | 24 | ||||
-rwxr-xr-x | backup-dataset.sh | 27 | ||||
-rwxr-xr-x | snap.sh | 45 |
3 files changed, 96 insertions, 0 deletions
diff --git a/backup-all.sh b/backup-all.sh new file mode 100755 index 0000000..aeea10b --- /dev/null +++ b/backup-all.sh | |||
@@ -0,0 +1,24 @@ | |||
1 | #!/bin/sh -euf | ||
2 | |||
3 | usage() ( | ||
4 | echo "backup-all.sh <dataset list> [duplicity options]" | ||
5 | echo | ||
6 | echo "Backups each dataset listed in the given file using Duplicity," | ||
7 | echo "each line of the file being a pair of a dataset name and a Duplicity destination URI." | ||
8 | echo | ||
9 | ) | ||
10 | |||
11 | backup_all() ( | ||
12 | LIST_FILE="$1" | ||
13 | DUPLICITY_OPTIONS="$2" | ||
14 | |||
15 | while read -r LINE; do | ||
16 | backup-dataset.sh $LINE $DUPLICITY_OPTIONS | ||
17 | done <"$LIST_FILE" | ||
18 | ) | ||
19 | |||
20 | case "${1:-help}" in | ||
21 | "help") usage;; | ||
22 | *) backup_all "$1" "${2:-}";; | ||
23 | esac | ||
24 | |||
diff --git a/backup-dataset.sh b/backup-dataset.sh new file mode 100755 index 0000000..e4303c5 --- /dev/null +++ b/backup-dataset.sh | |||
@@ -0,0 +1,27 @@ | |||
1 | #!/bin/sh -euf | ||
2 | |||
3 | SNAPSHOT_DIRECTORY="/var/backups/dataset-snapshots" | ||
4 | |||
5 | usage() ( | ||
6 | echo "backup-dataset.sh <dataset> <duplicity destination> [duplicity options]" | ||
7 | echo | ||
8 | echo "Performs a duplicity incremental backup of the specified dataset." | ||
9 | echo "An atomic snapshot of the dataset is taken prior to backing up." | ||
10 | echo | ||
11 | ) | ||
12 | |||
13 | backup_dataset() ( | ||
14 | DATASET="$1" | ||
15 | DESTINATION="$2" | ||
16 | DUPLICITY_OPTIONS="$3" | ||
17 | |||
18 | sudo snap.sh snap "$DATASET" | ||
19 | PASSPHRASE="null" duplicity $DUPLICITY_OPTIONS "$SNAPSHOT_DIRECTORY/$DATASET" "$DESTINATION" | ||
20 | sudo snap.sh free "$DATASET" | ||
21 | ) | ||
22 | |||
23 | case "${1:-help}" in | ||
24 | "help") usage;; | ||
25 | *) backup_dataset "$1" "$2" "${3:-}";; | ||
26 | esac | ||
27 | |||
@@ -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 | |||