[LINUX] [Minecraft server] No MOD / plug-in required !! Automatic backup + restart system

Shell backup system

I run a Minecraft server I made it because I wanted to back it up and restart it on a regular basis.

By operating from the OS side using a shell script instead of processing in minecraft It works without mods or plugins.

Make an article so that you don't forget it for yourself

conditions

This article is created under the following conditions.

-Being a Linux server. (Probably possible with bat on Windows) --It doesn't matter whether it's Forge Server or Bukkit Server. -** Compatible with all versions **

Advance preparation

It is convenient to run the server process in the screen environment, so if screen is not included, install it.

# yum -y install screen

Run it as root user or sudo.

Next, create a directory to save the backup data. The location and directory name are free. (Avoid only in the world directory of the Minecraft server)

$ mkdir backup

Shell script

This is the main program this time. First, make a shell wherever you like. Please use the same user as the Minecraft server below. How to use the vi editor

$ vi backup.sh

↓ Copy and paste ↓ (* Only the file path needs to be rewritten)

backup.sh



#!/bin/bash
#
#Minecraft backup automatic creation script

#######Send a message for those in the game.#######
time1=60 #You can adjust the time until restart by playing with this number
screen -S minecraft -X stuff 'say §a§l§n automatic announcement'`echo -ne '\015'`
screen -S minecraft -X stuff 'say §b'$time1'After § seconds, perform § l backup § 6 and § l reboot § 6.'`echo -ne '\015'`
time2=`expr $time1 - 10`
sleep `expr $time2`
screen -S minecraft -X stuff 'say §c The server will stop automatically soon.'`echo -ne '\015'`
sleep 10
screen -S minecraft -X stuff 'stop'`echo -ne '\015'` #End of minecraft

##########Wait until minecraft is completely finished##########
#To count the number of java processes
#If you are running java elsewhere, please adjust
count1=`ps -ef | grep java | grep -v grep | wc -l`
while :
do
    if [ $count1 = 0 ] ; then
        break
    else
        sleep 0.5
        count1=`ps -ef | grep java | grep -v grep | wc -l`
    fi
done


##############Backup start#################
#Copy of world directory
cp minecraft path/world backup directory path/backup

#Compress to zip
zip backup directory path/backup/world backup directory path/backup

#Date the file name
cp -aiv backup directory path/backup/world.zip backup directory path/backup/world.`date "+%Y%m%d_%H%M"`.zip

#Erase what you don't need
rm -rf backup directory path/backup/world
rm backup directory path/backup/world.zip

#############minecraft restart##############
screen -S minecraft -X stuff 'java -Xmx4096M -Xms1024M -jar server jar absolute path(Example/home/User/Minecraft/server.jar) nogui'`echo -ne '\015'`
#java argument is optional

Please make fine adjustments by yourself.

Grant permissions to run

$ chmod 744 backup.sh

Start the server

Due to sending commands ** The server starts on screen. ** ← Important here screen is like a virtual desktop. Launch a screen named minecraft.

$ screen -S minecraft

If the screen called minecraft is already running, attach it.

$ screen -r minecraft

Run Minecraft Server inside the screen. From the shell or from the command ↓ In case of command

$cd minecraft server path
$ java -Xmx4096M -Xms1024M -jar server.jar nogui

I think this will start the server as usual, so detach it.

Press the Ctrl key + ʻa keyand then thed key`.

Then you will be returned to the Linux command line, but behind the scenes, Minecraft's server is running. ** If you want to go back to Minecraft console, please attach above **

Perform backup

Run the shell with the screen detached.

$ ./backup.sh

A message appears in Minecraft It is successful if automatic stop → backup → automatic start is performed. 2020-08-18_19.43.42_LI.jpg Back file data is created in this way. コメント 2020-08-18 195033.jpg To restore the backup, unzip the zip and replace it with the world directory.

That is all. Thank you for your hard work.

Advanced version

Regular execution for 24-hour server

It can be automatically executed at a fixed time every day. Use cron for that.

CentOS should already have cron, but if it doesn't, install it.

# yum -y install crontabs

Create a couomb. Please do it with the same user as the Minecraft server

$ crontab -e

The vi editor starts Below is an example. For example, if you wanted to run at 6:00 AM and 4:00 PM

crontab


0 6,16 * * * /home/User/backup.sh(backup.absolute path of sh)

, And write. For details, please see this article because it is excellent.

Recommended Posts

[Minecraft server] No MOD / plug-in required !! Automatic backup + restart system