Page 1 of 1

Bash Scripts to backup minetest and start/restart a running servers

PostPosted: Sat Jan 19, 2013 23:27
by RAPHAEL
Some scripts I just finished that I used. Mod variables paths whatever as needed to suit your needs. License is do whatever you want but give credit where credit is due.

Start/Restart server:
#!/bin/bash
#
# Place script in root of minetest run in place and make executable
#
# The startup command expects world to be in rootfolder/world
# Change as needed/wanted


# here I am setting a time stamp variable which I like to use for logging
TIMESTAMP=`date +%Y%m%d.%H%M`

# Logsbak directory
LOGSBAK=`$HOME/.minetest/logsbak`

# Shutdown any running process
kill $(pidof minetestserver)

# Wait
sleep 10

# Cleanup
tar -cvjf ${LOGSBAK}/logback-${TIMESTAMP}.tar.bz2 debug.txt nohup.out
rm debug.txt nohup.out

# Start the server
nohup ./bin/minetestserver --gameid minetest --world world &

# Wait for it to start
sleep 10

# Renice running process to 10
renice 10 $(pidof minetestserver)

echo "All done"



Backup script:
#!/bin/bash
#
# Place script in root of minetest run in place and make executable
#
# Make sure to make directory for worldbackups and alter any variables as needed
#
# The startup command expects world to be in rootfolder/world
# Change as needed/wanted

#terminate minetestserver
kill $(pidof minetestserver)

# Wait
sleep 10

# Variable for logsback directory
LOGSBAK=`$HOME/.minetest/logsbak`

# here I am setting a time stamp variable which I like to use for logging
TIMESTAMP=`date +%Y%m%d.%H%M`

# here I am setting up the backup directory as a variable
DEST_DIR="$HOME/.minetestworldbackups"

# here I am setting up the directory in which I want to backup, again another variable
SRC_DIR="$HOME/.minetest"

# let's create a variable for the backup file name file
FNAME="minetestbackup"

# let's create a variable for the log file, let's also name the log file with the filename and timestamp it
LOG="$HOME/.minetestworldbackups/$FNAME-$TIMESTAMP.log"

# start the backup, create a log file which will record any messages run by this script
echo -e "Starting backup of minetest $SRC_DIR directory" >> ${LOG}

# compress the directory and files, direct the tar.gz file to your destination directory
tar -vczf ${DEST_DIR}/${FNAME}-${TIMESTAMP}.tar.gz ${SRC_DIR} >> ${LOG}

# end the backup, append to log file created by this script
echo -e "Ending backup of minetest $SRC_DIR" >> ${LOG}

# Cleanup
tar -cvjf ${LOGsBAK}/logback-${TIMESTAMP}.tar.bz2 debug.txt nohup.out
rm debug.txt nohup.out

# Start the server
nohup ./bin/minetestserver --gameid minetest --world world &

# Wait
sleep 10

# Renice running process
renice 10 $(pidof minetestserver)

echo "all done.."

PostPosted: Sun Jan 20, 2013 22:47
by vibe-x
I've done a script too. Maybe it will help someone.
It checks for existing minetest process.

1. No minetest started, no pid file -> start
2. minetest started, pidfile already exists -> check for process (NO process existing) -> start new one, overwrite pid file
3. minetest started, pidfile already exists -> check for process (process existing) -> do not start

You can define log file, if empty, it wont create any one.
Maybe you can use it:

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
#!/bin/bash


#PARAMETERS

#Directory to run in
WORKDIR="/home/minetest/minetest-0.4.4-d1-linux"
#Pifile
PIDFILE="status.pid"
# Screen output log
SCREENLOG="/dev/null"

# how many tries to kill (close)
KILL_TIMES="5"

# Binary to run
MT_BIN="bin/minetestserver"
# minetest Game id
MT_GAMEID="minetest_game"
# empty if no log!
MT_LOG="server.log"

function start_mt() {
        if [ -z $MT_LOG ];then
                nohup $MT_BIN --gameid $MT_GAMEID --logfile '' > $SCREENLOG 2>&1 &
        else
                nohup $MT_BIN --gameid $MT_GAMEID --logfile $MT_LOG > $SCREENLOG 2>&1 &
        fi
        echo $! > $PIDFILE
}

function do_start() {
        if [ -e $PIDFILE ];then
                kill -0 `cat $PIDFILE` 2>&1 > /dev/null
                if [ $? -eq 0 ];then
                        echo "MineTest already running! Nothing to do!"
                else
                        echo "Pidfile found, but no MineTest running!, Starting ..."
                        echo "Starting ..."
                        rm $PIDFILE
                        start_mt
                fi
        else
                echo "Starting"
                start_mt
        fi
}

function do_stop() {
        if [ -e $PIDFILE ]; then
                tmp_count="1"
                kill -0 `cat $PIDFILE`
                while [ $? -eq 0 ];do
                        if [ $tmp_count -ge 5 ];then
                                kill `cat $PIDFILE`
                                let tmp_count=$tmp_count+1
                        else
                                kill -9 `cat $PIDFILE`
                                break;
                        fi
                        kill -0 `cat $PIDFILE`
                done
                rm $PIDFILE
        else
                echo "No Pidfile found, nothing to do!"
        fi
}

cd $WORKDIR

case $1 in
        start)
                do_start;;
        stop)
                do_stop;;
        *)
                echo "$0 {start|stop}";;
esac



I will update status and so on. if someone has any idea to make things better, tell me ;)

PostPosted: Mon Jan 21, 2013 03:51
by RAPHAEL
Bring on the helpful scripts! lol (can never have too many variations of bash scripts)