Add log levels to Minecraft backup script
This commit is contained in:
		@@ -7,18 +7,50 @@ SCRATCH=$(mktemp -p /var/tmp -d minecraft.XXXXXXXXXX)
 | 
				
			|||||||
NICE=15
 | 
					NICE=15
 | 
				
			||||||
TAR=minecraft-$(date +%Y-%m-%d).tar
 | 
					TAR=minecraft-$(date +%Y-%m-%d).tar
 | 
				
			||||||
WAIT=30
 | 
					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() {
 | 
					finish() {
 | 
				
			||||||
    printf "Cleaning up ...\n"
 | 
					    log info "Cleaning up"
 | 
				
			||||||
    rm -rf "$SCRATCH"
 | 
					    rm -rf "$SCRATCH"
 | 
				
			||||||
    if ! systemctl -q is-active "$SERVICE"; then
 | 
					    if ! systemctl -q is-active "$SERVICE"; then
 | 
				
			||||||
        start_server
 | 
					        start_server $SERVICE
 | 
				
			||||||
    fi
 | 
					    fi
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
trap finish EXIT
 | 
					trap finish EXIT
 | 
				
			||||||
 | 
					
 | 
				
			||||||
error_exit() {
 | 
					error_exit() {
 | 
				
			||||||
    printf "%s\n" "$1"
 | 
					    log crit "$1"
 | 
				
			||||||
    exit 1
 | 
					    exit 1
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -27,14 +59,14 @@ stop_server() {
 | 
				
			|||||||
    local attempts="${2:-$WAIT}"
 | 
					    local attempts="${2:-$WAIT}"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if ! systemctl -q is-active "$unit"; then
 | 
					    if ! systemctl -q is-active "$unit"; then
 | 
				
			||||||
        printf "%s is already stopped\n" "$unit"
 | 
					        log warning "%s is already stopped\n" "$unit"
 | 
				
			||||||
        return 0
 | 
					        return 0
 | 
				
			||||||
    fi
 | 
					    fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    systemctl -q stop "$unit"
 | 
					    systemctl -q stop "$unit"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    while systemctl -q is-active "$unit"; do
 | 
					    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--))
 | 
					        ((attempts--))
 | 
				
			||||||
        if ((attempts == 0)); then
 | 
					        if ((attempts == 0)); then
 | 
				
			||||||
            return 1
 | 
					            return 1
 | 
				
			||||||
@@ -50,14 +82,14 @@ start_server() {
 | 
				
			|||||||
    local attempts="${2:-$WAIT}"
 | 
					    local attempts="${2:-$WAIT}"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if systemctl -q is-active "$unit"; then
 | 
					    if systemctl -q is-active "$unit"; then
 | 
				
			||||||
        printf "%s is already started\n" "$unit"
 | 
					        log warning "%s is already started\n" "$unit"
 | 
				
			||||||
        return 0
 | 
					        return 0
 | 
				
			||||||
    fi
 | 
					    fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    systemctl -q start "$unit"
 | 
					    systemctl -q start "$unit"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    while ! systemctl -q is-active "$unit"; do
 | 
					    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--))
 | 
					        ((attempts--))
 | 
				
			||||||
        if ((attempts == 0)); then
 | 
					        if ((attempts == 0)); then
 | 
				
			||||||
            return 1
 | 
					            return 1
 | 
				
			||||||
@@ -74,7 +106,7 @@ open_files() {
 | 
				
			|||||||
    local attempts="${2:-$WAIT}"
 | 
					    local attempts="${2:-$WAIT}"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    while (($(lsof +D "$dir" | wc -l) > 0)); do
 | 
					    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--))
 | 
					        ((attempts--))
 | 
				
			||||||
        if ((attempts == 0)); then
 | 
					        if ((attempts == 0)); then
 | 
				
			||||||
            return 1
 | 
					            return 1
 | 
				
			||||||
@@ -87,26 +119,32 @@ open_files() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
main() {
 | 
					main() {
 | 
				
			||||||
    if ! stop_server; then
 | 
					    log info "Stopping %s service" $SERVICE
 | 
				
			||||||
 | 
					    if ! stop_server $SERVICE; then
 | 
				
			||||||
        error_exit "Failed to stop $SERVICE"
 | 
					        error_exit "Failed to stop $SERVICE"
 | 
				
			||||||
    fi
 | 
					    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"
 | 
					        error_exit "Open files exist in $VAR_DIR"
 | 
				
			||||||
    fi
 | 
					    fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    log info "Starting backup"
 | 
				
			||||||
    if ! tar -cf "$SCRATCH/$TAR" -C "$VAR_DIR" world; then
 | 
					    if ! tar -cf "$SCRATCH/$TAR" -C "$VAR_DIR" world; then
 | 
				
			||||||
        error_exit "Archive failed"
 | 
					        error_exit "Archive failed"
 | 
				
			||||||
    fi
 | 
					    fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if ! start_server; then
 | 
					    log info "Starting %s service" $SERVICE
 | 
				
			||||||
 | 
					    if ! start_server $SERVICE; then
 | 
				
			||||||
        error_exit "Failed to start $SERVICE"
 | 
					        error_exit "Failed to start $SERVICE"
 | 
				
			||||||
    fi
 | 
					    fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    log info "Compressing $TAR"
 | 
				
			||||||
    if ! nice -n "$NICE" xz -9 "$SCRATCH/$TAR"; then
 | 
					    if ! nice -n "$NICE" xz -9 "$SCRATCH/$TAR"; then
 | 
				
			||||||
        error_exit "Failed to compress $SCRATCH/$TAR"
 | 
					        error_exit "Failed to compress $SCRATCH/$TAR"
 | 
				
			||||||
    else
 | 
					    else
 | 
				
			||||||
        mv "$SCRATCH/$TAR.xz" "$BACKUP_DIR"
 | 
					        mv "$SCRATCH/$TAR.xz" "$BACKUP_DIR"
 | 
				
			||||||
 | 
					        log info "Symlinking current archive"
 | 
				
			||||||
        ln -nfs "$BACKUP_DIR/$TAR.xz" "$BACKUP_DIR/minecraft-latest.tar.xz"
 | 
					        ln -nfs "$BACKUP_DIR/$TAR.xz" "$BACKUP_DIR/minecraft-latest.tar.xz"
 | 
				
			||||||
    fi
 | 
					    fi
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user