summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorroot2017-05-17 04:06:27 +0200
committerroot2017-05-17 04:06:27 +0200
commit5bdbcea22a55979bcd4aa21aa4f76c9b818e1511 (patch)
tree06ab275a2bf501768b0bdf4a9f843c4b2c1c3bd0
parent118ff790598aebf3c3f11a2ed73f0c842ae83e9c (diff)
downloadserver-utilities-5bdbcea22a55979bcd4aa21aa4f76c9b818e1511.tar.gz
Add backup scripts
-rwxr-xr-xbackup-all.sh24
-rwxr-xr-xbackup-dataset.sh27
-rwxr-xr-xsnap.sh45
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
3usage() (
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
11backup_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
20case "${1:-help}" in
21 "help") usage;;
22 *) backup_all "$1" "${2:-}";;
23esac
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
3SNAPSHOT_DIRECTORY="/var/backups/dataset-snapshots"
4
5usage() (
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
13backup_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
23case "${1:-help}" in
24 "help") usage;;
25 *) backup_dataset "$1" "$2" "${3:-}";;
26esac
27
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