Skip to content

Instantly share code, notes, and snippets.

@peaceman
Created March 10, 2011 23:06
Show Gist options
  • Save peaceman/865146 to your computer and use it in GitHub Desktop.
Save peaceman/865146 to your computer and use it in GitHub Desktop.
#!/bin/bash
# This script should handle the automatic backup and map generation
# of a Minecraft server.
#
# Steps of the process:
# - Stop the Minecraft server if it runs
# - Copy the whole Minecraft directory to /tmp
# - Start the Minecraft server with the specified parameters
# - Compress and transfer the data to a backuphost
# - Start map generation and transfer the pictures to a webhost
# - Delete the created folders in /tmp
# - Delete backups that are older than 7 days but keep backups from monday, the same for servermaps
# -------------------------------------------------------------------------------------------------
# Path to Minecraft directory
MCPATH=/home/killa/minecraft_new
# Name of minecraft.jar
MCJARNAME=Minecraft_Mod.jar
# Name of the Minecraft world
MCWORLDNAME=world
# Parameters for minecraft
MCPARAMS="-Xms1024M -Xmx2048M nogui"
# Path to temporary folder
TMPFOLDER=/tmp
# Timestamp for filenames etc.
TIMESTAMP=$(date +%y-%m-%d.%H-%M-%S)
# Name of the screen session
SCREENNAME=mc
# Directory which contains the map generation tools
MAPDIR=/home/killa/c10t-1.5
# Parameters for the map generation tools
MAPPARAMS="-M1536"
# Hostname of the webserver which hosts the maps
MAPHOST=vlin2.nc23.de
# Username for login into the webserver
MAPHOSTUSER=killa
# Full path to the ssh key-to-use
MAPHOSTKEY=/home/killa/.ssh/id_rsa
# Full path to the directory which will be used for maps
MAPHOSTPATH=/var/www/vhosts/killa/public/minecraft/map
# Hostname of the backupserver
BACKUPHOST=googlerocks.dyndns.org
# Username for login into the backupserver
BACKUPHOSTUSER=mcbck-killa
# Full path to the ssh key-to-use for the backupserver
BACKUPHOSTKEY=/home/killa/.ssh/id_rsa
# Backup directory
BACKUPDIR=
# Time to wait before the Minecraft server goes down
STOPDELAY=30
# Message to display ingame before the Minecraft server goes down
STOPMSG="The server will be restarted in $STOPDELAY seconds for daily backup and map generation"
# -----------------------------------------------------------------------
ACTDATADIR=$TMPFOLDER/mcbackup_$TIMESTAMP
function serverIsRunning() {
return $(ps ux | grep "java -jar $MCJARNAME $MCPARAMS" | grep -v grep | wc -l)
}
function screenExists() {
return $(ps ux | grep -e ".*SCREEN.*$SCREENNAME.*" | grep -v grep | wc -l)
}
function startServer() {
serverIsRunning()
serverState=$?
screenExists()
screenState=$?
if [ serverState -eq 1 ]; then
echo "Didn't start the server because it was already running"
exit 0
fi
if [ screenState -eq 0 ]; t# - Start the Minecraft server with the specified parametershen
screen -dmS $SCREENNAME
sleep 3
fi
screen -S $SCREENNAME -X stuff "`printf "cd $MCPATH && java -jar $MCJARNAME $MCPARAMS\r"`"
}
function stopServer() {
serverIsRunning()
serverState=$?
screenExists()
screenState=$?
if [ $serverState -eq 0 ]; then
echo "Couldn't stop the server, because no running instance was found"
return
fi
if [ $screenState -eq 0 ]; then
echo "Couldn't stop the server, because no screen with the name $SCREENNAME was found"
return
elif [ $screenState -gt 1 ]; then
echo "Couldn't stop the server, because there are $screenState screens with the name $SCREENNAME"
exit 1
fi
screen -S $SCREENNAME -X stuff "`printf "say $STOPMSG\r"`"
sleep $STOPDELAY
screen -S $SCREENNAME -X stuff "`printf "save-all\rstop\r"`"
retVal=1
tries=0
until [ $retVal -a $tries -le 15 ]; do
serverIsRunning()
retVal=$?
let "tries++"
sleep 1
done
if [ $tries -gt 15 ]; then
echo "The server didnt shutdown after 15 seconds"
exit 1
done
}
function transferToBackupServer() {
tar -cvjf - $ACTDATADIR | ssh -i $BACKUPHOSTKEY $BACKUPHOSTUSER@$BACKUPHOST "cat - > $BACKUPDIR/mcbackup_$TIMESTAMP.tar.bz"
}
function mapGenerationAndTransfer() {
cd $MAPDIR
./google-api.sh $ACTDATADIR/$MCWORLDNAME $ACTDATADIR.MAPGEN $MAPPARAMS
tar -cvf - $ACTDATADIR.MAPGEN | ssh -i $MAPHOSTKEY $MAPHOSTUSER@$MAPHOST "tar -C $MAPHOSTPATH -xvf -"
}
stopServer()
cp -rf $MCPATH $ACTDATADIR
startServer()
transferToBackupServer() &
mapGenerationAndTransfer() &
wait
rm -rf $ACTDATADIR $ACTDATADIR.MAPGEN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment