From 94e894645fec86aaffcb72ea9bbd2cad05dd4857 Mon Sep 17 00:00:00 2001 From: Nicolas Chan Date: Sat, 21 Mar 2020 19:09:49 -0700 Subject: [PATCH] Bug fixes for deleting methods --- backup.sh | 7 +++++-- src/backup-methods/tar.sh | 27 ++++++++++----------------- src/core.sh | 10 ++-------- src/logging.sh | 1 + test.sh | 9 +++++++-- 5 files changed, 25 insertions(+), 29 deletions(-) diff --git a/backup.sh b/backup.sh index 1de034b..1497fed 100755 --- a/backup.sh +++ b/backup.sh @@ -1,4 +1,4 @@ -#!/bin/env bash +#!/usr/bin/env bash # Minecraft server automatic backup management script # by Nicolas Chan @@ -27,6 +27,7 @@ SUPPRESS_WARNINGS=false # Suppress warnings DATE_FORMAT="%F_%H-%M-%S" TIMESTAMP=$(date +$DATE_FORMAT) +OPTIND=1 while getopts 'a:bcd:e:f:ghi:l:m:o:p:qs:v' FLAG; do case $FLAG in a) COMPRESSION_ALGORITHM=$OPTARG ;; @@ -68,7 +69,9 @@ done BASE_DIR=$(dirname "$(realpath "$0")") # shellcheck source=src/logging.sh -source "$BASE_DIR/src/logging.sh" +source "$BASE_DIR/src/logging.sh" \ + -q "$SUPPRESS_WARNINGS" \ + -v "$DEBUG" # Check for missing encouraged arguments if [[ $SCREEN_NAME == "" ]]; then diff --git a/src/backup-methods/tar.sh b/src/backup-methods/tar.sh index 1c0d48c..4be5865 100755 --- a/src/backup-methods/tar.sh +++ b/src/backup-methods/tar.sh @@ -23,16 +23,14 @@ mkdir -p "$BACKUP_DIRECTORY" # Parse file timestamp to one readable by "date" parse-file-timestamp () { - local DATE_STRING - DATE_STRING=$(echo "$1" | awk -F_ '{gsub(/-/,":",$2); print $1" "$2}') - echo "$DATE_STRING" + echo "$1" | awk -F_ '{gsub(/-/,":",$2); print $1" "$2}' } # Delete a backup delete-backup () { local BACKUP=$1 rm -f "$BACKUP_DIRECTORY/$BACKUP" - message-players "Deleted old backup" "$BACKUP" + echo "Deleted old backup" "$BACKUP" } # Sequential delete method @@ -44,9 +42,9 @@ delete-sequentially () { BASENAME=$(basename "$BACKUP_NAME") BACKUPS_UNSORTED+=("$BASENAME") done - local BACKUPS=() # List oldest first - while IFS='' read -r line; do BACKUPS+=("$line"); done < <(IFS=$'\n' sort <<<"${BACKUPS_UNSORTED[*]}") + # shellcheck disable=SC2207 + IFS=$'\n' BACKUPS=($(sort <<<"${BACKUPS_UNSORTED[*]}")) while [[ $MAX_BACKUPS -ge 0 && ${#BACKUPS[@]} -gt $MAX_BACKUPS ]]; do delete-backup "${BACKUPS[0]}" @@ -57,9 +55,9 @@ delete-sequentially () { BASENAME=$(basename "$BACKUP_NAME") BACKUPS_UNSORTED+=("$BASENAME") done - local BACKUPS=() # List oldest first - while IFS='' read -r line; do BACKUPS+=("$line"); done < <(IFS=$'\n' sort <<<"${BACKUPS_UNSORTED[*]}") + # shellcheck disable=SC2207 + IFS=$'\n' BACKUPS=($(sort <<<"${BACKUPS_UNSORTED[*]}")) done } @@ -107,6 +105,7 @@ delete-thinning () { fi local CURRENT_INDEX=0 + local BACKUPS_UNSORTED_RAW=("$BACKUP_DIRECTORY"/*) local BACKUPS_UNSORTED=() for BACKUP_NAME in "${BACKUPS_UNSORTED_RAW[@]}"; do @@ -114,9 +113,9 @@ delete-thinning () { BASENAME=$(basename "$BACKUP_NAME") BACKUPS_UNSORTED+=("$BASENAME") done - local BACKUPS=() # List newest first - while IFS='' read -r line; do BACKUPS+=("$line"); done < <(IFS=$'\n' sort -r <<<"${BACKUPS_UNSORTED[*]}") + # shellcheck disable=SC2207 + IFS=$'\n' BACKUPS=($(sort -r <<<"${BACKUPS_UNSORTED[*]}")) for BLOCK_INDEX in "${!BLOCK_SIZES[@]}"; do local BLOCK_SIZE=${BLOCK_SIZES[BLOCK_INDEX]} @@ -130,14 +129,8 @@ delete-thinning () { local OLDEST_BACKUP_TIMESTAMP OLDEST_BACKUP_TIMESTAMP=$(parse-file-timestamp "${OLDEST_BACKUP_IN_BLOCK:0:19}") - local BLOCK_COMMAND="$BLOCK_FUNCTION $OLDEST_BACKUP_TIMESTAMP" - if $BLOCK_COMMAND; then - # Oldest backup in this block satisfies the condition for placement in the next block - if $DEBUG; then - echo "$OLDEST_BACKUP_IN_BLOCK promoted to next block" - fi - else + if ! $BLOCK_FUNCTION "$OLDEST_BACKUP_TIMESTAMP"; then # Oldest backup in this block does not satisfy the condition for placement in next block delete-backup "$OLDEST_BACKUP_IN_BLOCK" break diff --git a/src/core.sh b/src/core.sh index f219349..67f81e7 100755 --- a/src/core.sh +++ b/src/core.sh @@ -1,12 +1,5 @@ #!/usr/bin/env bash -# Minecraft server automatic backup management script -# by Nicolas Chan -# https://github.com/nicolaschan/minecraft-backup -# MIT License -# -# For most convenience, run automatically with cron. - # This script implements the core functionality, and expects the following # functions to be defined by the method scripts: # @@ -35,6 +28,7 @@ while [[ $1 != "--" ]]; do done shift +OPTIND=1 while getopts 'c:g:p:q:v:' FLAG; do case $FLAG in c) ENABLE_CHAT_MESSAGES=$OPTARG ;; @@ -124,7 +118,7 @@ message-players-color () { local HOVER_MESSAGE=$3 log-info "$MESSAGE ($HOVER_MESSAGE)" if $ENABLE_CHAT_MESSAGES; then - execute_command \ + execute-command \ "tellraw @a [\"\",{\"text\":\"[$PREFIX] \",\"color\":\"gray\",\"italic\":true},{\"text\":\"$MESSAGE\",\"color\":\"$COLOR\",\"italic\":true,\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":[{\"text\":\"$HOVER_MESSAGE\"}]}}}]" fi } diff --git a/src/logging.sh b/src/logging.sh index ee242e1..8784982 100644 --- a/src/logging.sh +++ b/src/logging.sh @@ -3,6 +3,7 @@ SUPPRESS_WARNINGS=false DEBUG=true +OPTIND=1 while getopts 'q:v:' FLAG; do case $FLAG in q) SUPPRESS_WARNINGS=$OPTARG ;; diff --git a/test.sh b/test.sh index 4d54554..a3e98fc 100755 --- a/test.sh +++ b/test.sh @@ -6,13 +6,18 @@ ITERATIONS=1000 MINUTE_INTERVAL=30 MINUTES_SINCE_START=0 +WORLD=test/world +BACKUP_DIR=test/backups +mkdir -p $WORLD +echo "hello there" > "$WORLD/content.txt" + if [[ $1 != "" ]]; then ITERATIONS=$1 fi -for (( c=1; c<=$ITERATIONS; c++ )); do +for (( c=1; c<=ITERATIONS; c++ )); do TIMESTAMP=$(( START_TIMESTAMP + MINUTES_SINCE_START * 60 )) FILE_NAME=$(date -d "@$TIMESTAMP" +%F_%H-%M-%S) - ./backup.sh -q -i /home/nicolas/privatesurvival/world -o /home/nicolas/backups -f $FILE_NAME + ./backup.sh -q -s minecraft -i "$WORLD" -o "$BACKUP_DIR" -f "$FILE_NAME" (( MINUTES_SINCE_START += MINUTE_INTERVAL )) done