Font Size

Tutorial how to run CraftBukkit on linux server with automatic backup and restart

Hello All,

Some script we use on Mineserver.sk

Purpose:

To run CB on linux server ( I use ubuntu ) in screen mode so you can log off and it will go on.

To have full backup of your map every day. ( this save me a lot .. )

To have automatic restart daily for stability purpose.

Setting of CB will not be covered as this can be easily find on its homepage..

 

Note*

my directory where bukkit binary is, is: /var/minecraft/bukkit/

 

I use two start scripts.

 

1: bukkit_start.sh <--- This one start the nogui

 

1
2
#!/bin/sh
java  -Xincgc -Xmx4G -jar craftbukkit-0.0.1-SNAPSHOT.jar nogui

 

2: start.sh <--- this one take care of running it in screen

1
2
#!/bin/bash
screen -S minecraft -t world -d -m /var/minecraft/bukkit/bukkit_start.sh

 

Parameters for screen are important we use them to pass commands to game console with script..

 

My backup script:

backup.sh

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash
# Minecraft AutoBackup
NOW=$(date +"%d-%m-%Y")
i=$(date +"%Hh%Mm%Ss")
cd /var/minecraft/bukkit/

if [ -e server.log.lck ] #check if server is running
then #Inform players that backup started
screen -S minecraft -p world -X stuff "say ###Auto Backup - Started###"`echo -ne '\015'`
screen -S minecraft -p world -X stuff "save-off"`echo -ne '\015'`
screen -S minecraft -p world -X stuff "save-all"`echo -ne '\015'`
sleep 5
tar -czf /var/minecraft/bukkit/backup/Svet_MineCraftu-$NOW-$i.tar.gz /var/minecraft/bukkit/Svet_MineCraftu  #/var/minecraft/bukkit/backup/ is where is my backup located and Svet_MineCraftu is name of map
screen -S minecraft -p world -X stuff "save-on"`echo -ne '\015'`
screen -S minecraft -p world -X stuff "say ###Auto Backup - Done###"`echo -ne '\015'`
find /var/minecraft/bukkit/backup/ -mmin +2880 -exec rm {} \; #remove backups older than 2days
fi

 

Restart script

restart.sh <-- will restart server with 1min info in advance for players

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash
# Minecraft AutoRestart

cd /var/minecraft/bukkit/
screen -S minecraft -p world -X stuff "say ###Auto Restart - Zacina###"`echo -ne '\015'`
screen -S minecraft -p world -X stuff "say # Restart za 1 Minutu!"`echo -ne '\015'`
sleep 50
screen -S minecraft -p world -X stuff "say # Restart za 10 Sekund!"`echo -ne '\015'`
sleep 5
screen -S minecraft -p world -X stuff "say # Restart za 5 Sekun! Uz by ste sa mali odpojit !"`echo -ne '\015'`
screen -S minecraft -p world -X stuff "save-all"`echo -ne '\015'`
sleep 5
screen -S minecraft -p world -X stuff "stop"`echo -ne '\015'`
sleep 5
screen -S minecraft -t world -d -m /var/minecraft/bukkit/bukkit_start.sh
sleep 5
screen -S minecraft -X stuff "say ###Auto Restart - Skoncil###"`echo -ne '\015'`

 

Check script if server is running or not ( do not detect hanged server yet… )

check.sh

 

1
2
3
4
5
6
7
8
9
10
11
#!/bin/sh
cd /var/minecraft/bukkit/
if [ -e server.log.lck ]
then
echo "Server is running !"
else
echo "Server is down !"
screen -S minecraft -t world -d -m /var/minecraft/bukkit/bukkit_start.sh
NOW=$(date +"%b-%d-%y %H:%M")
echo '[' $NOW ']: Server was down.... Started !' >> /var/minecraft/bukkit/logs/ServerCheck.log
fi

 

And how it is tight up all together in cron ?

 

Cron:

 

1
2
3
4
5
6
7
## minecraft maintanance scripts
#This backs up the server at 3:35am every morning.
35  3  *  *  *  cd /var/minecraft/bukkit && ./backup.sh > /dev/null 2>&1
#This restarts the server at 3:40am every morning.
40  3  *  *  *  cd /var/minecraft/bukkit && ./restart.sh > /dev/null 2>&1
#Check if server is running every 30min and if not, will start it
*/30  *  *  *  * cd /var/minecraft/bukkit && ./check.sh > /dev/null 2>&1

 

Maybe it can be helpful to somebody…

 

Comments

Posted On
Apr 27, 2011
Posted By
jg

Hi,

thanks for the scripts I use them and they do work fine.
However why do use >> on line 10 of check check.sh

This generated a syntax error for me, I just replaced it with >> since it isn’t even under quotes.
I’ll have to review the crontab as well as I suspect the &gt and &amp wont work there as well.

I’m on debian (lenny) by the way.
cheers

Posted On
Apr 30, 2011
Posted By
VladoPortos

Ah it might have happen with the auto conversion that wordpress done to the script, sorry about that, I have fixed it now.

[...] jakiegoś gotowca do robienia backupów map Minecrafta natrafiłem na arcyciekawy wpis kolegi ze Słowacji gdzie można znaleźć pakiet skryptów *.sh do podstawowej obsługi serwera [...]

Posted On
Jun 18, 2011
Posted By
Ralf

Hey, I understand most parts of the scripts but in “-X stuff”, what is the “stuff” for? Is it a linux user? Can you explain?

Posted On
Jun 20, 2011
Posted By
VladoPortos

Its part of the script so sending command to screen session, from manual

-X Send the specified command to a running screen session. You can use the -d
or -r option to tell screen to look only for attached or detached screen
sessions. Note that this command doesn’t work if the session is password
protected.

I just put ‘stuff’ there, it has no meaning, but i think it will not work without it.. you can always try :)

V.

Posted On
Jan 04, 2012
Posted By
Neero

Thanks for this, much better than my(tm) backup script :) This even removes old backups :D

Is the -t on screen necessary? Didn’t have that in my script.

Also, I have server crash almost once every day (not only minecraft, the whole server crashes totaly). I think it is related to my backup script. Most of the times (always?), it crashes in the middle of the night when no one is online. I think it is the backup that causes it because when my backup hdd was full, the server ran for several days without a crash. I backup the whole minecraft folder (server files, plugins, maps, etc). Do you have any opinion on why that happens?

Also, for those who have the dynmap mod, and wants to exclude the web/tiles directory, add this on the end of the tar line:
–exclude “/path/to/bukkit/plugins/dynmap/web/tiles”

Posted On
Jan 04, 2012
Posted By
Neero

One more thing btw :)
Is it possible to make it check if there has been any user logged in since last backup? That would save me a whole load of diskspace :)

Posted On
Jan 07, 2012
Posted By
VladoPortos

About the server crashes, it can be lots of factors. Depends what kind of server you have, I can’t say for windows or Mac, but for linux there should be something in logs, if its not than I would suggest to install some kind of dump program, like kdump that would create crasf file if you are lucky :) and analyse that why it is happening.

Most time I seen server crash:
- Memory issue ( need to run couple of memtest to check )
- Disk issue ( server might not be powerful enough to handle I/O during backup and crash )
- Power source ( believe or not I had this happen to me, too many HDD in server and not enough power, server will run but crash if there was some more action going on )

Posted On
Apr 16, 2012
Posted By
Landrash

Nice script.

Leave a Reply


Feel generous today ?

By me a beer !

Login