[BACK]Return to mtctl_default.sh CVS log [TXT][DIR] Up to [local] / mtctl

Annotation of mtctl/mtctl_default.sh, Revision 1.3

1.1       bountyht    1: #---------------------------------------------------------------------
                      2: # Filename: mtctl
                      3: # Purpose: Minetest server Control
                      4: # License: Copyright (C) 2021 by Miniontoby <miniontoby@ircnow.org>
                      5: #---------------------------------------------------------------------
1.3     ! minionto    6: VERSION="1.2"
1.2       bountyht    7: config=[]
                      8: config[0]="no"; config[1]="/usr/local/share/minetest/"
1.1       bountyht    9:
                     10: #---------------------------------------------------------------------
                     11: # Alias
                     12:
                     13: mtinfo() {
                     14:        MSG="$1"
                     15:        echo -e "mtctl: $MSG" | sed -e 's/\\n/\\nmtctl: /'
                     16: }
                     17: mthelp() {
1.3     ! minionto   18:        echo -e "usage:  mtctl start|stop|restart|status|create|backup worldname\n        mtctl list|help|version|check_updates"; exit 1
1.1       bountyht   19: }
1.2       bountyht   20: if [ "x$1" == "x" ]; then mthelp; exit 1; fi
1.1       bountyht   21:
                     22:
                     23: #---------------------------------------------------------------------
1.2       bountyht   24: # Setup
                     25:
                     26: if [[ "`id -u`" -eq 0 ]]; then CONFDIR=/etc/mtctl; else CONFDIR=$HOME/.mtctl; fi
                     27: if [ \! -d $CONFDIR ]; then mkdir -p $CONFDIR || exit 1; fi
                     28: if [ \! -f $CONFDIR/config ]; then
                     29:        mtinfo "\nNo configfile found!\nCreating it now!\n\n"
                     30:        echo -n 'Use custom build minetest folder (yes/no): '; read MTBUILD
                     31:        case $MTBUILD in
                     32:                YES|yes) MTBUILD='yes'
                     33:                        EXAMPLEDIR="$HOME/minetest"
                     34:                        ;;
                     35:                NO|no) MTBUILD='no'
                     36:                        EXAMPLEDIR="$HOME/.minetest"
                     37:                        ;;
                     38:                *) mtinfo "Error, not a valid option!"
                     39:                        exit 1
                     40:                        ;;
                     41:        esac
                     42:        echo -n "Insert the path to the minetest folder(eg. $EXAMPLEDIR): "; read MTLOCATION
                     43:
                     44:        echo -e "builded=$MTBUILD\nlocation=$MTLOCATION" > $CONFDIR/config
                     45:        unset MTBUILD MTLOCATION EXAMPLEDIR
                     46:        mtinfo "Configfile created!!\n\n"
                     47: fi
                     48:
                     49: while read line; do
                     50:        linea="`echo $line | grep -F = 2> /dev/null`"
                     51:        if [ "x$linea" \!= "x" ]; then
                     52:                varname=$(echo "$line" | cut -d '=' -f 1)
                     53:                case $varname in
                     54:                        builded) indexnumber=0
                     55:                                ;;
                     56:                        location) indexnumber=1
                     57:                                ;;
                     58:                esac
                     59:                config[$indexnumber]=$(echo "$line" | cut -d '=' -f 2-)
                     60:        fi
                     61: done < $CONFDIR/config
1.1       bountyht   62:
1.2       bountyht   63: mtsetup() {
                     64:        EXITDIR=${config[1]}/tmp
                     65:        if [ "${config[0]}" == "yes" ]; then SERVEREXE="${config[1]}/bin/minetestserver"; else SERVEREXE="`which minetestserver`"; fi
                     66:        WORLDBASE=${config[1]}/worlds
                     67:        LOGDIR=${config[1]}/log
                     68:        EXITFLAGALL=$EXITDIR/minestop.all
                     69:        OK=0; cd ${config[1]} || exit 1
                     70:        touch temp.test && OK=1; if [ "@$OK" == "@0" ]; then mtinfo "Error: Directory tree should be owned by the MT user:\n${config[1]}"; exit 1; fi
                     71:        rm temp.test || exit 1
                     72:        if [ \! -f $SERVEREXE ]; then mtinfo "Error: Couldn't determine SERVEREXE setting\n$SERVEREXE"; exit 1; fi
                     73:
                     74:        mkdir -p $EXITDIR || exit 1
                     75:        mkdir -p $WORLDBASE || exit 1
                     76:        mkdir -p $LOGDIR || exit 1
                     77: }
1.1       bountyht   78:
                     79: #---------------------------------------------------------------------
                     80: # Normal Functions
                     81:
                     82: GetPIDS() {
                     83:        FOO=`ps ax | grep $SERVEREXE | grep " $1" | grep -e "--port" | sed -e "s/^ *//" -e "s/ .*//"`; BAR=`ps ax | grep "mtctl start *$2"\$ | grep -v grep | grep -v "^ *$$ " | sed -e "s/^ *//" -e "s/ .*//"`
                     84: }
                     85:
                     86: SetWorld() {
                     87:        NAME=$1; if [ "x$NAME" == "x" ]; then mthelp; exit 1; fi
                     88:        WORLDDIR=$WORLDBASE/$NAME; if [ \! -d $WORLDDIR ]; then mtstatus "Failed"; mtinfo "Error: World not found: $NAME"; exit 1; fi
                     89:        PORT=`grep '^port.*=' $WORLDDIR/world.conf | sed -e "s/^.*= *//" -e "s/ .*//"`; if [ "x$PORT" == "x" ]; then mtstatus "Failed"; mtinfo "Error: Port for $NAME not defined in cfg file"; exit 1; fi
                     90: }
                     91: mtstatus() {
                     92:        CHECK="$1"
                     93:        EXTRA=""
                     94:        if [ "$CHECK" == "Ok" ]; then
                     95:                GetPIDS "$WORLDDIR" "$NAME"
                     96:                if [ "x$FOO$BAR" \!= "x" ]; then
                     97:                        CHECK="Online"
                     98:                else
                     99:                        CHECK="Offline"
                    100:                fi
                    101:                EXTRA="\n"
                    102:        else if [ "$CHECK" == "Failed" ]; then EXTRA="  \n"; fi; fi
                    103:        echo -ne "\r$NAME($CHECK)$EXTRA"
                    104: }
                    105:
                    106: #---------------------------------------------------------------------
                    107: # Functions for executing the actions
                    108:
                    109: startMT() {
                    110:        SetWorld "$1"; mtstatus
                    111:        GetPIDS "$WORLDDIR" "$NAME"
                    112:        BAR=`echo $BAR | wc -l`
                    113:        if [ "x$FOO" \!= "x" ]; then
                    114:                mtstatus "Failed"
                    115:                mtinfo "Error: World seems already to be running\nIf it is not running, run $ mctl stop $NAME to be sure"
                    116:                exit 1
                    117:        fi
                    118:
                    119:        EXITFLAGWORLD=$EXITDIR/minestop.$NAME
1.2       bountyht  120:        MINETEST_SUBGAME_PATH=${config[1]}/games
1.1       bountyht  121:        # MAYBE Export if linux doesnt work
                    122:
                    123:        rm -f $EXITFLAGALL $EXITFLAGWORLD
                    124:        sleep 1; DIR=`pwd` || exit 1; cd $WORLDDIR || exit 1
                    125:        F1=env_meta.txt; F2=env_meta.old; if [ -s $F1 ]; then N=`grep EnvArgsEnd $F1 | wc -l` || exit 1; if [ "x$N" == "x0" ]; then rm -f $F1 || exit 1; fi; fi; if [ -s $F1 ]; then rm -f $F2; cp -p $F1 $F2 || exit 1; else if [ -s $F2 ]; then rm -f $F1; cp -p $F2 $F1 || exit 1; else rm -f $F1 || exit 1; fi; fi
                    126:        cd $DIR || exit 1
                    127:
                    128:        (
                    129:        while true; do
                    130:                $SERVEREXE --config $WORLDDIR/world.conf --port $PORT --logfile $LOGDIR/debug-$NAME.log --map-dir $WORLDDIR >> $LOGDIR/$NAME.log 2>&1
                    131:                if [ -f $EXITFLAGALL ]; then exit 0; fi; if [ -f $EXITFLAGWORLD ]; then exit 0; fi; sleep 10
                    132:        done
                    133:        ) > $LOGDIR/start-$NAME.txt &
                    134:        mtstatus "Ok"
                    135: }
                    136:
                    137: stopMT() {
                    138:        SetWorld "$1"; mtstatus
                    139:        GetPIDS "$WORLDDIR" "$NAME"
                    140:        if [ "x$FOO$BAR" == "x" ]; then mtstatus "Failed"; mtinfo "$NAME seems to be stopped already"; exit 0; fi
                    141:        if [ "x$FOO" \!= "x" ]; then kill -SIGINT $FOO 2> /dev/null ; sleep 1; kill -SIGINT $FOO 2> /dev/null ; sleep 1; kill -SIGINT $FOO 2> /dev/null ; sleep 1; kill -SIGINT $FOO 2> /dev/null ; fi
                    142:        if [ "x$BAR" \!= "x" ]; then kill -9 $BAR; fi
                    143:
                    144:        mtstatus "Waiting"
                    145:        sleep 15; GetPIDS "$WORLDDIR" "$NAME"
                    146:
                    147:        if [ "x$FOO$BAR" == "x" ]; then
                    148:                mtstatus "Ok"
                    149:        else
                    150:                kill -9 $FOO; kill -9 $FOO; kill -9 $FOO; kill -9 $FOO;
                    151:                if [ "x$BAR" \!= "x" ]; then kill -9 $BAR; fi
                    152:
                    153:                mtstatus "Waiting."
                    154:                sleep 15; GetPIDS "$WORLDDIR" "$NAME"
                    155:                if [ "x$FOO$BAR" == "x" ]; then
                    156:                        mtstatus "Ok"
                    157:                else
                    158:                        mtstatus "Failed"; mtinfo "Error: Stop of $NAME failed\nTry once more, then consult a sysadmin"; exit 1
                    159:                fi
                    160:        fi
                    161: }
                    162:
                    163: restartMT() {
                    164:        SetWorld "$1"
                    165:        /usr/bin/mtctl stop "$1"|| exit 1
                    166:        /usr/bin/mtctl start "$1" || exit 1
                    167: }
                    168:
                    169: statusMT() {
                    170:        SetWorld "$1";
                    171:        mtstatus "Ok"
                    172: }
                    173:
                    174: listMT() {
                    175:        for WORLDNAME in `ls $WORLDBASE 2> /dev/null | grep '^[a-zA-Z0-9]*$' | sort`; do
                    176:                SetWorld "$WORLDNAME"
                    177:                mtinfo "World: $WORLDNAME Port: $PORT"
                    178:        done
                    179: }
                    180:
                    181: createMT() {
                    182:        NAME=$1; if [ "x$NAME" == "x" ]; then mthelp; exit 1; fi
                    183:        if [ "x$NAME" == "x" ]; then echo -e "Failed\nError: Worlds need a name\n"; exit 1; else if [ -d $WORLDBASE/$NAME ]; then echo -e "Failed\nError: World already exist\n"; exit 1; else echo -e "Server name: Ok"; fi; fi
                    184:        echo -ne "\nServer Description: "; read SERVER_DESCRIPTION
                    185:        PortCheck () {
                    186:                CHECK='yes'; if [ -z "${PORT##*[!0-9]*}" ]; then CHECK='no'; ERROR="Ports are only numeric"; return; fi; for x in `ls /home/$MTUSER/minetest/worlds | sort`; do export WCFILE=$WORLDBASE/$x/world.conf; if [ -f $WCFILE ]; then export PORTF=`grep '^port.*=' $WCFILE | sed -e "s/^.*= *//" -e "s/ .*//"`; if [ "x$PORTF" == "x$1" ]; then CHECK='no'; ERROR="Port already in use"; return; fi; fi; done
                    187:        }
                    188:        while true; do echo -ne "\nPort: "; read PORT; PortCheck $PORT; if [ "$CHECK" == "no" ]; then echo -e "Failed\nError: $ERROR"; sleep 1; else echo -e "Ok"; break; fi; done
                    189:        echo -ne "\nMessage Of The Day [MOTD]: "; read MOTD; echo -ne "\nSeed: "; read SEED; echo -ne "\nCreative (true/false): "; read CREATIVE_MODE
                    190:        echo -ne "\nEnable Damage (true/false): "; read ENABLE_DAMAGE; echo -ne "\nEnable Player Versus Player [PVP] (true/false): "; read ENABLE_PVP
                    191:        echo -ne "\nUsername: "; read USERNAME
                    192:
                    193:        cd $WORLDBASE; mkdir $NAME; cd $NAME
1.3     ! minionto  194:        echo -e "server_name = $NAME\nserver_description = $SERVER_DESCRIPTION\nport = $PORT\nmotd = $MOTD\nfixed_map_seed = $SEED\ncreative_mode = $CREATIVE_MODE\nenable_damage = $ENABLE_DAMAGE\nenable_pvp = $ENABLE_PVP\nname = $USERNAME\nserver_announce = true\nserverlist_url = servers.minetest.net\nsqlite_synchronous = 0\nserver_unload_unused_data_timeout = 900\nserver_map_save_interval = 900.0" > world.conf
1.1       bountyht  195:        echo -e "creative_mode = $CREATIVE_MODE\nenable_damage = $ENABLE_DAMAGE\nauth_backend = sqlite3\nbackend = sqlite3\nplayer_backend = sqlite3\ngameid = minetest\nworld_name = $NAME" > world.mt
                    196:        echo -ne "\nWorld created!\nWant to start the world?(yes/no): "; read startit
                    197:        if [[ "$startit" == "yes" ]]; then /usr/bin/mtctl start $NAME; fi
                    198:        echo -e "\nSuccess: \033[1;32mDone \033[m\nFeel free to join your new server at port $PORT"
                    199: }
                    200:
1.3     ! minionto  201: backupMT() {
        !           202:        SetWorld "$1"
        !           203:        # Backup script from mtlbak by Minix
        !           204:        # Copyright (C) 2021 Minix from FreedomTest (freedomtest@protonmail.com)
        !           205:
        !           206:        BACKUP_DIR="$CONFDIR/backups/$NAME"
        !           207:        LOG_FILE="$LOGDIR/backup_$NAME.log"
        !           208:        mkdir -p $BACKUP_DIR
        !           209:        echo -ne "" >> $LOG_FILE
        !           210:
        !           211:        if [ -f /tmp/$(basename $WORLDDIR)_local_backup_in_progress ]; then
        !           212:                echo "\nBackup for $(basename $WORLDDIR) on $(date) aborted because another backup is already in progress, this may be caused by a backup that is taking longer than expected" >> $LOG_FILE
        !           213:                mtinfo "Backup for $(basename $WORLDDIR) on $(date) aborted because another backup is already in progress, this may be caused by a backup that is taking longer than expected"
        !           214:                exit 1
        !           215:        fi
        !           216:
        !           217:        touch /tmp/$(basename $WORLDDIR)_local_backup_in_progress
        !           218:        rm $BACKUP_DIR/map.sqlite.tmp* 2> /dev/null #Cleaning in case of crashed attempts
        !           219:
        !           220:        echo -e "\n$(date) backup started" >> $LOG_FILE
        !           221:        mtstatus "Backup started"
        !           222:
        !           223:        try=0
        !           224:        while [ $try -lt 5 ]; do
        !           225:                echo "Starting backup attempt #$(expr $try + 1) on $(date)" >> $LOG_FILE
        !           226:                sqlite3 $WORLDDIR/map.sqlite ".backup $BACKUP_DIR/map.sqlite.tmp" 2> /dev/null
        !           227:                if [ $? -eq 0 ]; then
        !           228:                        mv $BACKUP_DIR/{map.sqlite.tmp,map.sqlite}
        !           229:                        #Backup auth.sqlite and players.sqlite files properly
        !           230:                        until sqlite3 $WORLDDIR/auth.sqlite ".backup $BACKUP_DIR/auth.sqlite" 2> /dev/null; do
        !           231:                                continue
        !           232:                        done
        !           233:                        until sqlite3 $WORLDDIR/players.sqlite ".backup $BACKUP_DIR/players.sqlite" 2> /dev/null; do
        !           234:                                continue
        !           235:                        done
        !           236:                        rsync -ptrW --delete --exclude "*.sqlite" $WORLDDIR/ $BACKUP_DIR/
        !           237:                        echo "Backup finished succesfully on $(date)" >> $LOG_FILE
        !           238:                        mtstatus "Backup success"
        !           239:                        rm /tmp/$(basename $WORLDDIR)_local_backup_in_progress 2> /dev/null
        !           240:                        exit 0
        !           241:                else
        !           242:                        rm $BACKUP_DIR/map.sqlite.tmp 2> /dev/null
        !           243:                        try=$(expr $try + 1)
        !           244:                        if [ $try -eq 5 ]; then
        !           245:                                echo "Backup failed 5 times, aborting on $(date)" >> $LOG_FILE
        !           246:                                mtstatus "Backup failed"
        !           247:                                rm /tmp/$(basename $WORLDDIR)_local_backup_in_progress 2> /dev/null
        !           248:                                exit 1
        !           249:                        else
        !           250:                                echo "map.sqlite backup attempt #$try failed, retrying in 60 seconds" >> $LOG_FILE
        !           251:                                sleep 60
        !           252:                        fi
        !           253:                fi
        !           254:        done
        !           255: }
        !           256:
        !           257:
1.1       bountyht  258: #---------------------------------------------------------------------
                    259: # Handle the actions
1.2       bountyht  260: ACTION="$1"
1.1       bountyht  261:
                    262: case $ACTION in
                    263:        start)
1.2       bountyht  264:                mtsetup
1.1       bountyht  265:                startMT "$2"
                    266:                ;;
                    267:        stop)
1.2       bountyht  268:                mtsetup
1.1       bountyht  269:                stopMT "$2"
                    270:                ;;
                    271:        restart)
1.2       bountyht  272:                mtsetup
1.1       bountyht  273:                restartMT "$2"
                    274:                ;;
                    275:        status)
1.2       bountyht  276:                mtsetup
1.1       bountyht  277:                statusMT "$2"
                    278:                ;;
                    279:        list)
1.2       bountyht  280:                mtsetup
1.1       bountyht  281:                listMT
                    282:                ;;
                    283:        create)
1.2       bountyht  284:                mtsetup
1.1       bountyht  285:                createMT "$2"
                    286:                ;;
                    287:        version)
                    288:                mtinfo "Version: $VERSION"
                    289:                ;;
                    290:        check_updates)
                    291:                NEWESTVERSION=$(curl https://cvsweb.planetofnix.com/cgi-bin/cvsweb/~checkout~/mtctl/version.txt?content-type=text/plain 2> /dev/null)
                    292:                if [ "$NEWESTVERSION" \!= "$VERSION" ]; then
                    293:                        mtinfo "Update avaible!\n\nInstalling update NOW!"
1.3     ! minionto  294:                        curl -sSLk https://ircforever.org/mtctl.php | $SHELL
1.1       bountyht  295:                fi
1.2       bountyht  296:                ;;
1.1       bountyht  297:        help)
                    298:                mthelp
                    299:                ;;
                    300:        *)
                    301:                mthelp
                    302:                ;;
                    303: esac
                    304:
                    305:
                    306: #---------------------------------------------------------------------
                    307: # Final

CVSweb