Skip to content

Instantly share code, notes, and snippets.

@peaceman
Created March 10, 2011 14:12
Show Gist options
  • Save peaceman/864139 to your computer and use it in GitHub Desktop.
Save peaceman/864139 to your computer and use it in GitHub Desktop.
backup scripts with additional functionality for map generation
#!/bin/bash
#########################################################################
# Backup script for minecraft servers with SCP to another site. #
# Stops and starts specified minecraft server for 100% backups. #
# Supports multiple minecraft servers on the same machine #
# #
# Author #
# Pierre Christoffersen, www.nahaz.se #
# Feel free to redistribute, change or improve, but leave original #
# authors and contributers in comments. #
# http://github.com/Nahaz/Minecraft-Backup-Bash-Script #
# #
# Exitcodes: #
# 0=Completed with no errors #
# 1=Backupd done, server not restarted #
# 2=Failed #
# #
# Variables used: #
# #
# Minecraft-server related: #
# MCDIR=/Dir/to/minecraft/server #
# MCSRV=Name of server.jar used #
# JXMS=512M #Amount of minimum ram for JVM #
# JXMX=3072M #Amount of maximum ram for JVM #
# GUI=nogui #nogui, don't change, only a var for future purposes #
# WORLDNAME=Name of minecraft world #
# SCREEN=Screen name minecraft server is running in #
# #
# Server restart/stop timer and message #
# TIME=60 #Countdown in seconds to shutdown server #
# MSG="Server restarting in "$TIME" seconds, back in a minute!" #
# TRIES=3 #Number of tries to start/stop server before giving up #
# #
# Temporary directory and remote site for backup #
# TMPDIR=/dir/to/tmp #
# BCKSRV=HOSTNAME #Hostname of backupserver #
# BCKDIR=/dir/on/backupserver/to/store/in #
# #
# Don't change these unless you understand what you're doing #
# LOG=$TMP/mc.$WORLDNAME.fullbck.log #
# OF=/tmp/$FILE #
# BUDIR=$MCDIR/$WORLDNAME #
# FILE=$WORLDNAME.$TIMESTAMP.fullbck.tar.gz #
# TIMESTAMP=$(date +%y%m%d.%T) #
# LOGSTAMP=$(date +%y%m%d\ %T) #
#########################################################################
#Minecraft properties
MCDIR=/home/killa/minecraft_new
MCSRV=Minecraft_Mod.jar
JXMS=1024M
JXMX=2048M
GUI=nogui
WORLDNAME=world
SCREEN=MINECRAFT-SERVER
#Map properties
MAPGENDIR=/home/killa/c10t-1.5
MAPGENOPTS="-M1536"
MAPGENTARGETHOST=vlin1.nc23.de
MAPGENTARGETUSER=killa
MAPGENTARGETDIR=/var/www/vhosts/killa/public/minecraft/map
MAPGENTARGETKEY=/home/killa/.ssh/id_rsa
#Restart properties
TIME=15
MSG="Server restarting in "$TIME" seconds, back in a minute!"
TRIES=3
#Backup vars
TMPDIR=/tmp
BCKSRV=googlerocks.dyndns.org
BCKSRVUSR=mcbck-killa
BCKDIR=
#no need to change these
TIMESTAMP=$(date +%y-%m-%d.%H-%M-%S)
LOGSTAMP=$(date +%y%m%d\ %T)
LOGFILE=$TMPDIR/mc.$WORLDNAME.fullbck.log
BUDIR=$MCDIR
FILE=$WORLDNAME.$TIMESTAMP.fullbck.tar.gz
OF=$TMPDIR/$FILE
#nifty functions, don't edit anything below
#Check if minecraft server is running, ONLINE == 1 if offline, ONLINE == 2 if running
function srv_check () {
CMD="ps ux | grep \"java -Xms$JXMS -Xmx$JXMX -jar $MCSRV $GUI\" | wc -l"
ONLINE=$(ps ux | grep "java -Xms$JXMS -Xmx$JXMX -jar $MCSRV $GUI" | wc -l)
echo "online state " $ONLINE
echo "proofed with $CMD"
}
function log () {
echo "[${LOGSTAMP}] ${@}" >> $LOGFILE
}
#Kill minecraft server, but post $MSG to server $TIME before shutdown and warn 5 seconds before shutdown. If "stop" don't work, kill $PID.
function kill_mc() {
screen -S $SCREEN -p 0 -X stuff "`printf "say $MSG\r"`"; sleep $TIME
screen -S $SCREEN -p 0 -X stuff "`printf "say Going down in 10 seconds! Saving world...\r"`"
screen -S $SCREEN -p 0 -X stuff "`printf "save-all\r"`"; sleep 5
screen -S $SCREEN -p 0 -X stuff "`printf "stop\r"`"; sleep 5
srv_check
if [ $ONLINE == 1 ]; then
log "Minecraft server shutdown successfully."
else
log "Minecraft server did NOT shutdown, will try with force."
local PID=$(ps -e | grep "java -Xms$JXMS -Xmx$JXMX -jar $MCSRV $GUI" | grep -v grep | awk '{print $1;}')
local STOP=$TRIES
while [[ $STOP -gt 0 && $ONLINE == 2 ]]; do
log "Try #${STOP} of stopping minecraft server."
kill $PID
srv_check
STOP=$(($STOP-1))
done
if [ $STOP == 0 ]; then
log "Could not kill minecraft server, exiting"
exit 2
else
log "Killed minecraft server after ${STOP} number of tries, proceeding with full backup."
fi
fi
}
#Start minecraft server with $PARAMS
function start_mc() {
function java_start() {
screen -S $SCREEN -p 0 -X stuff "`printf "cd $MCDIR\r"`"; sleep 1
screen -S $SCREEN -p 0 -X stuff "`printf "java -Xms$JXMS -Xmx$JXMX -jar $MCSRV $GUI\r"`"; sleep 3
}
local PARAMS="screen -AmdS $SCREEN java -Xms$JXMS -Xmx$JXMX -jar $MCSRV $GUI"
java_start
srv_check
if [ $ONLINE == 2 ]; then
log "Server started successfully with ${PARAMS}."
else
log "Server did not start, trying again."
local START=0
local SCREXIST=$(ps ux | grep "SCREEN -AmdS $SCREEN" | wc -l)
while [[ $START -lt 3 && $ONLINE == 1 ]]; do
log "Try #"$START" of starting minecraft server."
SCREXIST=$(ps ux | grep "SCREEN -AmdS $SCREEN" | wc -l)
if [ $SCREXIST == 1 ]; then
log "Screen session not found, starting screen with -AmdS ${SCREEN}."
screen -AmdS $SCREEN; sleep 1
java_start
else
java_start
fi
srv_check
START=$(($START+1))
done
if [ $START == 3 ]; then
log "Server did not start after ${START} number of tries, exiting."
exit 1
else
log "Server started after ${START} number of tries with ${PARAMS}"
exit 0
fi
fi
}
function create_map() {
log "Start create map"
cd $MAPGENDIR
CMD="./google-api.sh $TMPDIR/$WORLDNAME.$TIMESTAMP/$WORLDNAME $TMPDIR/$WORLDNAME.$TIMESTAMP.MAPGEN $MAPGENOPTS"
echo $CMD
$CMD
tar -czfv - $TMPDIR/$WORLDNAME.$TIMESTAMP.MAPGEN/* | ssh -i $MAPGENTARGETKEY ${MAPGENTARGETUSER}@${MAPGENTARGETHOST} "tar --overwrite -C ${MAPGENTARGETDIR} -xvzf -"
}
function run_backup() {
#Backup dir, output to $LOG
mkdir $TMPDIR/$WORLDNAME.$TIMESTAMP
cp -rf $BUDIR/* $TMPDIR/$WORLDNAME.$TIMESTAMP
log "Proceeding to start server..."
start_mc
create_map
tar -czf $OF $TMPDIR/$WORLDNAME.$TIMESTAMP
if [ $? == 0 ]; then
log "TAR of ${BUDIR} to ${OF} was successful."
elif [ $? == 1 ]; then
log "TAR of ${BUDIR} to ${OF} was successful, but backup is not 100% of ${BUDIR}, most likely because it was changed during reading."
else
log "TAR of ${BUDIR} to ${OF} was NOT successful, reason: ${?} FATAL ERROR."
fi
rm -rf $TMPDIR/$WORLDNAME.$TIMESTAMP
#SCP backup to $BCKSRV, output to $LOG
scp -i ~/.ssh/id_rsa $OF $BCKSRVUSR@$BCKSRV:$BCKDIR
if [ $? == 0 ]; then
log "SCP of ${OF} to ${BCKSRV} was successful."
else
log "SCP of ${OF} to ${BCKSRV} was NOT successful, reason: ${?}:Some error ocurred."
fi
}
#Is minecraft server running? yes - stop then continue, no - continue
log "Beginning full backup of ${BUDIR}"
srv_check
if [ $ONLINE == 2 ]; then
kill_mc
if [ $ONLINE == 1 ]; then
run_backup
fi
else
run_backup
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment