0

So I have a mineraft server and I want to set it up so that it restarts at 6am and backs up itself too. I run the server in screen and to stop the server I have to open the screen (with screen -r) and run /stop in the MC server console. I would then like the computer to automatically copy the server world file to a backup somewhere maybe with the format PATH/TO/DIRECTORY/worldBackupDD.MM.YY/. And then after that the script would run sudo java -Xmx8G -Xms8G -jar /PATH/TO/DIRECTORY/server.jar nogui to start the server again in screen.

So what I want to do is (at 6am) stop the server, backup the server, then start the server again in screen. So how do I do this? Would I use cron? I don't really know how to set this up or how to do this in cron. Or would I do it another way? Any help would be appreciated.

(btw the server runs on ubuntu server 18.04)

1

Cron is a handy command. man cron

crontab -e

# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command

0 6 * * * /home/yourloginname/backup.sh

Then create backup.sh

#!/bin/sh
command-which-stop-the-server
tar -zcf /PATH/TO/DIR/worldBackup`date "%D"`.tar /TARGET/DIR/
/PATH/TO/java -Xmx8G -Xms8G -jar /PATH/TO/DIRECTORY/server.jar nogui

Note: you must use a full path, if this needs to be executed by root, you have to run cron by the root user.
See How to set up a root cron job properly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.