aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar TheMightyV <themightyv@protonmail.com> 2022-01-05 22:04:10 +0100
committerGravatar TheMightyV <themightyv@protonmail.com> 2022-01-05 22:04:10 +0100
commitae5db1b37ae8554dff72b0da42ccbbbbc64baf71 (patch)
tree453b212c75d89f796628b46f1103807a3da9fb8b
parent1ba766d277fc35435ca80b9fcfa28c57468f7bce (diff)
downloadminecraft-server-tools-ae5db1b37ae8554dff72b0da42ccbbbbc64baf71.tar.gz
split echos to debug/info/error levels
unified ls_all backend functions unified backend function calls ls_dir -> ls in backends fixed accidental echo -> log_info replacement
-rw-r--r--backends/borg.sh28
-rw-r--r--backends/bup.sh28
-rw-r--r--backends/tar.sh24
-rwxr-xr-xserver.sh147
-rwxr-xr-xtests.sh18
5 files changed, 106 insertions, 139 deletions
diff --git a/backends/borg.sh b/backends/borg.sh
index b0351a0..ec39bc8 100644
--- a/backends/borg.sh
+++ b/backends/borg.sh
@@ -1,7 +1,7 @@
function borg_init() {
local encryption
if [ -z "$BACKUP_PASSCOMMAND" ] ; then
- echo "borg: no password given, repository is not protected"
+ log_info "borg: no password given, repository is not protected"
encryption="none"
else
encryption="repokey-blake2"
@@ -10,9 +10,10 @@ function borg_init() {
export BORG_PASSCOMMAND="$BACKUP_PASSCOMMAND"
for backup_dir in ${BACKUP_DIRS[*]}
do
+ log_debug "Initializing repo at $backup_dir "
# borg will check if repo exists
borg init --encryption="$encryption" "$backup_dir"
- done
+ done
}
function borg_create_backup() {
@@ -22,9 +23,9 @@ function borg_create_backup() {
do
export BORG_REPO="$backup_dir"
- trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM
+ trap 'echo [WARNING] $( date ) Backup interrupted >&2; exit 2' INT TERM
- echo "borg: backing up to \"$backup_dir\""
+ log_info "borg: backing up to \"$backup_dir\""
borg create \
"${backup_dir}::${BACKUP_NAME}_$(date +'%F_%H-%M-%S')" \
@@ -35,7 +36,7 @@ function borg_create_backup() {
local backup_exit=$?
- echo_debug "borg: pruning repository at \"$backup_dir\""
+ log_debug "borg: pruning repository at \"$backup_dir\""
borg prune \
--prefix '{hostname}-' \
@@ -53,11 +54,11 @@ function borg_create_backup() {
retcode=$(( global_exit > retcode ? global_exit : retcode ))
if [ ${global_exit} -eq 0 ]; then
- echo_debug "borg: backup and prune finished successfully"
+ log_debug "borg: backup and prune finished successfully"
elif [ ${global_exit} -eq 1 ]; then
- echo "borg: backup and/or prune finished with warnings"
+ log_info "borg: backup and/or prune finished with warnings"
else
- echo "borg: backup and/or prune finished with errors"
+ log_error "borg: backup and/or prune finished with errors"
fi
#exit ${global_exit}
done
@@ -65,20 +66,11 @@ function borg_create_backup() {
}
# server_restore relies on output format of this function
-function borg_ls_dir() {
+function borg_ls() {
export BORG_PASSCOMMAND="$BACKUP_PASSCOMMAND"
borg list "$1" | cut -d' ' -f1 | sort -r
}
-function borg_ls_all() {
- export BORG_PASSCOMMAND="$BACKUP_PASSCOMMAND"
- for backup_dir in ${BACKUP_DIRS[*]}
- do
- echo "borg: backups in \"$backup_dir\":"
- borg list "$backup_dir" | cut -d' ' -f1
- done
-}
-
function borg_restore() {
export BORG_PASSCOMMAND="$BACKUP_PASSCOMMAND"
local remote="$1"
diff --git a/backends/bup.sh b/backends/bup.sh
index d1250de..6aa92b6 100644
--- a/backends/bup.sh
+++ b/backends/bup.sh
@@ -15,14 +15,14 @@ function bup_init() {
bup -d "$(bup_local)" index "$WORLD_NAME"
local status=$?
if [ $status -ne 0 ]; then
- echo_debug "bup: no local repo found, creating..."
+ log_debug "bup: no local repo found, creating..."
bup -d "$(bup_local)" init -r "$(bup_local)"
- echo_debug "bup: created local repo at $(bup_local)"
+ log_debug "bup: created local repo at $(bup_local)"
fi
}
function bup_create_backup() {
- echo_debug "bup: backup started"
+ log_debug "bup: backup started"
bup -d "$(bup_local)" index "$WORLD_NAME"
@@ -31,20 +31,20 @@ function bup_create_backup() {
local retcode=1
for backup_dir in ${BACKUP_DIRS[*]}
do
- echo "bup: backing up to \"$backup_dir\""
+ log_info "bup: backing up to \"$backup_dir\""
# try to save to remote
bup -d "$(bup_local)" save -r "$backup_dir" -n "$BACKUP_NAME" "$WORLD_NAME"
local status=$?
# if failed - reinit remote and try again
if [ $status -ne 0 ]; then
- echo_debug "bup: failed backing up to \"$backup_dir\", reinitializing remote..."
+ log_debug "bup: failed backing up to \"$backup_dir\", reinitializing remote..."
bup -d "$(bup_local)" init -r "$backup_dir"
status=$?
- if [ $status -ne 0 ]; then
- echo_debug "bup: created remote at \"$backup_dir\""
+ if [ $status -eq 0 ]; then
+ log_debug "bup: created remote at \"$backup_dir\""
bup -d "$(bup_local)" save -r "$backup_dir" -n "$BACKUP_NAME" "$WORLD_NAME"
else
- echo "bup: failed to make remote at \"$backup_dir\", moving on"
+ log_error "bup: failed to make remote at \"$backup_dir\", moving on"
fi
else
if [ ! "$backup_dir" = "$(bup_local)" ]; then
@@ -53,24 +53,16 @@ function bup_create_backup() {
fi
done
- echo "bup: backup finished"
+ log_debug "bup: backup finished"
return $retcode
}
# server_restore relies on output format of this function
-function bup_ls_dir() {
+function bup_ls() {
local backup_dir="$1"
bup -d "$(bup_local)" ls -r "$backup_dir" "$BACKUP_NAME" | sort -r
}
-function bup_ls_all() {
- for backup_dir in ${BACKUP_DIRS[*]}
- do
- echo "bup: backups in \"$backup_dir\":"
- bup -d "$(bup_local)" ls -r "$backup_dir" --human-readable -l "$BACKUP_NAME"
- done
-}
-
function bup_restore() {
local remote="$1"
local snapshot="$2"
diff --git a/backends/tar.sh b/backends/tar.sh
index 6f7a3d6..6748ac9 100644
--- a/backends/tar.sh
+++ b/backends/tar.sh
@@ -5,7 +5,7 @@ function tar_init() {
# TODO: Make default .tar with optional bup
function tar_create_backup() {
- echo_debug "tar: backing up..."
+ log_debug "tar: backing up..."
local status
@@ -14,23 +14,23 @@ function tar_create_backup() {
tar -czf "$archname" "./$WORLD_NAME"
status=$?
if [ $status -ne 0 ]; then
- echo "tar: failed to save the world"
+ log_error "tar: failed to save the world"
rm "$archname" #remove (probably faulty) archive
return 1
fi
- echo_debug "tar: world saved to $archname, pushing it to backup directories..."
+ log_debug "tar: world saved to $archname, pushing it to backup directories..."
# 0 if could save to at least one backup dir
# TODO: make more strict?
local retcode=1
for backup_dir in ${BACKUP_DIRS[*]}
do
- echo "tar: backing up to \"$backup_dir\""
+ log_info "tar: backing up to \"$backup_dir\""
# scp acts as cp for local destination directories
scp "$archname" "$backup_dir/"
status=$?
if [ $status -ne 0 ]; then
- echo "tar: failed pushing to \"$backup_dir\", moving on"
+ log_error "tar: failed pushing to \"$backup_dir\", moving on"
else
retcode=0
fi
@@ -38,13 +38,13 @@ function tar_create_backup() {
rm "$archname"
- echo "tar: backup finished"
+ log_debug "tar: backup finished"
return $retcode
}
# server_restore relies on output format of this function
-function tar_ls_dir() {
+function tar_ls() {
local backup_dir="$1"
if [[ "$backup_dir" == *:* ]]; then
@@ -56,14 +56,6 @@ function tar_ls_dir() {
fi
}
-function tar_ls_all() {
- for backup_dir in ${BACKUP_DIRS[*]}
- do
- echo "tar: backups in ${backup_dir}:"
- tar_ls_dir "$backup_dir"
- done
-}
-
function tar_restore() {
local remote="$1"
local snapshot="$2"
@@ -73,7 +65,7 @@ function tar_restore() {
scp "$remote/$snapshot" "/tmp/"
status=$?
if [ $status -ne 0 ]; then
- echo "tar: failed to get archive from \"$remote/$snapshot\""
+ log_error "tar: failed to get archive from \"$remote/$snapshot\""
return 1
fi
diff --git a/server.sh b/server.sh
index 2f4a0ae..2b58798 100755
--- a/server.sh
+++ b/server.sh
@@ -4,7 +4,7 @@ if [ -e "serverconf.sh" ]
then
source "serverconf.sh"
else
- echo No configuration found in PWD. Exiting.
+ log_error "No configuration found in PWD. Exiting."
exit 1
fi
@@ -12,14 +12,16 @@ source "backends/tar.sh"
source "backends/bup.sh"
source "backends/borg.sh"
-function echo_debug() {
+function log_debug() {
if [ $VERBOSE -ne 0 ]; then
- echo "$1"
+ printf "[DEBUG] $1\n" >&2
fi
}
+log_info() { printf "[INFO] $1\n" ; }
+log_error() { printf "[ERROR] $1\n" >&2 ; }
function backup_hook_example {
- bup -d $CUR_BACK_DIR ls -l $BACKUP_NAME/latest/var/minecraft
+ bup -d $CUR_BACK_DIR ls -l "$BACKUP_NAME/latest/var/minecraft"
}
function send_cmd () {
@@ -28,7 +30,7 @@ function send_cmd () {
function assert_not_running() {
if server_running; then
- echo "It seems a server is already running. If this is not the case,\
+ log_info "It seems a server is already running. If this is not the case,\
manually attach to the running screen and close it."
exit 1
fi
@@ -36,7 +38,7 @@ function assert_not_running() {
function assert_running() {
if ! server_running; then
- echo "Server not running"
+ log_info "Server not running"
exit 1
fi
}
@@ -46,7 +48,7 @@ function server_start() {
if [ ! -f "eula.txt" ]
then
- echo "eula.txt not found. Creating and accepting EULA."
+ log_info "eula.txt not found. Creating and accepting EULA."
echo "eula=true" > "eula.txt"
fi
@@ -54,7 +56,7 @@ function server_start() {
$JRE_JAVA $JVM_ARGS -jar $JAR $JAR_ARGS
pid=`tmux -S $TMUX_SOCKET list-panes -t $TMUX_WINDOW -F "#{pane_pid}"`
echo $pid > $PIDFILE
- echo Started with PID $pid
+ log_info "Started with PID $pid"
exit
}
@@ -73,7 +75,7 @@ function server_stop() {
RET=$?
done
- echo "stopped the server"
+ log_info "stopped the server"
rm -f $PIDFILE
@@ -98,9 +100,9 @@ function server_running() {
function server_status() {
if server_running
then
- echo "Server is running"
+ log_info "Server is running"
else
- echo "Server is not running"
+ log_info "Server is not running"
fi
exit
}
@@ -116,6 +118,21 @@ function players_online() {
[ `tail -n 3 "$LOGFILE" | grep -c "There are 0"` -lt 1 ]
}
+function backup_backend_run() {
+ local cmd
+ # could do ${BACKUP_BACKEND}_$1 though
+ if [ $BACKUP_BACKEND = "bup" ]; then
+ cmd="bup"
+ elif [ $BACKUP_BACKEND = "borg" ]; then
+ cmd="borg"
+ else
+ cmd="tar"
+ fi
+ cmd="${cmd}_$1"
+ log_debug "Backup backend command: \"$cmd\""
+ eval "$cmd"
+}
+
function init_backup() {
# even though bup and borg are smart, they will not create a path for a repo
for backup_dir in ${BACKUP_DIRS[*]}
@@ -129,13 +146,7 @@ function init_backup() {
fi
done
- if [ $BACKUP_BACKEND = "bup" ]; then
- bup_init
- elif [ $BACKUP_BACKEND = "borg" ]; then
- borg_init
- else
- tar_init
- fi
+ backup_backend_run "init"
}
function same_world() {
@@ -158,52 +169,44 @@ function test_backup_integrity() {
# restore most recent backup to a temporary dir
if ! server_restore "$backup_dir" 0 "$tmpdir" ; then
- echo "Failed to get latest snapshot from \"$backup_dir\""
+ log_error "Failed to get latest snapshot from \"$backup_dir\""
retcode=1
elif ! same_world "$WORLD_NAME" "$tmpdir/$WORLD_NAME" ; then
- echo "Latest backup from \"$backup_dir\" differs from current world!"
+ log_error "Latest backup from \"$backup_dir\" differs from current world!"
retcode=1
else
- echo "Backup at \"$backup_dir\" is OK"
+ log_info "Backup at \"$backup_dir\" is OK"
fi
rm -r "$tmpdir"
done
if [ $retcode -ne 0 ] ; then
- echo "Backup integrity check: FAILED"
+ log_error "Backup integrity check: FAILED"
else
- echo "Backup integrity check: OK"
+ log_info "Backup integrity check: OK"
fi
return $retcode
}
function create_backup() {
init_backup
-
- if [ $BACKUP_BACKEND = "bup" ]; then
- bup_create_backup
- elif [ $BACKUP_BACKEND = "borg" ]; then
- borg_create_backup
- else
- tar_create_backup
- fi
-
+ backup_backend_run "create_backup"
test_backup_integrity
}
function server_backup_safe() {
local force=$1
- echo "Detected running server. Checking if players online..."
+ log_info "Detected running server. Checking if players online..."
if [ "$force" != "true" ] && ! players_online; then
- echo "Players are not online. Not backing up."
+ log_info "Players are not online. Not backing up."
return
fi
- echo "Disabling autosave"
+ log_info "Disabling autosave"
send_cmd "save-off"
send_cmd "save-all flush"
- echo "Waiting for save... If froze, run /save-on to re-enable autosave!!"
+ log_info "Waiting for save... If froze, run /save-on to re-enable autosave!!"
sleep 1
while [ $(tail -n 3 "$LOGFILE" | grep -c "Saved the game") -lt 1 ]
@@ -211,29 +214,29 @@ function server_backup_safe() {
sleep 1
done
sleep 2
- echo "Done! starting backup..."
+ log_info "Done! starting backup..."
create_backup
local RET=$?
- echo "Re-enabling auto-save"
+ log_info "Re-enabling auto-save"
send_cmd "save-on"
if [ $RET -eq 0 ]; then
- echo Running backup hook
+ log_info "Running backup hook"
$BACKUP_HOOK
fi
}
function server_backup_unsafe() {
- echo "No running server detected. Running Backup"
+ log_info "No running server detected. Running Backup"
create_backup
local status=$?
if [ $status -eq 0 ]; then
- echo Running backup hook
+ log_info "Running backup hook"
$BACKUP_HOOK
fi
}
@@ -251,12 +254,12 @@ function server_backup() {
if [ "$force" = "true" ]; then
if backup_running; then
- echo "A backup is running. Aborting..."
+ log_info "A backup is running. Aborting..."
return
fi
else
if fbackup_running; then
- echo "A force backup is running. Aborting..."
+ log_info "A force backup is running. Aborting..."
return
fi
fi
@@ -268,14 +271,12 @@ function server_backup() {
fi
}
-function ls_backups() {
- if [ $BACKUP_BACKEND = "bup" ]; then
- bup_ls_all
- elif [ $BACKUP_BACKEND = "borg" ]; then
- borg_ls_all
- else
- tar_ls_all
- fi
+function server_ls_backups() {
+ for backup_dir in ${BACKUP_DIRS[*]}
+ do
+ log_info "backups in ${backup_dir}:"
+ backup_backend_run "ls \"$backup_dir\""
+ done
}
# creates a selection dialog
@@ -285,7 +286,6 @@ function choose_from() {
echo "$item"
return
done
- echo ""
}
# checks if an item is in the array
@@ -318,32 +318,26 @@ function server_restore() {
fi
if [ ${#BACKUP_DIRS[@]} -eq 0 ]; then
- echo "No backup directories found, abort"
+ log_error "No backup directories found, abort"
return 1
fi
if [ -z $backup_dir ]; then
- echo "From where get the snapshot?"
+ log_info "From where get the snapshot?"
backup_dir="$(choose_from "${BACKUP_DIRS[@]}")"
fi
if ! is_in "$backup_dir" "${BACKUP_DIRS[@]}" ; then
- echo "No valid backup directory selected, abort"
+ log_error "No valid backup directory selected, abort"
return 1
fi
- local snapshots=$(
- if [ $BACKUP_BACKEND = "bup" ]; then
- bup_ls_dir "$backup_dir"
- elif [ $BACKUP_BACKEND = "borg" ]; then
- borg_ls_dir "$backup_dir"
- else
- tar_ls_dir "$backup_dir"
- fi
- )
+ local snapshots="$( backup_backend_run "ls \"$backup_dir\"" )"
+ log_debug "Snapshots found:"
+ log_debug "$snapshots"
if [ -z "$snapshots" ]; then
- echo "No snapshots found, abort"
+ log_error "No snapshots found, abort"
return 1
fi
# convert multiline string to bash array
@@ -352,47 +346,40 @@ function server_restore() {
local snapshot
if [ -z $snapshot_index ]; then
- echo "Select which snapshot to restore"
+ log_info "Select which snapshot to restore"
snapshot=$(choose_from "${snapshots[@]}")
else
snapshot="${snapshots[snapshot_index]}"
fi
if ! is_in "$snapshot" "${snapshots[@]}" ; then
- echo "No valid snapshot selected, abort"
+ log_error "No valid snapshot selected, abort"
return 1
fi
- echo_debug "Restoring snapshot \"$snapshot\" from \"$backup_dir\""
+ log_debug "Restoring snapshot \"$snapshot\" from \"$backup_dir\""
# if we restore to PWD, we will overwrite the current world, which might be harmful
local oldworld_name
if [ "$dest" = "$PWD" ] && [[ -d "$WORLD_NAME" ]]; then
- echo -n "Preserving old world: "
oldworld_name="${WORLD_NAME}.old.$(date +'%F_%H-%M-%S.%N')"
- mv -n -v "$PWD/$WORLD_NAME" "$PWD/$oldworld_name"
+ log_info "Preserving old world: $(mv -n -v "$PWD/$WORLD_NAME" "$PWD/$oldworld_name")"
fi
- if [ $BACKUP_BACKEND = "bup" ]; then
- bup_restore "$backup_dir" "$snapshot" "$dest"
- elif [ $BACKUP_BACKEND = "borg" ]; then
- borg_restore "$backup_dir" "$snapshot" "$dest"
- else
- tar_restore "$backup_dir" "$snapshot" "$dest"
- fi
+ backup_backend_run "restore \"$backup_dir\" \"$snapshot\" \"$dest\""
local status=$?
# if we preseved the current world, but failed to restore the snapshot
if [ ! -z ${oldworld_name+x} ] && [ $status -ne 0 ]; then
- echo "Failed to restore snapshot, putting old world back where it was:"
+ log_error "Failed to restore snapshot, putting old world back where it was:"
rm -rv "$PWD/$WORLD_NAME"
mv -v "$PWD/$oldworld_name" "$PWD/$WORLD_NAME"
return 1
fi
- echo_debug "Snapshot restored"
+ log_debug "Snapshot restored"
return 0
}
@@ -422,7 +409,7 @@ case $1 in
server_backup "true"
;;
"ls")
- ls_backups
+ server_ls_backups
;;
*)
echo "Usage: $0 start|stop|attach|status|backup"
diff --git a/tests.sh b/tests.sh
index fc6a692..38ce83b 100755
--- a/tests.sh
+++ b/tests.sh
@@ -55,13 +55,17 @@ function test_backend() {
exit
fi
- ls_backups
-
+ local backups="$(server_ls_backups)"
+ if [ -z "$backups" ]; then
+ log_error "Found no backups"
+ cleanup
+ exit
+ fi
# corrupting current (new) world
find "$DIR/$WORLD_NAME" -type f -exec shred {} \;
if same_world "$DIR/$WORLD_NAME" "$DIR/$new_world" ; then
- echo "Failed to corrupt new world"
+ log_error "Failed to corrupt new world"
cleanup
exit
fi
@@ -71,13 +75,13 @@ function test_backend() {
server_restore "${BACKUP_DIRS[0]}" 0
# must be: new backup == new world
if ! same_world "$DIR/$WORLD_NAME" "$DIR/$new_world" ; then
- echo "${BACKUP_BACKEND}: new backup != new world"
+ log_error "${BACKUP_BACKEND}: new backup != new world"
cleanup
exit
fi
# must be: new backup != old world
if same_world "$DIR/$WORLD_NAME" "$DIR/$old_world" ; then
- echo "${BACKUP_BACKEND}: new backup == old world"
+ log_error "${BACKUP_BACKEND}: new backup == old world"
cleanup
exit
fi
@@ -92,13 +96,13 @@ function test_backend() {
fi
# must be: old backup == old world
if ! same_world "$DIR/$WORLD_NAME" "$DIR/$old_world" ; then
- echo "${BACKUP_BACKEND}: old backup != old world"
+ log_error "${BACKUP_BACKEND}: old backup != old world"
cleanup
exit
fi
# must be: old backup != new world
if same_world "$DIR/$WORLD_NAME" "$DIR/$new_world" ; then
- echo "${BACKUP_BACKEND}: old backup == new world"
+ log_error "${BACKUP_BACKEND}: old backup == new world"
cleanup
exit
fi