2019-10-27 23:36:54 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
SERVICE=minecraft.service
|
|
|
|
BACKUP_DIR=/opt/minecraft/backup
|
|
|
|
VAR_DIR=/opt/minecraft/var
|
|
|
|
SCRATCH=$(mktemp -p /var/tmp -d minecraft.XXXXXXXXXX)
|
|
|
|
NICE=15
|
|
|
|
TAR=minecraft-$(date +%Y-%m-%d).tar
|
|
|
|
WAIT=30
|
2019-11-29 21:00:53 +00:00
|
|
|
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
|
|
|
|
}
|
2019-10-27 23:36:54 +00:00
|
|
|
|
|
|
|
finish() {
|
2019-11-29 21:00:53 +00:00
|
|
|
log info "Cleaning up"
|
2019-10-27 23:36:54 +00:00
|
|
|
rm -rf "$SCRATCH"
|
|
|
|
if ! systemctl -q is-active "$SERVICE"; then
|
2019-11-29 21:00:53 +00:00
|
|
|
start_server $SERVICE
|
2019-10-27 23:36:54 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
trap finish EXIT
|
|
|
|
|
|
|
|
error_exit() {
|
2019-11-29 21:00:53 +00:00
|
|
|
log crit "$1"
|
2019-10-27 23:36:54 +00:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
stop_server() {
|
|
|
|
local unit="${1:-$SERVICE}"
|
|
|
|
local attempts="${2:-$WAIT}"
|
|
|
|
|
|
|
|
if ! systemctl -q is-active "$unit"; then
|
2019-11-29 21:00:53 +00:00
|
|
|
log warning "%s is already stopped\n" "$unit"
|
2019-10-27 23:36:54 +00:00
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
systemctl -q stop "$unit"
|
|
|
|
|
|
|
|
while systemctl -q is-active "$unit"; do
|
2019-11-29 21:00:53 +00:00
|
|
|
log warning "Waiting for %s to stop ... (%s)\n" "$unit" "$attempts"
|
2019-10-27 23:36:54 +00:00
|
|
|
((attempts--))
|
|
|
|
if ((attempts == 0)); then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
start_server() {
|
|
|
|
local unit="${1:-$SERVICE}"
|
|
|
|
local attempts="${2:-$WAIT}"
|
|
|
|
|
|
|
|
if systemctl -q is-active "$unit"; then
|
2019-11-29 21:00:53 +00:00
|
|
|
log warning "%s is already started\n" "$unit"
|
2019-10-27 23:36:54 +00:00
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
systemctl -q start "$unit"
|
|
|
|
|
|
|
|
while ! systemctl -q is-active "$unit"; do
|
2019-11-29 21:00:53 +00:00
|
|
|
log warning "Waiting for %s to start ... (%d)\n" "$unit" "$attempts"
|
2019-10-27 23:36:54 +00:00
|
|
|
((attempts--))
|
|
|
|
if ((attempts == 0)); then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
open_files() {
|
|
|
|
local dir=${1-$VAR_DIR}
|
|
|
|
local attempts="${2:-$WAIT}"
|
|
|
|
|
|
|
|
while (($(lsof +D "$dir" | wc -l) > 0)); do
|
2019-11-29 21:00:53 +00:00
|
|
|
log warning "Waiting for open files ... (%d)\n" "$attempts"
|
2019-10-27 23:36:54 +00:00
|
|
|
((attempts--))
|
|
|
|
if ((attempts == 0)); then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
main() {
|
2019-11-29 21:05:35 +00:00
|
|
|
log info "Stopping %s" $SERVICE
|
2019-11-29 21:00:53 +00:00
|
|
|
if ! stop_server $SERVICE; then
|
2019-10-27 23:36:54 +00:00
|
|
|
error_exit "Failed to stop $SERVICE"
|
|
|
|
fi
|
|
|
|
|
2019-11-29 21:00:53 +00:00
|
|
|
log info "Checking for open files"
|
|
|
|
if ! open_files $VAR_DIR; then
|
2019-10-27 23:36:54 +00:00
|
|
|
error_exit "Open files exist in $VAR_DIR"
|
|
|
|
fi
|
|
|
|
|
2019-11-29 21:00:53 +00:00
|
|
|
log info "Starting backup"
|
2019-10-27 23:36:54 +00:00
|
|
|
if ! tar -cf "$SCRATCH/$TAR" -C "$VAR_DIR" world; then
|
|
|
|
error_exit "Archive failed"
|
|
|
|
fi
|
|
|
|
|
2019-11-29 21:05:35 +00:00
|
|
|
log info "Starting %s" $SERVICE
|
2019-11-29 21:00:53 +00:00
|
|
|
if ! start_server $SERVICE; then
|
2019-10-27 23:36:54 +00:00
|
|
|
error_exit "Failed to start $SERVICE"
|
|
|
|
fi
|
|
|
|
|
2019-11-29 21:00:53 +00:00
|
|
|
log info "Compressing $TAR"
|
2019-10-27 23:36:54 +00:00
|
|
|
if ! nice -n "$NICE" xz -9 "$SCRATCH/$TAR"; then
|
|
|
|
error_exit "Failed to compress $SCRATCH/$TAR"
|
|
|
|
else
|
|
|
|
mv "$SCRATCH/$TAR.xz" "$BACKUP_DIR"
|
2019-11-29 21:00:53 +00:00
|
|
|
log info "Symlinking current archive"
|
2019-10-27 23:36:54 +00:00
|
|
|
ln -nfs "$BACKUP_DIR/$TAR.xz" "$BACKUP_DIR/minecraft-latest.tar.xz"
|
|
|
|
fi
|
2019-11-29 21:02:40 +00:00
|
|
|
|
|
|
|
log info "Backup complete"
|
2019-10-27 23:36:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
main
|