FTB Server Backup Script

  • Please make sure you are posting in the correct place. Server ads go here and modpack bugs go here

RabidBurn

New Member
Jul 29, 2019
36
0
0
Hey Guys,
Have I got a treat for you!

*edit* I'm not actually sure what section this goes in, so mods, feel free to move it if this is the wrong place for it!

After reading "Johnleys fast and easy Server Setup Guide," I decided that I could make some modifications to his backup script to make it more powerful.

Code:
#!/bin/bash

TIME=$(date +"%Y-%m-%d.%H")
HOUR=$(date +"%H")

DEST="/mnt/nasBackup/MonsterBackups"
SRCWORLD="/ftbLauncher/Monster/v1.1.1/world"
SRCFULL="/ftbLauncher/Monster/v1.1.1/"
TDESTWORLD="/ftbLauncher/Monster/v1.1.1/temp"
TDESTFULL="/ftbLauncher/Monster/temp"

screen -S ftbM -X stuff "say Starting backup. World not saving... $(printf '\r')"
screen -S ftbM -X stuff "save-off $(printf '\r')"
screen -S ftbM -X stuff "save-all $(printf '\r')"

#WORLD
if [ "$(($HOUR % 2))" = 0 ]; then

        echo "$(($HOUR % 2))"
        cp -r $SRCWORLD/* $TDESTWORLD
        zip -r  $DEST/WORLDBKPS/MonsterWORLDBKP-srv-$TIME.zip $TDESTWORLD/*

        rm -r $TDESTWORLD/*
else
        echo "Not a bi-hour."
fi
#END WORLD

#FULL
if [ "$(($HOUR % 12))" = 0 ]; then

        cp -r $SRCFULL $TDESTFULL
        zip -r  $DEST/FULLBKPS/MonsterFULLBKP-srv-$TIME.zip $TDESTFULL*

        rm -r $TDESTFULL/*
else
        echo "Not a 12-hour."
fi
#END FULL

screen -S ftbM -X stuff "save-on $(printf '\r')"
screen -S ftbM -X stuff "say Backup complete. World saving again. $(printf '\r')"

It also needs two lines in crontab, which you can modify however you want to make the backup happen at different intervals. (modified by typing "sudo crontab -e", editing it, and then saving)

Code:
0 */2 * * * /ftbLauncher/Monster/v1.1.1/ftb-backup.sh
0 0 * * * /ftbLauncher/Monster/v1.1.1/ftb-backup.sh

Now, this script is designed to make a world backup every two hours, and a full modpack folder backup every twelve hours. I understand that it may not be necessary to have THAT many backups, but I find that it soothes the voice in the back of my head saying "What if hitting this with a wrench corrupts my world, and somehow deletes my whole modpack folder?" and "What if this guy griefs my base?". Up to you guys how often you want to run the script though. Note: if you want to make this happen at odd hours (like 1, 3, 5, etc.) you need to modify the if statements so that the hour % 3 == 0.

It's pretty simple, basically you add all of your directories here:
Code:
DEST="/mnt/nasBackup/MonsterBackups"
SRCWORLD="/ftbLauncher/Monster/v1.1.1/world"
SRCFULL="/ftbLauncher/Monster/v1.1.1/"
TDESTWORLD="/ftbLauncher/Monster/v1.1.1/temp"
TDESTFULL="/ftbLauncher/Monster/temp"
making sure that you end the paths exactly the way they end here (DOUBLE CHECK THE FORWARD SLASHES, you might accidentally delete everything if you don't) I use temporary directories, because zipping and transferring the files to my NAS takes some time, and world saving doesn't start up again until after it's done.

Now, you can set the name of your file here:
Code:
zip -r  $DEST/WORLDBKPS/MonsterWORLDBKP-srv-$TIME.zip $TDESTWORLD/*
and here
Code:
zip -r  $DEST/FULLBKPS/MonsterFULLBKP-srv-$TIME.zip $TDESTFULL*
(for the world and full backups respectively)
where it says "MonsterWORLDBKP-srv-$TIME.zip" and "MonsterFULLBKP-srv-$TIME.zip"

Also, you need to make sure that the script is accessing the right screen session by changing
"ftbM" to the name of your screen session in
Code:
screen -S ftbM -X stuff "blah blah blah"
(change it for every line of this) and you should be done! Save this file in the same directory as your "world" directory, make sure that the crontab links straight to this file, and you should be good!

If there's anything I've forgotten, or you have any problems, go ahead and comment! I promise I'll fix it...
Credits for the original script goes to Bråthen who posted it in Johnley's post on the forums.
~RabidBurn
 
Last edited:
  • Like
Reactions: Wagon153

madnewmy

New Member
Jul 29, 2019
1,119
0
0
Can that work with ssp also?

I, let's say, make my game crash every hour or so and give myself an hearth attack every time ;)
 

RabidBurn

New Member
Jul 29, 2019
36
0
0
Can that work with ssp also?

I, let's say, make my game crash every hour or so and give myself an hearth attack every time ;)
Well it's written in bash, so if you're playing on Linux it should work, it might even work on Mac (both of these are untested), but if you're playing on WIndows you're SOL.

~RabidBurn
 

Dead_Lemon

New Member
Jul 29, 2019
1
0
0
Thanks RabidBurn for the script, this has been really helpful

I've been looking for an auto backup method that was simple, but I've been struggling to figure out how you can back up while the server is live (I've been taking it down while no one is playing and making a back up every other day), I didn't realize that there was a world save disable function built into minecraft, which is useful :)
I also didn't know you could push commands into a detached screen, I'm stoked about that one :D

I've stripped out a fair amount out of the script, as it backs up way to often for my needs, as I'm only running a small server and it doesn't have much space to store entire game back ups every 12 hours, lol.
I basically split them into two scripts instead and set cronjobs for them both, at a rate of one world back up a day and a game backup per month.
Seems to be working well so far :D
 

RabidBurn

New Member
Jul 29, 2019
36
0
0
Thanks RabidBurn for the script, this has been really helpful

I've been looking for an auto backup method that was simple, but I've been struggling to figure out how you can back up while the server is live (I've been taking it down while no one is playing and making a back up every other day), I didn't realize that there was a world save disable function built into minecraft, which is useful :)
I also didn't know you could push commands into a detached screen, I'm stoked about that one :D

I've stripped out a fair amount out of the script, as it backs up way to often for my needs, as I'm only running a small server and it doesn't have much space to store entire game back ups every 12 hours, lol.
I basically split them into two scripts instead and set cronjobs for them both, at a rate of one world back up a day and a game backup per month.
Seems to be working well so far :D

Hey Dead,
That's really great to hear! That's exactly the reason that I posted it on here!

When I originally made the script, the two different types of backups actually WERE in two separate files, but I wanted to learn more bash and I figured that it would be more convenient so I just put them together into one file with if statements.

Thanks so much for commenting, I really appreciate it!
~RabidBurn
 

keybounce

New Member
Jul 29, 2019
1,925
0
0
My concern is this part:
Code:
screen -S ftbM -X stuff "save-all $(printf '\r')"

#WORLD
if [ "$(($HOUR % 2))" = 0 ]; then

echo "$(($HOUR % 2))"
cp -r $SRCWORLD/* $TDESTWORLD

How do you tell if the save-all has completed before you start making copies and zips?
 

RabidBurn

New Member
Jul 29, 2019
36
0
0
My concern is this part:
Code:
screen -S ftbM -X stuff "save-all $(printf '\r')"

#WORLD
if [ "$(($HOUR % 2))" = 0 ]; then

echo "$(($HOUR % 2))"
cp -r $SRCWORLD/* $TDESTWORLD

How do you tell if the save-all has completed before you start making copies and zips?

Hey keybounce,
The way I understand it is that the game automatically saves at set intervals. The save-all command should happen instantaneously, and even if it doesn't complete in time, the world should have been automatically save-all'd not too long before the command was passed. I could be wrong, but I don't think it would cause any problems.

If you really want to be safe, adding a short sleep between the save-all and the zip creation should prevent any issues.

Thanks for the comment, I'm really happy that this script is actually being used!
~RabidBurn
 

Henry Link

Popular Member
Dec 23, 2012
2,601
553
128
USA - East Coast
@RabidBurn One small suggestion I could make is about the screen session being used. In your script you have that hard coded to ftbM. It would be better to make this a variable like the directories are. I run a server with three packs currently on it (DW20, Crash Landing & Unstable packs). Each server runs in its own screen session. I use a different script on that setup. But, it accomplishes the same thing.

If anyone is interested in that script I'll post it. It runs the pack as a service that has the following commands: start, stop, restart, backup, remove old backups, console & status. My cron entries actually restart the FTB servers and do the backups and auto deletes backup older than 7 days.
 

RabidBurn

New Member
Jul 29, 2019
36
0
0
@RabidBurn One small suggestion I could make is about the screen session being used. In your script you have that hard coded to ftbM. It would be better to make this a variable like the directories are. I run a server with three packs currently on it (DW20, Crash Landing & Unstable packs). Each server runs in its own screen session. I use a different script on that setup. But, it accomplishes the same thing.

If anyone is interested in that script I'll post it. It runs the pack as a service that has the following commands: start, stop, restart, backup, remove old backups, console & status. My cron entries actually restart the FTB servers and do the backups and auto deletes backup older than 7 days.

Hey Henry,
Great idea! I never thought about setting the screen session as a variable, but it shouldn't be too difficult to accomplish.

Also, I'd love to see your script!

~RabidBurn
 

keybounce

New Member
Jul 29, 2019
1,925
0
0
My understanding of saves is that world block data gets saved out about every 30 seconds, but other information -- including players and inventories -- only gets pushed when a world is unloaded, or the game is saved. Or when /save-all happens.

Hmm ...
 

Tankh

New Member
Jul 29, 2019
104
0
0
Modified the script to also remove all but the 5 latest backups created so your HDD doesn't fill up and crash the server.

It's also changed to specifically target window 0 in the selected screen so that it won't enter the commands in the last window you happened to have up the last time you logged in and checked the screen.
(the screen name is "mc" in my case, but that can of course be changed to whatever you use)

Code:
#!/bin/bash
#Crude backup script for minecraft servers
#  It simply copies the world folder to a different folder
#  and maintains the last 5 copies of backups.
#  IMPORTANT: Server must be running in a screen namned "mc" and in first window (window 0)
#
#  Define these 3 variables:
#      worldName - Should be the same as level-name in server.properties
#      worldDir  - Path to the whole server folder (do NOT end with a slash)
#      backupDir - Path to the folder you want to keep all the backups in (do NOT end with a slash)
#
#  Run this script in crontab with whichever periodicity you like
#
#  Change the "5" in the while loop to any desired number of backups you want to keep

# Define these variables for where your server is located, named and where to put backups
worldName="TanktronixMonster111"
worldDir="/home/minecraft/FTB_Monster_Server_1_1_1"
backupDir="/home/minecraft/Backups/TanktronixMonster"

# Set timestamp for backup folder
TIME=$(date +"%Y-%m-%d_%H:%M")

# Set destination and source paths for backup command
src="$worldDir/$worldName"
dst="$backupDir/$worldName-$TIME"

# Turning world saving off and broadcast message about backup to players
screen -S mc -p 0 -X stuff "say §7§o[BACKUP] Starting backup. World not saving...^M"
screen -S mc -p 0 -X stuff "save-off^M"
screen -S mc -p 0 -X stuff "save-all^M"

# Copy world folder to backup folder
cp -r $src $dst

# Turn world saving on again
screen -S mc -p 0 -X stuff "save-on^M"
screen -S mc -p 0 -X stuff "say §7§o[BACKUP] Backup complete. World saving again.^M"

# Remove oldest backups if more than 5 backups exists.
dirNum=$(du $backupDir -d1 | grep $worldName | wc -l)
while [ $dirNum -gt 5 ]; do
        #There are 5 or more backup folders, remove the oldest one.
        deleteDir=$(du $backupDir -d1 | grep $worldName | cut -f2 | sort | cut -d$'\n' -f1)
        rm -r $deleteDir
        dirNum=$(du $backupDir -d1 | grep $worldName | wc -l)
done #Loop exits when 5 or less backups exist. Let them be.

exit 0
 

IlliniJeeper

New Member
Jul 29, 2019
24
0
0
Modified the script to also remove all but the 5 latest backups created so your HDD doesn't fill up and crash the server.

It's also changed to specifically target window 0 in the selected screen so that it won't enter the commands in the last window you happened to have up the last time you logged in and checked the screen.
(the screen name is "mc" in my case, but that can of course be changed to whatever you use)

-snip-

Could you explain what targeting "window 0" does here and why it's beneficial to add that in the screen callout?

I love the ability to delete all but the last five backups to conserve hard drive space.
 

Tankh

New Member
Jul 29, 2019
104
0
0
Could you explain what targeting "window 0" does here and why it's beneficial to add that in the screen callout?

I love the ability to delete all but the last five backups to conserve hard drive space.

in my screen (named mc) I use several windows (think of them as tabs in a web browser). the first window is just the server log. the second window is htop to check server performance. 3rd and 4th window is just terminal for file access.
Whenever I want to do anything related to the minecraft server I enter that screen. one time I closed down my ssh session while I still had window 3 active (because I had done some file transfers or something) and when the backup script ran the next time, it input all the minecraft server commands in the last window I had open, which was just a shell terminal, so the "save-all, save-off etc. etc." never affected the minecraft server since they weren't written in the server log.
 

IlliniJeeper

New Member
Jul 29, 2019
24
0
0
in my screen (named mc) I use several windows (think of them as tabs in a web browser). the first window is just the server log. the second window is htop to check server performance. 3rd and 4th window is just terminal for file access.
Whenever I want to do anything related to the minecraft server I enter that screen. one time I closed down my ssh session while I still had window 3 active (because I had done some file transfers or something) and when the backup script ran the next time, it input all the minecraft server commands in the last window I had open, which was just a shell terminal, so the "save-all, save-off etc. etc." never affected the minecraft server since they weren't written in the server log.

When you're talking about these "tabs", you're talking about "Attached" screen sessions, right? Seems like a good way to go about this as long as you can always make sure that the server is the first screen session. Is there a way to ensure that that's always the case or do you just need to follow a procedure where the first screen fires up the server each time?
 

Tankh

New Member
Jul 29, 2019
104
0
0
When you're talking about these "tabs", you're talking about "Attached" screen sessions, right? Seems like a good way to go about this as long as you can always make sure that the server is the first screen session. Is there a way to ensure that that's always the case or do you just need to follow a procedure where the first screen fires up the server each time?
I don't know exactly how linux looks at the windows, but afaik it's just one single attached screen session (called "mc"), with several windows within. I can quickly switch back and forth between these windows with commands like [ctrl-a]+[num] where num is the windows you want to switch to (in my case 0-3), or [ctrl+a] [ctrl+a] to switch between the last two open windows.
Here's a list of some useful commands: http://www.pixelbeat.org/lkdb/screen.html

I can even split the screen in to parts and have one window open on the left, and an other to the right. It can also split horizontally. like this

Whenever I have to start up the screen/server again after the whole server has shut down for some reason, I just make sure the first window is where I start the minecraft server. Don't know if there's an easy way to switch windows to different positions after they have been created, so I just follow the "procedure".
You could probably have a separate script that starts the screen and all the windows and the minecraft server all in one command, for easy startup. I haven't had the need for that yet though :p