From 3e5f948a004a668961631f73d2794867503eb890 Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Fri, 29 Nov 2019 15:00:53 -0600 Subject: [PATCH] Add log levels to Minecraft backup script --- roles/minecraft/files/minecraft-backup.sh | 60 ++++++++++++++++++----- 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/roles/minecraft/files/minecraft-backup.sh b/roles/minecraft/files/minecraft-backup.sh index b0dd96b..868d3f5 100644 --- a/roles/minecraft/files/minecraft-backup.sh +++ b/roles/minecraft/files/minecraft-backup.sh @@ -7,18 +7,50 @@ SCRATCH=$(mktemp -p /var/tmp -d minecraft.XXXXXXXXXX) NICE=15 TAR=minecraft-$(date +%Y-%m-%d).tar WAIT=30 +VERBOSE=${VERBOSE:-4} + +declare -A LOG_LEVELS +LOG_LEVELS=( + [emerg]=0 + [alert]=1 + [crit]=2 + [err]=3 + [warning]=4 + [notice]=5 + [info]=6 + [debug]=7 +) + +log() { + local LEVEL + local TS + + LEVEL=$1 + TS="$(date -Is)" + shift + + if [[ $VERBOSE -ge ${LOG_LEVELS[$LEVEL]} ]]; then + if [[ $# -ge 2 ]]; then + local FMT=$1 + shift + printf "%s [%s] ${FMT}\n" "${TS}" "$LEVEL" "$@" + else + echo "${TS} [$LEVEL]" "$@" + fi + fi +} finish() { - printf "Cleaning up ...\n" + log info "Cleaning up" rm -rf "$SCRATCH" if ! systemctl -q is-active "$SERVICE"; then - start_server + start_server $SERVICE fi } trap finish EXIT error_exit() { - printf "%s\n" "$1" + log crit "$1" exit 1 } @@ -27,14 +59,14 @@ stop_server() { local attempts="${2:-$WAIT}" if ! systemctl -q is-active "$unit"; then - printf "%s is already stopped\n" "$unit" + log warning "%s is already stopped\n" "$unit" return 0 fi systemctl -q stop "$unit" while systemctl -q is-active "$unit"; do - printf "Waiting for %s to stop ... (%s)\n" "$unit" "$attempts" + log warning "Waiting for %s to stop ... (%s)\n" "$unit" "$attempts" ((attempts--)) if ((attempts == 0)); then return 1 @@ -50,14 +82,14 @@ start_server() { local attempts="${2:-$WAIT}" if systemctl -q is-active "$unit"; then - printf "%s is already started\n" "$unit" + log warning "%s is already started\n" "$unit" return 0 fi systemctl -q start "$unit" while ! systemctl -q is-active "$unit"; do - printf "Waiting for %s to start ... (%d)\n" "$unit" "$attempts" + log warning "Waiting for %s to start ... (%d)\n" "$unit" "$attempts" ((attempts--)) if ((attempts == 0)); then return 1 @@ -74,7 +106,7 @@ open_files() { local attempts="${2:-$WAIT}" while (($(lsof +D "$dir" | wc -l) > 0)); do - printf "Waiting for open files ... (%d)\n" "$attempts" + log warning "Waiting for open files ... (%d)\n" "$attempts" ((attempts--)) if ((attempts == 0)); then return 1 @@ -87,26 +119,32 @@ open_files() { main() { - if ! stop_server; then + log info "Stopping %s service" $SERVICE + if ! stop_server $SERVICE; then error_exit "Failed to stop $SERVICE" fi - if ! open_files; then + log info "Checking for open files" + if ! open_files $VAR_DIR; then error_exit "Open files exist in $VAR_DIR" fi + log info "Starting backup" if ! tar -cf "$SCRATCH/$TAR" -C "$VAR_DIR" world; then error_exit "Archive failed" fi - if ! start_server; then + log info "Starting %s service" $SERVICE + if ! start_server $SERVICE; then error_exit "Failed to start $SERVICE" fi + log info "Compressing $TAR" if ! nice -n "$NICE" xz -9 "$SCRATCH/$TAR"; then error_exit "Failed to compress $SCRATCH/$TAR" else mv "$SCRATCH/$TAR.xz" "$BACKUP_DIR" + log info "Symlinking current archive" ln -nfs "$BACKUP_DIR/$TAR.xz" "$BACKUP_DIR/minecraft-latest.tar.xz" fi }