Skip to content

Instantly share code, notes, and snippets.

@reillysiemens
Created January 21, 2018 01:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reillysiemens/fa780306365f52a16722e52ebb154fa4 to your computer and use it in GitHub Desktop.
Save reillysiemens/fa780306365f52a16722e52ebb154fa4 to your computer and use it in GitHub Desktop.
Minecraft Backup
#!/bin/sh
: ${MINECRAFT_WORLD="survival"}
src="/srv/${MINECRAFT_WORLD}/world"
dst="/var/games/minecraft/${MINECRAFT_WORLD}.txz"
send_command() {
world=$1
cmd=$2
# Send the command to the tmux session.
/usr/local/bin/tmux send-keys -t ${world} "${cmd}" C-m
}
save_off() {
world=$1
send_command ${world} "save-off"
}
save_on() {
world=$1
send_command ${world} "save-on"
}
save_all() {
world=$1
send_command ${world} "save-all"
}
archive() {
src=$1
dst=$2
tmp="/tmp/$(basename ${dst})"
# Create the archive at a temporary location first to minimize
# inconsistencies stemming from access when the new archive is being
# written.
/usr/bin/tar -a -cf ${tmp} ${src}
/bin/mv ${tmp} ${dst}
}
# Turn off world auto-saving.
save_off ${MINECRAFT_WORLD}
# Save the world.
save_all ${MINECRAFT_WORLD}
# Archive the world.
archive ${src} ${dst}
# Turn on world auto-saving.
save_on ${MINECRAFT_WORLD}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment