Skip to content

Commit

Permalink
Add option for explicit restic hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaschan committed Jun 15, 2021
1 parent e5eb486 commit 8350b3b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 10 additions & 3 deletions backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 ;;
Expand All @@ -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)"
Expand All @@ -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 ;;
Expand Down Expand Up @@ -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)"
Expand Down
18 changes: 18 additions & 0 deletions test/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 8350b3b

Please sign in to comment.