
6 changed files with 35 additions and 37 deletions
@ -1,44 +1,40 @@ |
|||||
#!/bin/bash |
#!/bin/bash |
||||
|
|
||||
do_backup() { |
do_backup() { |
||||
mcbe-stop $1 "Shutting down server for scheduled daily backup." |
echo "Shutting down server for scheduled daily backup." |
||||
rsyn /home/mcadmin/worlds/ /vol/worlds/ |
mcbe-shutdown 10 "Shutting down server for scheduled daily backup." |
||||
systemctl start mcbe |
rsync -vaSH /home/mcadmin/worlds/ /vol/worlds/ |
||||
rm /home/mcadmin/backup.attempts |
sleep 5 |
||||
|
echo "Restarting server after backup." |
||||
|
service mcbe start |
||||
} |
} |
||||
|
|
||||
# Check if server is started |
# 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 service is not active, do backup immediately and exit |
||||
if [[ $status == inactive ]]; then |
if [[ $status == inactive ]]; then |
||||
rsyn /home/mcadmin/worlds/ /vol/worlds/ |
echo "Service is inactive: copying worlds immediately without restarting service." |
||||
rm /home/mcadmin/backup.attempts |
rsync -vaSH /home/mcadmin/worlds/ /vol/worlds/ |
||||
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 no players are present, do backup immediately and exit |
||||
if [[ $present == 0 ]]; then |
elif [[ $players == 0 ]]; then |
||||
do_backup 0 |
echo "No players detected: running backup immediately." |
||||
exit 0 |
do_backup |
||||
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 |
# if 12 or more hours have passed since backup was triggered, go ahead and force a shutdown anyways |
||||
attempts=$(( $(cat backup.attempts) + 1 )) |
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 |
# otherwise, schedule another attempt in an hour |
||||
else |
else |
||||
echo $attempts > backup.attempts |
echo "Players present in server: rescheduling backup to one hour from now." |
||||
systemd-run --on-active=1h $0 |
systemd-run --on-active=1h $0 |
||||
fi |
fi |
||||
|
@ -1,9 +1,11 @@ |
|||||
[Unit] |
[Unit] |
||||
After=mcbe.service |
After=mcbe.service |
||||
Description=Back up Minecraft Bedrock Edition server |
Description=Backup Minecraft Bedrock Edition Server |
||||
Requisite=mcbe.service |
Requisite=mcbe.service |
||||
|
|
||||
[Service] |
[Service] |
||||
ExecStart=/usr/local/bin/mcbe-backup |
ExecStart=/usr/local/bin/mcbe-backup |
||||
|
StandardOutput=journal |
||||
|
StandardError=journal |
||||
KillMode=none |
KillMode=none |
||||
Type=oneshot |
Type=oneshot |
Loading…
Reference in new issue