From 8350b3bf7013e2277bfdb62ff639c2be843b653e Mon Sep 17 00:00:00 2001 From: Nicolas Chan Date: Mon, 14 Jun 2021 23:12:19 -0700 Subject: [PATCH] Add option for explicit restic hostname --- README.md | 1 + backup.sh | 13 ++++++++++--- test/test.sh | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e9f742b..ed7bdcc 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,7 @@ Command line options: -e Compression file extension, exclude leading "." (default: gz) -f Output file name (default is the timestamp) -h Shows this help text +-H Set hostname for restic backup (restic only) -i Input directory (path to world folder, use -i once for each world) -l Compression level (default: 3) -m Maximum backups to keep, use -1 for unlimited (default: 128) diff --git a/backup.sh b/backup.sh index f3dc8e2..d3c0827 100755 --- a/backup.sh +++ b/backup.sh @@ -20,6 +20,7 @@ ENABLE_CHAT_MESSAGES=false # Tell players in Minecraft chat about backup status PREFIX="Backup" # Shows in the chat message DEBUG=false # Enable debug messages SUPPRESS_WARNINGS=false # Suppress warnings +RESTIC_HOSTNAME="" # Leave empty to use system hostname LOCK_FILE="" # Optional lock file to acquire to ensure two backups don't run at once LOCK_FILE_TIMEOUT="" # Optional lock file wait timeout (in seconds) WINDOW_MANAGER="screen" # Choices: screen, tmux, RCON @@ -40,7 +41,7 @@ debug-log () { fi } -while getopts 'a:cd:e:f:hi:l:m:o:p:qr:s:t:u:vw:x' FLAG; do +while getopts 'a:cd:e:f:hH:i:l:m:o:p:qr:s:t:u:vw:x' FLAG; do case $FLAG in a) COMPRESSION_ALGORITHM=$OPTARG ;; c) ENABLE_CHAT_MESSAGES=true ;; @@ -55,6 +56,7 @@ while getopts 'a:cd:e:f:hi:l:m:o:p:qr:s:t:u:vw:x' FLAG; do echo "-e Compression file extension, exclude leading \".\" (default: gz)" echo "-f Output file name (default is the timestamp)" echo "-h Shows this help text" + echo "-H Set hostname for restic backup (restic only)" echo "-i Input directory (path to world folder, use -i once for each world)" echo "-l Compression level (default: 3)" echo "-m Maximum backups to keep, use -1 for unlimited (default: 128)" @@ -67,9 +69,9 @@ while getopts 'a:cd:e:f:hi:l:m:o:p:qr:s:t:u:vw:x' FLAG; do echo "-u Lock file timeout seconds (empty = unlimited)" echo "-v Verbose mode" echo "-w Window manager: screen (default), tmux, RCON" - echo "-x Bukkit-style server backup mode (world files are split by dimension)" exit 0 ;; + H) RESTIC_HOSTNAME=$OPTARG ;; i) SERVER_WORLDS+=("$OPTARG") ;; l) COMPRESSION_LEVEL=$OPTARG ;; m) MAX_BACKUPS=$OPTARG ;; @@ -522,7 +524,12 @@ do-backup () { if [[ "$RESTIC_REPO" != "" ]]; then RESTIC_TIMESTAMP="${TIMESTAMP:0:10} ${TIMESTAMP:11:2}:${TIMESTAMP:14:2}:${TIMESTAMP:17:2}" - restic backup -r "$RESTIC_REPO" "${SERVER_WORLDS[@]}" --time "$RESTIC_TIMESTAMP" "$QUIET" + if [[ "$RESTIC_HOSTNAME" == "" ]]; then + RESTIC_HOSTNAME_OPTION=() + else + RESTIC_HOSTNAME_OPTION=("--host" "$RESTIC_HOSTNAME") + fi + restic backup -r "$RESTIC_REPO" "${SERVER_WORLDS[@]}" --time "$RESTIC_TIMESTAMP" "$QUIET" "${RESTIC_HOSTNAME_OPTION[@]}" ARCHIVE_EXIT_CODE=$? if [ "$ARCHIVE_EXIT_CODE" -eq 3 ]; then log-warning "Incomplete snapshot taken (some files could not be read)" diff --git a/test/test.sh b/test/test.sh index 9fb69dc..5935aab 100755 --- a/test/test.sh +++ b/test/test.sh @@ -80,6 +80,24 @@ check-latest-backup-restic () { # Tests +test-restic-explicit-hostname () { + EXPECTED_HOSTNAME="${HOSTNAME}blahblah" + TIMESTAMP="$(date +%F_%H-%M-%S --date="2021-01-01")" + ./backup.sh -i "$TEST_TMP/server/world" -r "$TEST_TMP/backups-restic" -s "$SCREEN_TMP" -f "$TIMESTAMP" -H "$EXPECTED_HOSTNAME" + check-latest-backup-restic + LATEST_BACKUP_HOSTNAME=$(restic -r "$TEST_TMP/backups-restic" snapshots latest --json | jq -r '.[0]["hostname"]') + assertEquals "$EXPECTED_HOSTNAME" "$LATEST_BACKUP_HOSTNAME" +} + +test-restic-default-hostname () { + EXPECTED_HOSTNAME="${HOSTNAME}" + TIMESTAMP="$(date +%F_%H-%M-%S --date="2021-01-01")" + ./backup.sh -i "$TEST_TMP/server/world" -r "$TEST_TMP/backups-restic" -s "$SCREEN_TMP" -f "$TIMESTAMP" + check-latest-backup-restic + LATEST_BACKUP_HOSTNAME=$(restic -r "$TEST_TMP/backups-restic" snapshots latest --json | jq -r '.[0]["hostname"]') + assertEquals "$EXPECTED_HOSTNAME" "$LATEST_BACKUP_HOSTNAME" +} + test-backup-defaults () { TIMESTAMP="$(date +%F_%H-%M-%S --date="2021-01-01")" ./backup.sh -i "$TEST_TMP/server/world" -o "$TEST_TMP/backups" -s "$SCREEN_TMP" -f "$TIMESTAMP"