
6 changed files with 35 additions and 37 deletions
@ -1,44 +1,40 @@ |
|||
#!/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.attempts |
|||
echo "Shutting down server for scheduled daily backup." |
|||
mcbe-shutdown 10 "Shutting down server for scheduled daily backup." |
|||
rsync -vaSH /home/mcadmin/worlds/ /vol/worlds/ |
|||
sleep 5 |
|||
echo "Restarting server after backup." |
|||
service mcbe start |
|||
} |
|||
|
|||
# Check if server is started |
|||
status=$(systemctl show "$service" -p ActiveState --value) |
|||
status=$(systemctl show mcbe -p ActiveState --value) |
|||
# Check if any players present |
|||
players=$(mcbe-exec list | cut -d ' ' -f 3 | cut -d '/' -f 1) |
|||
# check when mcbe-backup was last triggered |
|||
last=$(date -d "$(systemctl show mcbe-backup.timer -p LastTriggerUSec --value)" +%s) |
|||
# get current time |
|||
today=$(date +%s) |
|||
|
|||
# if service is not active, do backup immediately and exit |
|||
if [[ $status == inactive ]]; then |
|||
rsyn /home/mcadmin/worlds/ /vol/worlds/ |
|||
rm /home/mcadmin/backup.attempts |
|||
exit 0 |
|||
fi |
|||
echo "Service is inactive: copying worlds immediately without restarting service." |
|||
rsync -vaSH /home/mcadmin/worlds/ /vol/worlds/ |
|||
|
|||
# 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 |
|||
elif [[ $players == 0 ]]; then |
|||
echo "No players detected: running backup immediately." |
|||
do_backup |
|||
|
|||
# read backup.wait and add one to it |
|||
attempts=$(( $(cat backup.attempts) + 1 )) |
|||
# if 12 or more hours have passed since backup was triggered, go ahead and force a shutdown anyways |
|||
elif [[ $(( $today - $last )) -ge 43200 ]]; then |
|||
echo "Backup was triggered more than 12 hours ago: forcing backup now." |
|||
do_backup |
|||
|
|||
# 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 |
|||
echo $attempts > backup.attempts |
|||
echo "Players present in server: rescheduling backup to one hour from now." |
|||
systemd-run --on-active=1h $0 |
|||
fi |
|||
|
@ -1,9 +1,11 @@ |
|||
[Unit] |
|||
After=mcbe.service |
|||
Description=Back up Minecraft Bedrock Edition server |
|||
Description=Backup Minecraft Bedrock Edition Server |
|||
Requisite=mcbe.service |
|||
|
|||
[Service] |
|||
ExecStart=/usr/local/bin/mcbe-backup |
|||
StandardOutput=journal |
|||
StandardError=journal |
|||
KillMode=none |
|||
Type=oneshot |
Loading…
Reference in new issue