Skip to content

Instantly share code, notes, and snippets.

@celestefox
Last active December 8, 2018 01:40
Show Gist options
  • Save celestefox/cbf2d8981a4f8f9213db50b5a88e5c02 to your computer and use it in GitHub Desktop.
Save celestefox/cbf2d8981a4f8f9213db50b5a88e5c02 to your computer and use it in GitHub Desktop.
Script to handle out-of-minecraft save backups, designed for MultiMC
#! /usr/bin/env bash
#
# Automatic world backups, outside of minecraft.
# This is expected to be run as a "post-exit command" from MultiMC.
# (Or pre-launch, which might make more sense if you use external tools?)
set -o errexit -o nounset -o pipefail
IFS=$'\n'
main() {
# Nicer and a bit more explanatory names for the variables we're given
local instance_name="${INST_NAME}"
local instance_id="${INST_ID}"
local instance_directory="${INST_DIR}"
local instance_minecraft_directory="${INST_MC_DIR}"
# probably won't need these two, but for completeness's sake
local instance_java_binary="${INST_JAVA}"
local instance_java_args="${INST_JAVA_ARGS}"
local backup_directory="${instance_directory}/automated_backups"
# ensure the backup directory exists.
if ! mkdir -p "${backup_directory}" ; then
echo "Unable to ensure backup directory ${backup_directory} exists!" >&2
exit 2
fi
# Find any saves to backup, ignoring folders called NEI or DragonAPI, which
# are created by the mods with the same names to hold data (grumpy face).
# `set (+-)e` pair needed because I want to check if find fails.
local saves
set +e
saves="$(find -H "${instance_minecraft_directory}/saves" -xdev -mindepth 1 \
-maxdepth 1 -type d \! \( -name NEI -o -name DragonAPI \))"
if [[ "$?" -ne 0 ]]; then
echo "Finding saves to backup failed!" >&2
exit 4
fi
set -e
if [[ -z "${saves}" ]]; then
echo "Found no saves to backup!"
# Not a failure - no work to do, which is totally possible, especially if
# being used as a pre-launch hook, instead of post-exit.
exit 0
fi
local failed=0
local save
for save in "${saves}"; do
local save_name="$(basename ${save})"
local save_backup_path="${backup_directory}/${save_name}-$(date +%s).zip"
if ! zip -r "${save_backup_path}" "${save}"; then
# Again, not really a *failure* - just, like, a major warning. We
# still want to try to backup everything else.
echo "Unable to backup save ${save_name}!" >&2
failed=1
fi
done
if [[ "${failed}" -eq 1 ]]; then
echo "At least one backup failed!" >&2
exit 8
fi
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment