ansible/roles/minecraft/templates/recover.sh

58 lines
1.6 KiB
Bash
Raw Normal View History

#! /bin/sh
#
# recover.sh
# Recover a Minecraft world save from our S3 bucket
# Copyright (C) 2020 Vintage Salt <rehashedsalt@cock.li>
#
# Distributed under terms of the MIT license.
#
set -e
# Get to our magic dir
export MINECRAFT_DIR="/var/minecraft/{{ mcname }}"
cd "$MINECRAFT_DIR" || exit 50
# Make sure we have a backup
if ! aws s3 ls "s3://{{ aws_backup_bucket }}/{{ mcname }}/" > /dev/null 2>&1; then
echo "No backups available"
exit 0
fi
# If the world directory exists, we need to ensure that we don't clobber it
if [ -d "world" ]; then
echo "Backing up current world"
2020-06-25 08:56:43 -05:00
tar czf "recover-world-$(date -Iseconds).tar.gz" world --remove-files --force-local
fi
# If it STILL exists, then we have a problem
if [ -d "world" ]; then
echo "World was found to exist after tarring; bailing"
exit 51
fi
# Get our latest good backup
backup="$(aws s3 ls "s3://{{ aws_backup_bucket }}/{{ mcname }}/" | tail -n 1 | awk '{print $4}')"
echo "Restoring backup: $backup"
aws s3 cp "s3://{{ aws_backup_bucket }}/{{ mcname }}/$backup" world.tgz
# Decompress it
tar xzf world.tgz
# Find the world
2020-06-25 08:54:47 -05:00
worlddir="$(find ./var -type d -name "world" | head -n 1)"
# Move it in place
mv "$worlddir" .
# Verify our work
if [ -f "world/level.dat" ]; then
echo "Recovered from backup: $backup"
else
echo "Failed to recover from backup: $backup"
fi
2020-06-25 08:45:39 -05:00
# Remove the var dir, if it exists, because that's what it ends up being named
if [ -d "var" ]; then
# Never use relative paths with rm in a script
# That's heresy
2020-06-25 08:47:56 -05:00
rm -rf "/var/minecraft/{{ mcname }}/var"
2020-06-25 08:45:39 -05:00
fi
2020-06-25 08:57:24 -05:00
# Remove our old world.tgz
if [ -f "world.tgz" ]; then
rm "/var/minecraft/{{ mcname }}/world.tgz"
fi