Please READ ALL OF THE INTRO BIT first:
INTRO:
Basic world backup in script form with crontab(preps for the more advanced bit)
ADVANCED BIT:
Backup script with the ability to send commands to the minecraft server, i.e. save-off, save-all, save-on, say, and so on an so forth.
LOGGING:
Advance script, with output a to a time stamped log file each time.
And that's that, I hope this helps people. Sorry it's a bit rough, I don't write things like this often. I am more than open to suggestions and comments. If you have any questions feel free to comment. Also I run minecraft from a ramdisk on my linux server, does that interest people?
::CHEAT SHEET::
Basic crontab commands:
crontab -l
Basic screen commands:
screen -ls
INTRO:
Basic world backup in script form with crontab(preps for the more advanced bit)
Most linux servers have crontab installed by default. Crontab is a scheduler program, it basically just lets you run scripts/programs/commands at certain times, days, weeks, months, etc. So to start you need to plan out how often you want to backup, for this example I will show you a backup every hour.
To start off you need to make a shell file, (ext .sh, basically linux's .bat file) so just open up your favorite text editor and save a file as .sh. For windows users using notepad when you go to save, set the "Set as type: to All files (*)" and call it whatever, again for this example I will call mine hourlycp.sh.
Once done lets begin:
It's best practice to make sure to set your directory to the folder that contains your minecraft server folder, not the minecraft server folder itself. My FTBsrv folder in the my user profile /home/deathcat99 so we will start with cd /home/deathcat99
Next the copying itself:
cp -vrf FTBsrv/ backup/ (this will just copy over itself every time the backup script runs. Not ideal, but for this intro it will do.
Now to setup Crontab:
first thing you want to do is type crontab -l most linux environments will return blank, but some will have a brief how to that is commented out (in crontab anything that starts with # is commented and not seen by the system) whether you have something that existing or not, the next step is to edit your current crontab using the command 'crontab -e'
Crontab's use is minute hour day-of-month month day-of-week command so for the use here we would want to add the following line to the file "00 * * * sh /home/deathcat99/hourlycp.sh" ( change the path to your script as neccessary) this will now run the copy script once every hour of every day.
So wrapping up the intro:
hourlycp.sh shuld look like
cd /home/deathcat99
cp -vrf FTBsrv/ backup/
and our crontab should look like
00 * * * sh /home/deathcat99/hourly.sh
Now why am I going this route? Because if we really wanted to we could jut make a crontab entry to:
"00 * * * cp /home/deathcat99/FTBsrv /home/deathcat99/backup" and do the same thing. Because this is expandable as well as wel can do quite a bit more. Thus starts the Advanced bit.
To start off you need to make a shell file, (ext .sh, basically linux's .bat file) so just open up your favorite text editor and save a file as .sh. For windows users using notepad when you go to save, set the "Set as type: to All files (*)" and call it whatever, again for this example I will call mine hourlycp.sh.
Once done lets begin:
It's best practice to make sure to set your directory to the folder that contains your minecraft server folder, not the minecraft server folder itself. My FTBsrv folder in the my user profile /home/deathcat99 so we will start with cd /home/deathcat99
Next the copying itself:
cp -vrf FTBsrv/ backup/ (this will just copy over itself every time the backup script runs. Not ideal, but for this intro it will do.
Now to setup Crontab:
first thing you want to do is type crontab -l most linux environments will return blank, but some will have a brief how to that is commented out (in crontab anything that starts with # is commented and not seen by the system) whether you have something that existing or not, the next step is to edit your current crontab using the command 'crontab -e'
Crontab's use is minute hour day-of-month month day-of-week command so for the use here we would want to add the following line to the file "00 * * * sh /home/deathcat99/hourlycp.sh" ( change the path to your script as neccessary) this will now run the copy script once every hour of every day.
So wrapping up the intro:
hourlycp.sh shuld look like
cd /home/deathcat99
cp -vrf FTBsrv/ backup/
and our crontab should look like
00 * * * sh /home/deathcat99/hourly.sh
Now why am I going this route? Because if we really wanted to we could jut make a crontab entry to:
"00 * * * cp /home/deathcat99/FTBsrv /home/deathcat99/backup" and do the same thing. Because this is expandable as well as wel can do quite a bit more. Thus starts the Advanced bit.
Backup script with the ability to send commands to the minecraft server, i.e. save-off, save-all, save-on, say, and so on an so forth.
Well what if you want to do more? like maybe stop saving while you do the copy to make sure everying gets backed up properly and to cut down on lag, or maybe server messages while you back up, well lets start there.
FIRST things first:
For most minecraft server admins that have a minecraft server up 24/7 we use screen. Screen allow you to make a session that allows you to run whatever you want in its own environment without needing user control. (so you can log off) Screen is simple enough to work around. The basics "screen -l" list current screens, "screen -r 'pid or name" attach to a screen using its pid or name, and "ctrl+a+d" disconnect a session without exiting it. So to start run the command screen -ls, this should find nothing unless you already using screen for other things. next just type "screen" and hit enter. This will display a brief how-to(feel free to read though this) but for now just hit enter again, you will be returned to what looks like the normal prompt User@server~$ where you would be entering commands, but if you press the combo ctrl+a+d you will disconnect the session and see a message detached from 'number'.'tty'.'name or host'. Lets take a better look at that, type screen -ls again, now you should see something that look like "1570.pts-1.minecraft (05/13/2014 01:00:23 PM) (Detached)" (if not type screen again, enter through it and try the key combo again) to break this down what you need to know is the 1570. The 1570 or what ever number you have is the pid or process ID, and we will use this to reattach the screen session, ex. screen -r 1570. This will take you back to the previous prompt here you can do anything, start your minecraft server, whatever and it will stay running whether you are logged in or not. Just use the key comb "Ctrl+a+d" to detach at any time and go on with what you were doing.
For this find and attach to your screen session(screen -ls and screen -r pid), or make a new one if you don't have one up(just run screen) then go to your minecraft server folder and start your server Ex. sh Startserver.sh (most ftb server packs). Now that we can get minecraft server to run independently lets get back to the backup script.
SECOND things.
Now we have way more options and a bit more screen commands to utilize. Go back to your backup script and lets add some thing. With screen up and running we can pass commands to our minecraft server using screen. The Use is screen -S pidhere -X stuff 'minecraft command here'$'\n'. So if we wanted to send a server message saying the backup was going to start we could do:
screen -S 1570 -X stuff 'say Server backup starting'$'\n'
Two things to note:
It would look something like this:
cd /home/deathcat99/
screen -S minecraft -X stuff 'say Server going Read-Only'$'\n'
screen -S minecraft -X stuff 'save-off'$'\n'
screen -S minecraft -X stuff 'save-all'$'\n'
cp -vrf FTBsrv/ backup/
screen -S minecraft -X stuff 'save-on'$'\n'
screen -S minecraft -X stuff 'say Backup Successful'$'\n'
So in wrap up, we didn't have to change the crontab at all, it should still look like this:
00 * * * sh /home/deathcat99/hourlycp.sh
And hourlycp.sh now looks like as it does above.
FIRST things first:
For most minecraft server admins that have a minecraft server up 24/7 we use screen. Screen allow you to make a session that allows you to run whatever you want in its own environment without needing user control. (so you can log off) Screen is simple enough to work around. The basics "screen -l" list current screens, "screen -r 'pid or name" attach to a screen using its pid or name, and "ctrl+a+d" disconnect a session without exiting it. So to start run the command screen -ls, this should find nothing unless you already using screen for other things. next just type "screen" and hit enter. This will display a brief how-to(feel free to read though this) but for now just hit enter again, you will be returned to what looks like the normal prompt User@server~$ where you would be entering commands, but if you press the combo ctrl+a+d you will disconnect the session and see a message detached from 'number'.'tty'.'name or host'. Lets take a better look at that, type screen -ls again, now you should see something that look like "1570.pts-1.minecraft (05/13/2014 01:00:23 PM) (Detached)" (if not type screen again, enter through it and try the key combo again) to break this down what you need to know is the 1570. The 1570 or what ever number you have is the pid or process ID, and we will use this to reattach the screen session, ex. screen -r 1570. This will take you back to the previous prompt here you can do anything, start your minecraft server, whatever and it will stay running whether you are logged in or not. Just use the key comb "Ctrl+a+d" to detach at any time and go on with what you were doing.
For this find and attach to your screen session(screen -ls and screen -r pid), or make a new one if you don't have one up(just run screen) then go to your minecraft server folder and start your server Ex. sh Startserver.sh (most ftb server packs). Now that we can get minecraft server to run independently lets get back to the backup script.
SECOND things.
Now we have way more options and a bit more screen commands to utilize. Go back to your backup script and lets add some thing. With screen up and running we can pass commands to our minecraft server using screen. The Use is screen -S pidhere -X stuff 'minecraft command here'$'\n'. So if we wanted to send a server message saying the backup was going to start we could do:
screen -S 1570 -X stuff 'say Server backup starting'$'\n'
Two things to note:
the " $'\n' " is very important, that is the command for the enter key basically
the minecraft command has to be in single not double quotes and the quotes end before the $
So for example, we want to Alert the players and stop saving on the server, force a save, and then restart saving after the backup...the minecraft command has to be in single not double quotes and the quotes end before the $
It would look something like this:
cd /home/deathcat99/
screen -S minecraft -X stuff 'say Server going Read-Only'$'\n'
screen -S minecraft -X stuff 'save-off'$'\n'
screen -S minecraft -X stuff 'save-all'$'\n'
cp -vrf FTBsrv/ backup/
screen -S minecraft -X stuff 'save-on'$'\n'
screen -S minecraft -X stuff 'say Backup Successful'$'\n'
So in wrap up, we didn't have to change the crontab at all, it should still look like this:
00 * * * sh /home/deathcat99/hourlycp.sh
And hourlycp.sh now looks like as it does above.
Advance script, with output a to a time stamped log file each time.
Okay, for you server admins out there... the script is all well an good, but wheres the proof? How do you know it's backing up regularly and what it's backing up? Well here is an adaptation of the backup script you might like. it saves logs to /home/deathcat99/scripts/logs/hourlycp with the filename hourlycpTIMESTAMPHERE.txt
(cd /home/deathcat99/ ;
echo server msg ;
screen -S minecraft -X stuff 'say Server going Read-Only.'$'\n' ;
echo save-off ;
screen -S minecraft -X stuff 'save-off'$'\n' ;
echo save-all ;
screen -S minecraft -X stuff 'save-all'$'\n' ;
echo copy ;
cp -vrf FTBsrv/ backup/ ;
echo save-on ;
screen -S minecraft -X stuff 'save-on'$'\n' ;
echo copied sucessfully ;
screen -S minecraft -X stuff 'say Backup Successful'$'\n' ;
echo end > scripts/logs/hourlycp/hourlycp$(date +%m.%d.%Y@%H.%M).txt
I'll let you read through that, it should be self explanatory. the output log looks like:
server msg
save-off
save-all
copy
"FILES IT COPIED"
save-on
copied sucessfully
end
(cd /home/deathcat99/ ;
echo server msg ;
screen -S minecraft -X stuff 'say Server going Read-Only.'$'\n' ;
echo save-off ;
screen -S minecraft -X stuff 'save-off'$'\n' ;
echo save-all ;
screen -S minecraft -X stuff 'save-all'$'\n' ;
echo copy ;
cp -vrf FTBsrv/ backup/ ;
echo save-on ;
screen -S minecraft -X stuff 'save-on'$'\n' ;
echo copied sucessfully ;
screen -S minecraft -X stuff 'say Backup Successful'$'\n' ;
echo end > scripts/logs/hourlycp/hourlycp$(date +%m.%d.%Y@%H.%M).txt
I'll let you read through that, it should be self explanatory. the output log looks like:
server msg
save-off
save-all
copy
"FILES IT COPIED"
save-on
copied sucessfully
end
::CHEAT SHEET::
Basic crontab commands:
crontab -l
list current crontabs
crontab-eedit crontab
Basic screen commands:
screen -ls
list current screen sessions
screen -rconnect to a screen USE: screen -r 'pid or name'
screen -S "pid here" -X stuff ' "minecraft server cmd with single quotes" '$'\n'pass command to minecraft server running in screen session
Last edited: