</s>#!/bin/bash
# original author : Relliktsohg
# Huge thanks to Maine for his incremental backup
# THanks to endofzero for his improved update routine
# T4b added some functions and improved others
# Configuration
MC_PATH=/path/to/the/minecraft/folder
SERVERMOD=0
RUNECRAFT=0
SCREEN_NAME="minecraft"
MEMALOC=128
DISPLAY_ON_LAUNCH=0
WORLD_NAME="WorldName"
BKUP_PATH=/path/to/the/minecraft/backup/folder
BKUP_DAYS_INCR=2
BKUP_DAYS_FULL=5
BACKUP_FULL_LINK=${BKUP_PATH}/${WORLD_NAME}_full.tgz
BACKUP_INCR_LINK=${BKUP_PATH}/${WORLD_NAME}_incr.tgz
CARTO_PATH=$MC_PATH/c10t-HEAD/
MAPS_PATH=/path/to/the/minecraft/map/pics/folder
LOG_TDIR=/path/to/the/minecraft/logs/folder
LOGS_DAYS=7
# End of configuration
SERVERHELP='Usage : minecraft <status | start [force] | stop | restart [warn] | logs [clean] | backup [clean] | cartography | update | display | cmd <command>>
Possible values for "command" are:
help or ? shows all known commands
kick <player> removes a player from the server
ban <player> bans a player from the server
pardon <player> pardons a banned player so that they can connect again
ban-ip <ip> bans an IP address from the server
pardon-ip <ip> pardons a banned IP address so that they can connect again
op <player> turns a player into an op
deop <player> removes op status from a player
tp <player1> <player2> moves one player to the same location as another player
give <player> <id> [num] gives a player a resource
tell <player> <message> sends a private message to a player
stop gracefully stops the server
save-all forces a server-wide level save
save-off disables terrain saving (useful for backup scripts)
save-on re-enables terrain saving
list lists all currently connected players
say <message> broadcasts a message to all players'
if [ $SERVERMOD -eq 1 ]
then
if [ -e $MC_PATH/logs/*.log.lck ]
then
ONLINE=1
else
ONLINE=0
fi
else
if [ -e $MC_PATH/server.log.lck ]
then
# ps -e | grep java | wc -l
ONLINE=1
else
ONLINE=0
fi
fi
display() {
screen -R $SCREEN_NAME
}
server_launch() {
echo "Launching minecraft server..."
if [ $SERVERMOD -eq 1 ]
then
cd $MC_PATH; screen -L -m -d -S $SCREEN_NAME java -Xmx${MEMALOC}M -Xms${MEMALOC}M -Djava.net.preferIPv4Stack=true -jar Minecraft_Mod.jar nogui; sleep 1
else
cd $MC_PATH; screen -L -m -d -S $SCREEN_NAME java -Xmx${MEMALOC}M -Xms${MEMALOC}M -Djava.net.preferIPv4Stack=true -jar minecraft_server.jar nogui; sleep 1
fi
}
server_stop() {
echo "Stopping minecraft server..."
screen -S $SCREEN_NAME -p 0 -X stuff "`printf "stop.\r"`"; sleep 5
}
if [ $# -gt 0 ]
then
case $1 in
#################################################################
"status")
if [ $ONLINE -eq 1 ]
then
echo "Minecraft server seems ONLINE."
echo "The following players are online: $( screen -S $SCREEN_NAME -p 0 -X stuff "`printf "list\r"`" && sleep 1 && tail server.log | egrep -A 1 '.+list' | tail -n 1 | sed -e 's/.*: //g' )"
else
echo "Minecraft server seems OFFLINE."
fi
;;
#################################################################
"start")
if [ $ONLINE -eq 1 ]
then
echo "Server seems to be already running !"
case $2 in
"force")
kill `ps -e | grep java | cut -d " " -f 1`
rm -fr $MC_PATH/*.log.lck 2> /dev/null/
;;
esac
else
server_launch
if [ $DISPLAY_ON_LAUNCH -eq 1 ]
then
display
fi
fi
;;
#################################################################
"stop")
if [ $ONLINE -eq 1 ]
then
server_stop
else
case $2 in
"force")
kill `ps -e | grep java | cut -d " " -f 1`
rm -fr $MC_PATH/*.log.lck 2> /dev/null/
;;
*)
echo "Server seems to be offline..."
;;
esac
fi
;;
#################################################################
"restart")
if [ $ONLINE -eq 1 ]
then
case $2 in
"warn")
screen -S $SCREEN_NAME -p 0 -X stuff "`printf "say Server will restart in 30s !\r"`"; sleep 20
screen -S $SCREEN_NAME -p 0 -X stuff "`printf "say Server will restart in 10s !\r"`"; sleep 10
;;
esac
server_stop
fi
server_launch
if [ $DISPLAY_ON_LAUNCH -eq 1 ]
then
display
fi
;;
#################################################################
"logs")
mkdir -p $LOG_TDIR
cd $LOG_TDIR
case $2 in
"clean")
DATE=$(date +%d-%m --date "$LOGS_DAYS day ago")
if [ -e logs-$DATE ]
then
mkdir -p $BKUP_PATH/logs
mv logs-$DATE $BKUP_PATH/logs/
fi
;;
esac
DATE=$(date +%d-%m)
LOG_NEWDIR=logs-$DATE
if [ -e $LOG_TDIR/$LOG_NEWDIR ]
then
rm $LOG_TDIR/$LOG_NEWDIR/*
else
mkdir $LOG_TDIR/$LOG_NEWDIR
fi
DATE=$(date +%d-%m-%Hh%M)
LOG_TFILE=logs-$DATE.log
if [ $SERVERMOD -eq 1 ]
then
if [ $ONLINE -eq 1 ]
then
LOG_LCK=$(basename $MC_PATH/logs/*.log.lck .log.lck)
echo "Found a log lock : $LOG_LCK"
else
LOG_LCK=""
fi
cd $MC_PATH/logs/
for i in *
do
if [ $i != $LOG_LCK.log.lck ] # skip du fichier lck
then
cat $i >> $LOG_TDIR/$LOG_NEWDIR/$LOG_TFILE
if [ $i != $LOG_LCK.log ] # On ne supprime pas le fichier log courant, si le serv est en route
then
rm $i
fi
fi
done
else
cd $MC_PATH
cat server.log >> $LOG_TDIR/$LOG_NEWDIR/$LOG_TFILE
fi
if [ -e $LOG_TDIR/ip-list.log ]
then
cat $LOG_TDIR/ip-list.log | sort | uniq > $LOG_TDIR/templist.log
fi
cat $LOG_TDIR/$LOG_NEWDIR/$LOG_TFILE | egrep '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+.+logged in' | sed -e 's/.*\[INFO\]\s//g' -e 's/\[\//\t/g' -e 's/:.*//g' >> $LOG_TDIR/templist.log
cat $LOG_TDIR/templist.log | sort | uniq -w 4 > $LOG_TDIR/ip-list.log
rm $LOG_TDIR/templist.log
cat $LOG_TDIR/$LOG_NEWDIR/$LOG_TFILE | egrep 'logged in|lost connection' | sed -e 's/.*\([0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}\).\[INFO\].\([a-zA-Z0-9_]\{1,\}\).\{1,\}logged in/\1\t\2 : connected/g' -e 's/.*\([0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}\).\[INFO\].\([a-zA-Z0-9_]\{1,\}\).lost connection.*/\1\t\2 : disconnected/g' >> $LOG_TDIR/$LOG_NEWDIR/connexions-$DATE.log
cat $LOG_TDIR/$LOG_NEWDIR/$LOG_TFILE | egrep '<[a-zA-Z0-9_]+>|\[CONSOLE\]' | sed -e 's/.*\([0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}\).\[INFO\]./\1 /g' >> $LOG_TDIR/$LOG_NEWDIR/chat-$DATE.log
cat $LOG_TDIR/$LOG_NEWDIR/$LOG_TFILE | egrep 'Internal exception|error' | sed -e 's/.*\([0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}\).\[INFO\]./\1\t/g' >> $LOG_TDIR/$LOG_NEWDIR/errors-$DATE.log
;;
#################################################################
"backup")
mkdir -p $BKUP_PATH
if [ -e $MC_PATH/$WORLD_NAME ]
then
if [ $ONLINE -eq 1 ]
then
echo "Server running, warning players : backup in 10s."
screen -S $SCREEN_NAME -p 0 -X stuff "`printf "say Backing up the map in 10s\r"`"; sleep 10
screen -S $SCREEN_NAME -p 0 -X stuff "`printf "say Now backing up the map...\r"`"
echo "Issuing save-all command, wait 5s..."
screen -S $SCREEN_NAME -p 0 -X stuff "`printf "save-all\r"`"; sleep 5
echo "Issuing save-off command..."
screen -S $SCREEN_NAME -p 0 -X stuff "`printf "save-off\r"`"; sleep 1
fi
cd $BKUP_PATH
DATE=$(date +%Y-%m-%d-%Hh%M)
FILENAME=$WORLD_NAME-$DATE
BACKUP_FILES=$BKUP_PATH/list.$DATE
if test `date +%H` -eq 0 -o ! -f $BACKUP_FULL_LINK
then
# Make full backup, and remove old incrementals
FILENAME=$FILENAME-full.tgz
# Remove incrementals older than $BKUP_DAYS_INCR
# Remove full archives older than $BKUP_DAYS_FULL
find ./$WORLD_NAME-*-incr.tgz -type f -mtime +$BKUP_DAYS_INCR -print > purgelist
find ./$WORLD_NAME-*-full.tgz -type f -mtime +$BKUP_DAYS_FULL -print >> purgelist
rm -f `cat purgelist`
rm -f purgelist
# Now make our full backup
pushd $MC_PATH
find $WORLD_NAME -type f -print > $BACKUP_FILES
tar -zcvf $BKUP_PATH/$FILENAME --files-from=$BACKUP_FILES
popd
rm -f $BACKUP_FULL_LINK $BACKUP_INCR_LINK
ln -s $FILENAME $BACKUP_FULL_LINK
else
# Make incremental backup
FILENAME=$FILENAME-incr.tgz
pushd $MC_PATH
find $WORLD_NAME -newer $BACKUP_FULL_LINK -type f -print > $BACKUP_FILES
tar -zcvf $BKUP_PATH/$FILENAME --files-from=$BACKUP_FILES
popd
rm -f $BACKUP_INCR_LINK
ln -s $FILENAME $BACKUP_INCR_LINK
fi
rm -f $BACKUP_FILES
if [ $ONLINE -eq 1 ]
then
echo "Issuing save-on command..."
screen -S $SCREEN_NAME -p 0 -X stuff "`printf "save-on\r"`"; sleep 1
screen -S $SCREEN_NAME -p 0 -X stuff "`printf "say Backup is done, have fun !\r"`"
fi
echo "Backup process is over."
else
echo "The world \"$WORLD_NAME\" does not exist.";
fi
;;
#################################################################
"cartography")
if [ -e $CARTO_PATH ]
then
if [ -e $MC_PATH/$WORLD_NAME ]
then
if [ $ONLINE -eq 1 ]
then
echo "Issuing save-all command, wait 5s...";
screen -S $SCREEN_NAME -p 0 -X stuff "`printf "save-all\r"`"; sleep 5
echo "Issuing save-off command...";
screen -S $SCREEN_NAME -p 0 -X stuff "`printf "save-off\r"`"; sleep 1
screen -S $SCREEN_NAME -p 0 -X stuff "`printf "say Map cartography has begun.\r"`"
fi
mkdir -p $MAPS_PATH
DATE=$(date +%d-%m-%Y-%Hh%M)
FILENAME=$WORLD_NAME-map-$DATE
cd $CARTO_PATH
echo "Cartography in progress..."
./c10t -w $MC_PATH/$WORLD_NAME/ -o $FILENAME.png -z --striped-terrain --show-players --ttf-path=minecraft.ttf --ttf-color=255,0,0,0 --ttf-size 20 -s --threads 1 --memory-limit 64 --swap-file swap1
mv $FILENAME.png $MAPS_PATH
sleep 600
DATE=$(date +%d-%m-%Y-%Hh%M)
FILENAME=$WORLD_NAME-map-$DATE
./c10t -w $MC_PATH/$WORLD_NAME/ -o ${FILENAME}_night.png -z --striped-terrain --show-players --ttf-path=minecraft.ttf --ttf-color=255,0,0,0 --ttf-size 20 -s --threads 1 -n --memory-limit 64 --swap-file swap2
mv $FILENAME.png $MAPS_PATH
sleep 600
DATE=$(date +%d-%m-%Y-%Hh%M)
FILENAME=$WORLD_NAME-map-$DATE
./c10t -w $MC_PATH/$WORLD_NAME/ -o ${FILENAME}_caves.png -z -c --striped-terrain --show-players --ttf-path=minecraft.ttf --ttf-color=255,0,0,0 --ttf-size 20 -s --threads 1 --memory-limit 64 --swap-file swap3
mv $FILENAME.png $MAPS_PATH
cd $MC_PATH
echo "Cartography is done."
if [ $ONLINE -eq 1 ]
then
echo "Issuing save-on command..."
screen -S $SCREEN_NAME -p 0 -X stuff "`printf "save-on\r"`"; sleep 1
screen -S $SCREEN_NAME -p 0 -X stuff "`printf "say Map cartography is done.\r"`"
fi
else
echo "The world \"$WORLD_NAME\" does not exist.";
fi
else
echo "The path to cartographier seems to be wrong."
fi
;;
#################################################################
"update")
if [ $ONLINE -eq 1 ]
then
server_stop
fi
mkdir -p $BKUP_PATH
echo "Backing up current binaries..."
DATE=$(date +%d-%m-%Y)
cd $MC_PATH
if [ $SERVERMOD -eq 1 ]
then
tar -czf minecraft_server-$DATE.tar.gz minecraft_server.jar Minecraft_Mod.jar
rm Minecraft_Mod.jar
else
tar -czf minecraft_server-$DATE.tar.gz minecraft_server.jar
fi
mv minecraft_server-$DATE.tar.gz $BKUP_PATH
echo "Downloading new binaries..."
wget -N http://www.minecraft.net/download/minecraft_server.jar
if [ $SERVERMOD -eq 1 ]
then
"Downloading hey0's serverMod..."
mkdir -p ModTmp; cd ModTmp/
wget -O Minecraft_Mod.zip http://hey0.net/get.php?dl=serverbeta
unzip Minecraft_Mod.zip
cp bin/Minecraft_Mod.jar $MC_PATH/Minecraft_Mod.jar
cd $MC_PATH; rm -rf ModTmp
fi
if [ $RUNECRAFT -eq 1 ]
then
echo "Downloading Runecraft..."
mkdir -p ModTmp; cd ModTmp/
wget http://llama.cerberusstudios.net/runecraft_latest.zip
unzip runecraft_latest.zip
jar uvf $MC_PATH/minecraft_server.jar in.class o.class mm.class rm.class rn.class rt.class
cd $MC_PATH; rm -rf ModTmp
fi
server_launch
if [ $DISPLAY_ON_LAUNCH -eq 1 ]
then
display
fi
;;
#################################################################
"display")
if [ $ONLINE -eq 1 ]
then
display
else
echo "Server seems to be offline..."
fi
;;
#################################################################
"cmd")
if [ $ONLINE -eq 1 ]
then
if ! [[ $2 = '' ]]
then
case $2 in
"give")
case $5 in
Adminium|Bedrock) item=7
;;
Air) item=0
;;
Apple) item=260
;;
Cloth) item=26
;;
Arrow) item=262
;;
Cloth) item=34
;;
Bloodstone) item=87
;;
Cloth) item=28
;;
Boat) item=333
;;
Book) item=340
;;
Bookcase) item=47
;;
Bow) item=261
;;
Bowl) item=281
;;
Bread) item=297
;;
Brick) item=45
;;
Mushroom) item=39
;;
Bucket) item=325
;;
Furnace) item=62
;;
Cactus) item=81
;;
Boots) item=305
;;
Chestplate) item=303
;;
Helmet) item=302
;;
Pants) item=304
;;
Chest) item=54
;;
Clay) item=82
;;
Balls) item=337
;;
Brick) item=336
;;
Coal) item=263
;;
"Coal ore") item=16
;;
Cobblestone) item=4
;;
Stairs) item=67
;;
Compass) item=345
;;
Fish) item=350
;;
Crops) item=59
;;
Cloth) item=27
;;
Diamond) item=264
;;
Axe) item=279
;;
Block) item=57
;;
Boots) item=313
;;
Chestplate) item=311
;;
Helmet) item=310
;;
Hoe) item=293
;;
Ore) item=56
;;
Pants) item=312
;;
Pickaxe) item=278
;;
Spade) item=277
;;
Sword) item=276
;;
Dirt) item=3
;;
Step) item=43
;;
Egg) item=344
;;
Feather) item=288
;;
Fence) item=85
;;
Fire) item=51
;;
Rod) item=346
;;
Flint) item=318
;;
Steel) item=259
;;
Furnace) item=61
;;
Glass) item=20
;;
Ore) item=74
;;
Axe) item=286
;;
Block) item=41
;;
Boots) item=317
;;
Chestplate) item=315
;;
Dust) item=348
;;
Helmet) item=314
;;
Hoe) item=294
;;
Ingot) item=266
;;
Pants) item=316
;;
Pickaxe) item=285
;;
Record) item=225
6
;;
Spade) item=284
;;
Sword) item=283
;;
"Gold ore") item=14
;;
"Golden apple") item=322
;;
Grass) item=2
;;
Gravel) item=13
;;
Cloth) item=25
;;
Record) item=225
7
;;
Pork) item=320
;;
Gunpowder) item=289
;;
Ice) item=79
;;
Cloth) item=30
;;
Axe) item=258
;;
Block) item=42
;;
Boots) item=309
;;
Chestplate) item=307
;;
Door) item=71
;;
Helmet) item=306
;;
Hoe) item=292
;;
Ingot) item=265
;;
Pants) item=308
;;
Pickaxe) item=257
;;
Spade) item=256
;;
Sword) item=267
;;
"Iron door") item=330
;;
"Iron ore") item=15
;;
Lantern) item=91
;;
Jukebox) item=84
;;
Ladder) item=65
;;
Lava) item=10
;;
"Lava bucket") item=327
;;
Leather) item=334
;;
Boots) item=301
;;
Chestplate) item=299
;;
Helmet) item=298
;;
Pants) item=300
;;
Leaves) item=18
;;
Lever) item=69
;;
Lightstone) item=89
;;
Cloth) item=24
;;
Log) item=17
;;
Cloth) item=32
;;
Bucket) item=335
;;
"Mine cart") item=328
;;
Tracks) item=66
;;
Spawner) item=52
;;
Cobblestone) item=48
;;
Soup) item=282
;;
Obsidian) item=49
;;
Cloth) item=22
;;
Paintings) item=321
;;
Paper) item=339
;;
Cloth) item=33
;;
Pork) item=319
;;
Portal) item=90
;;
Minecart) item=343
;;
Pumpkin) item=86
;;
Cloth) item=29
;;
Fish) item=349
;;
Cloth) item=21
;;
Mushroom) item=40
;;
"Red rose") item=38
;;
Redstone) item=331
;;
Ore) item=73
;;
Wire) item=55
;;
Off) item=75
;;
On) item=76
;;
Reed) item=83
;;
Reed) item=338
;;
Saddle) item=329
;;
Sand) item=12
;;
Sapling) item=6
;;
Seeds) item=295
;;
Sign) item=323
;;
Post) item=63
;;
Ball) item=341
;;
Sand) item=88
;;
Snow) item=78
;;
Block) item=80
;;
Snowball) item=332
;;
Soil) item=60
;;
Sponge) item=19
;;
"Stationary lava") item=11
;;
"Stationary water") item=9
;;
Step) item=44
;;
Stick) item=280
;;
Stone) item=1
;;
Axe) item=275
;;
Button) item=77
;;
Hoe) item=291
;;
Pickaxe) item=274
;;
Plate) item=70
;;
Spade) item=273
;;
Sword) item=272
;;
Minecart) item=342
;;
String) item=287
;;
TNT) item=46
;;
Torch) item=50
;;
Cloth) item=31
;;
Sign) item=68
;;
Watch) item=347
;;
Water) item=8
;;
"Water bucket") item=326
;;
Wheat) item=296
;;
Cloth) item=36
;;
Wood) item=5
;;
Axe) item=271
;;
Door) item=64
;;
Hoe) item=290
;;
Pickaxe) item=270
;;
Plate) item=72
;;
Spade) item=269
;;
Stairs) item=53
;;
Sword) item=268
;;
"Wooden door") item=324
;;
Wool) item=35
;;
Workbench) item=58
;;
Cloth) item=23
;;
"Yellow flower") item=37
;;
*) item=$5
;;
esac
count=$4
if [[ $count -gt 64 ]]
then
count=64
echo "There is a maximum of 64 items per slot. Only gave 64 items."
fi
command="${2} ${3} ${item} ${count}"
screen -S $SCREEN_NAME -p 0 -X stuff "`printf "${command}\r"`"
echo "$(date +"%F %R:%S") [ServerScript] [INFO] Issued server command \"${command}\"." >> $MC_PATH/server.log
echo "Issued server command \"${command}\"."
echo "Logfile:"
echo "$(tail ${MC_PATH}/server.log | grep -A 3 ${command})"
;;
"help"|'?')
echo "$SERVERHELP"
;;
*)
command=''
first=ErstesMal
while ! [[ $2 = '' ]]
do
if [[ $first = ErstesMal ]]
then
#echo 'Kommando ohne "${command} ", da "first" "ErstesMal" ist.'
command="${2}"
first=ntesMal # && echo "\"first\" sollte \"ntesMal\" sein."
else
command="${command} ${2}"
#echo "Kommando mit \" \", da \"first\" \"ntesMal\" ist."
fi
#echo "2 war \"${2}\" und command war \"${command}\""
shift
done
screen -S $SCREEN_NAME -p 0 -X stuff "`printf "${command}\r"`"
echo "$(date +"%F %R:%S") [ServerScript] [INFO] Issued server command \"${command}\"." >> $MC_PATH/server.log
echo "Issued server command \"${command}\"."
echo "Logfile:"
echo "$(tail ${MC_PATH}/server.log | grep -A 3 ${command})"
;;
esac
else
echo "$SERVERHELP"
fi
else
echo "Server seems to be offline..."
fi
;;
#################################################################
*)
echo "Usage : minecraft <status | start [force] | stop | restart [warn] | logs [clean] | backup [clean] | cartography | update | display | cmd <command>>"
;;
esac
else
if [ $ONLINE -eq 1 ]
then
echo "Minecraft server seems to be online..."
echo "$SERVERHELP"
else
echo "Minecraft server seems to be offline..."
echo "$SERVERHELP"
fi
fi
exit 0
<e>