You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.3 KiB
85 lines
2.3 KiB
#!/bin/bash
|
|
|
|
if [[ -z $1 ]]; then
|
|
echo "Usage: $0 backup_dir"
|
|
exit 2
|
|
fi
|
|
backup_dir=$1
|
|
|
|
# get name of world loaded into server
|
|
world=$(grep ^level-name= "/home/mcadmin/server.properties" | cut -d = -f 2- -s)
|
|
# check if world name is empty
|
|
if [[ -z $world ]]; then
|
|
echo "Error: world-name field seems to be empty. Check server.properties file."
|
|
exit 1
|
|
fi
|
|
worlds_dir="/home/mcadmin/worlds"
|
|
if [[ ! -d "$worlds_dir/$world" ]]; then
|
|
echo "Error: world \"$world\" not found in $worlds_dir. Check server.properties file as well."
|
|
exit 1
|
|
fi
|
|
|
|
# Check if backup dir already exists
|
|
if [[ -e "$backup_dir" ]]; then
|
|
echo "Error: backup dir already exists. Delete directory before proceeding or use another directory name."
|
|
exit 1
|
|
fi
|
|
echo "Creating directory \"$backup_dir\"..."
|
|
mkdir -p "$backup_dir"
|
|
cd "$backup_dir"
|
|
|
|
# Check if server is active
|
|
status=$(systemctl show mcbe -p ActiveState --value)
|
|
# if service is not active, just rsync folder directly.
|
|
if [[ $status != active ]]; then
|
|
echo "Server is inactive: copying files directly..."
|
|
rsync -a "$worlds_dir/" ./
|
|
exit
|
|
fi
|
|
|
|
# prepare backup of active world
|
|
echo "Server is active: running save hold command..."
|
|
status=$(mcbe-exec save hold)
|
|
if [[ $status == "The command is already running" ]]; then
|
|
echo "Error: a save seems to be currently in progress. Did the previous save run correctly?"
|
|
mcbe-exec save resume
|
|
exit 1
|
|
fi
|
|
timeout=0
|
|
buffer=$(mcbe-exec save query)
|
|
until echo "$buffer" | grep -q 'Data saved'; do
|
|
# 1 minute timeout
|
|
if [[ "$timeout" -eq 60 ]]; then
|
|
echo "Error: save timed out."
|
|
mcbe-exec save resume
|
|
exit 1
|
|
fi
|
|
sleep 1
|
|
timeout=$(( ++timeout ))
|
|
buffer=$(mcbe-exec save query)
|
|
done
|
|
files=$(echo "$buffer" | grep -Eo "${world}[^:]+:[0-9]+")
|
|
|
|
# copy files for active world
|
|
echo "Copying active world..."
|
|
echo "$files" | while read -r line; do
|
|
file=${line%:*}
|
|
size=${line#*:}
|
|
mkdir -p "$(dirname "$file")"
|
|
rsync -a "$worlds_dir/$file" "$file"
|
|
truncate --size="$size" "$file"
|
|
done
|
|
|
|
# tell minecraft it can resume
|
|
mcbe-exec save resume
|
|
|
|
# copy inactive worlds
|
|
echo "Copying inactive worlds..."
|
|
ls -w 1 "$worlds_dir" | grep -v -E "^$world\$" | while read -r line; do
|
|
rsync -a "$worlds_dir/$line" ./
|
|
done
|
|
|
|
# make sure everything is owned by worlds owner
|
|
chown -R "$(stat -c %U:%G $worlds_dir)" ./
|
|
|
|
echo "Done!"
|
|
|