infrastructure/roles/minecraft/templates/minecraftctl.sh
2020-08-10 01:37:13 +02:00

100 lines
2.5 KiB
Bash

#!/bin/bash
MINECRAFT_PATH="{{ minecraft_data_location }}"
COMMAND=${1:-help}
cd "$MINECRAFT_PATH"
usage() {
cat <<EOF
$0 up
Starts the minecraft server
$0 restart
Restarts the minecraft server
$0 down
Stops the minecraft server
$0 logs
Show the logs of the minecraft server
$0 status
Show the status of all minecraft containers
$0 mod-ls
Lists all installed mod packages in the mods directory of the minecraft server
$0 mod-cp <some mod jar file>
Copies a mod package to the mods directory of the mincraft server
$0 mod-rm <file name from mod-ls output>
Removes a mod package from the mods directory of the minecraft server
$0 world-ls
Lists backed up worlds
$0 world-rm <time reference>
Removes a world backup
$0 world-backup
Creates a backup of the current world
$0 world-restore <time reference>
Restores a world from the backup
$0 world-destroy
Destroys the current world of the server.
Warning: Stop the server before using this.
EOF
}
case "$COMMAND" in
"restart")
docker-compose restart
;;
"up")
docker-compose up -d
;;
"down")
docker-compose down
;;
"logs")
docker-compose logs minecraft
;;
"status")
docker-compose ps
;;
"mod-cp")
if [ ! -e "$OLDPWD/$2" ]; then
echo "file \"$OLDPWD/$2\" not found"
exit 1
fi
cp "$OLDPWD/$2" "$MINECRAFT_PATH/data/mods/"
chown -R root:root "$MINECRAFT_PATH/data/mods/"
;;
"mod-rm")
if ls "$MINECRAFT_PATH/data/mods/" | grep "$2"; then
rm "$MINECRAFT_PATH/data/mods/$2"
fi
;;
"mod-ls")
ls "$MINECRAFT_PATH/data/mods/"
;;
"world-ls")
ls "$MINECRAFT_PATH/worlds/"
;;
"world-rm")
if ls "$MINECRAFT_PATH/worlds/" | grep "$2"; then
rm -Ir "$MINECRAFT_PATH/worlds/$2"
fi
;;
"world-backup")
mkdir "$MINECRAFT_PATH/worlds/$(date +%Y-%m-%dT%H:%M)"
cp -r "$MINECRAFT_PATH/data/"{world,world_the_end,world_nether} "$MINECRAFT_PATH/worlds/$(date +%Y-%m-%dT%H:%M)/"
;;
"world-restore")
if [ -e "$MINECRAFT_PATH/data/world" ]; then
echo "Please destroy the world before restoring"
exit 1
fi
if ls "$MINECRAFT_PATH/worlds/" | grep "$2"; then
cp -Ir "$MINECRAFT_PATH/worlds/$2/*" "$MINECRAFT_PATH/data/"
fi
;;
"world-destroy")
rm -Ir "$MINECRAFT_PATH/data/"{world,world_the_end,world_nether}
;;
*)
usage
;;
esac