From 5bdbcea22a55979bcd4aa21aa4f76c9b818e1511 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 17 May 2017 04:06:27 +0200 Subject: Add backup scripts --- backup-all.sh | 24 ++++++++++++++++++++++++ backup-dataset.sh | 27 +++++++++++++++++++++++++++ snap.sh | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100755 backup-all.sh create mode 100755 backup-dataset.sh create mode 100755 snap.sh 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 @@ +#!/bin/sh -euf + +usage() ( + echo "backup-all.sh [duplicity options]" + echo + echo "Backups each dataset listed in the given file using Duplicity," + echo "each line of the file being a pair of a dataset name and a Duplicity destination URI." + echo +) + +backup_all() ( + LIST_FILE="$1" + DUPLICITY_OPTIONS="$2" + + while read -r LINE; do + backup-dataset.sh $LINE $DUPLICITY_OPTIONS + done <"$LIST_FILE" +) + +case "${1:-help}" in + "help") usage;; + *) backup_all "$1" "${2:-}";; +esac + 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 @@ +#!/bin/sh -euf + +SNAPSHOT_DIRECTORY="/var/backups/dataset-snapshots" + +usage() ( + echo "backup-dataset.sh [duplicity options]" + echo + echo "Performs a duplicity incremental backup of the specified dataset." + echo "An atomic snapshot of the dataset is taken prior to backing up." + echo +) + +backup_dataset() ( + DATASET="$1" + DESTINATION="$2" + DUPLICITY_OPTIONS="$3" + + sudo snap.sh snap "$DATASET" + PASSPHRASE="null" duplicity $DUPLICITY_OPTIONS "$SNAPSHOT_DIRECTORY/$DATASET" "$DESTINATION" + sudo snap.sh free "$DATASET" +) + +case "${1:-help}" in + "help") usage;; + *) backup_dataset "$1" "$2" "${3:-}";; +esac + diff --git a/snap.sh b/snap.sh new file mode 100755 index 0000000..36f32e6 --- /dev/null +++ b/snap.sh @@ -0,0 +1,45 @@ +#!/bin/sh -euf + +MOUNT_DIRECTORY="/var/backups/dataset-snapshots" +SNAPSHOT_LABEL="snap" + +usage() ( + echo "snap.sh " + echo + echo "Creates or destroys a snapshot of a ZFS dataset, labelled with @$SNAPSHOT_LABEL." + echo "Snapshots are automatically mounted in $MOUNT_DIRECTORY." + echo "If a snapshot already exists, it is replaced." + echo +) + +create_snapshot() ( + DATASET="$1" + + SNAPSHOT_NAME="$DATASET@$SNAPSHOT_LABEL" + SNAPSHOT_DIRECTORY="$MOUNT_DIRECTORY/$DATASET" + + if zfs list "$SNAPSHOT_NAME" >/dev/null 2>/dev/null; then + echo "Snapshot already exists. Overwriting." + destroy_snapshot "$DATASET" + fi + + zfs snapshot "$SNAPSHOT_NAME" + mkdir -p "$SNAPSHOT_DIRECTORY" + mount -t zfs "$SNAPSHOT_NAME" "$SNAPSHOT_DIRECTORY" +) + +destroy_snapshot() ( + DATASET="$1" + + SNAPSHOT_NAME="$DATASET@$SNAPSHOT_LABEL" + + umount "$SNAPSHOT_NAME" 2>/dev/null || true + zfs destroy "$SNAPSHOT_NAME" +) + +case "${1:-help}" in + "help") usage;; + "snap") create_snapshot "$2";; + "free") destroy_snapshot "$2";; +esac + -- cgit v1.2.3