#!/bin/bash do_backup() { mcbe-stop $1 "Shutting down server for scheduled daily backup." rsyn /home/mcadmin/worlds/ /vol/worlds/ systemctl start mcbe rm /home/mcadmin/backup.lock } # Check if server is started status=$(systemctl show "$service" -p ActiveState --value) # if service is not active, do backup immediately and exit if [[ $status == inactive ]]; then rsyn /home/mcadmin/worlds/ /vol/worlds/ rm /home/mcadmin/backup.lock exit 0 fi # Check if any players present present=$(mcbe-exec list | cut -d ' ' -f 3 | cut -d '/' -f 1) # if no players are present, do backup immediately and exit if [[ $present == 0 ]]; then do_backup 0 exit 0 fi cd /home/mcadmin # create backup.attempts file if it doesn't exist if [[ ! -e backup.attempts ]]; then echo "0" > backup.attempts fi # read backup.wait and add one to it attempts=$(( $(cat backup.attempts) + 1 )) # if 12 or more attempts have already been made, go ahead and force a shutdown anyways if [[ $attempts -ge 12 ]]; then do_backup 10 # otherwise, schedule another attempt in an hour else systemd-run --on-active=1h $0 fi