Saturday, July 4, 2020

BATCH FILE RE-STARTER & BACKUP

I run an elaborate batch-file re-starter. It monitors the server for functionality, if it crashes, it will auto reboot, with an option to cancel it. This file works with the backup.bat file. Both of these are modified versions of my BLAZE batch files.

Please do not post this script anywhere else without credit, permission, or linkback


STARTSERVER.BAT FILE

@echo off
:: ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: :::: B L A Z E  S T A R T S E R  E R . B A T          ::::
:: :::: THIS MINECRAFT SERVER RE-STARTER SHOULD BE USED  ::::
:: :::: IN CONJUNCTION WITH THE BLAZE BACKUP.BAT         ::::
:: ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

title Minecraft Server Starter

:: NEED ABSOLUTE PATH TO MINECRAFT SERVER
c:
cd "\xampp\htdocs\blaze\mcserver\"

:: LOAD UP THE BATCH FILE
start /min "SERVER_BACKUP" backup.bat

:LOAD_SERVER
cls
echo   @@@@@@ @@@@@@@@ @@@@@@@  @@@  @@@ @@@@@@@@ @@@@@@@      
echo  !@@     @@!      @@!  @@@ @@!  @@@ @@!      @@!  @@@     
echo   !@@!!  @!!!:!   @!@!!@!  @!@  !@! @!!!:!   @!@!!@!      
echo      !:! !!:      !!: :!!   !: .:!  !!:      !!: :!!      
echo  ::.: :  : :: :::  :   : :    ::    : :: :::  :   : :     
echo.                                                          
echo  @@@@@@@  @@@  @@@ @@@  @@@ @@@  @@@ @@@ @@@  @@@  @@@@@@@
echo  @@!  @@@ @@!  @@@ @@!@!@@@ @@!@!@@@ @@! @@!@!@@@ !@@     
echo  @!@!!@!  @!@  !@! @!@@!!@! @!@@!!@! !!@ @!@@!!@! !@! @!@!@
echo  !!: :!!  !!:  !!! !!:  !!! !!:  !!! !!: !!:  !!! :!!   !!:
echo   :   : :  :.:: :  ::    :  ::    :  :   ::    :   :: :: :
echo.                                                                           

:: LET BACKUP BATCH KNOW THE SERVER IS UP (SORTA)
echo "server up" > server.up

:: LOAD YOUR SERVER HERE
start /wait "BLAZEMCPANEL" java.exe -Xmx4048M -Xms4048M  -jar minecraft_server.jar nogui
echo.

:: SYSTEM COMES HERE WHEN THE SERVER STOPS
:: WHAT WE DO HERE IS DO A TIME OUT INCASE THE EXIT WAS INTENTIONAL,
:: IF NOT IT WILL BE TREATED AS A CRASH
:: ALLOW 10 SECOND DELAY,
echo Server stoppped!! Server will reload in 10 seconds,
echo PRESS ANY KEY TO EXIT SERVER
call :controlTimeout 15
if errorlevel 1 (
    GOTO EXIT_SERVER
) else (
    GOTO CRASH
)
exit /b

:controlTimeout
setlocal
start "" /belownormal /b cmd /q /d /c "timeout.exe %~1 /nobreak > nul"
timeout.exe %~1 & tasklist | find "timeout" >nul
if errorlevel 1 ( set "exitCode=0" ) else (
    set "exitCode=1"
    taskkill /f /im timeout.exe 2>nul >nul
)
endlocal & exit /b %exitCode%

:CRASH
:: ADD TO LOG HERE IF CTRL-X WAS NOT PRESED
set this_date = date /t
echo Server Crashed on !this_date! >> crash.log

:: IF NOT, IT MUST HAVE BEEN A CRASH, RELOAD THE SERVER
GOTO LOAD_SERVER

:EXIT_SERVER

del /q server.up
exit 0


Please do not post this script anywhere else without credit, permission, or linkback


BACKUP.BAT
REQUIRED: 7z.exe & 7z.dll in your MC Server Directory
@echo off

:: ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: ::::         B L A Z E  B A C K U P . B A T           ::::
:: :::: THIS MINECRAFT SERVER RE-STARTER SHOULD BE USED  ::::
:: :::: IN CONJUNCTION WITH THE BLAZE STARTUP.BAT        ::::
:: ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: AUTHOR: R.SCORPIO rickscorpio64@gmail.com
:: WEBSITE: https://rustycraftmc.blogspot.com/

setlocal EnableDelayedExpansion
title "BACKUP SERVER"

:: SERVER BACKUP SECTION
:: I do not use a "date" format, as the files when created already have a timestamp
:: which you can see in the file explorer




:: ----------- EDIT THIS SECTION ------------------------------------

    :: put the name of the world you want to backup
    set worldname=world
    :: 600 = 10min, 900 = 15min, 1200 = 20min (add 300 seconds/5min)
    set interval=300
    :: BACKUP folder name
    set backupFolder=backups
    :: Set number of backups before counter reset. 5-10 is good for a small server
    set max_backups=25
    :: Starting counter. Really no reason to change this. Used for routine below
    set backupcount=1
   
:: ---- DO NOT EDIT PAST HERE ---------------------------------------
:backup
cls
echo "    _____                                           
echo " __|___  |__  ______  _____  __    _ ______  _____  
echo "|   ___|    ||   ___||     |\  \  //|   ___||     | 
echo " `-.`-.     ||   ___||     \ \  \// |   ___||     \ 
echo "|______|  __||______||__|\__\ \__/  |______||__|\__\
echo "   |_____|                                          
echo "    _____                                           
echo " __|___  |__  ____    ______  __  __  __   _  _____ 
echo "|      >    ||    \  |   ___||  |/ / |  | | ||     |
echo "|     <     ||     \ |   |__ |     \ |  |_| ||    _|
echo "|______>  __||__|\__\|______||__|\__\|______||___|  
echo "   |_____|                                          
                                                
echo.
:: if server.up exist, we can keep doing backups
:: this is a method used so backups do not continue if
:: the server is down.

echo Checking archive count
for %%x in ("%backupFolder%\*.7z") do (set /a "backupcount=!backupcount!+1")
if !backupcount! gtr %max_backups% ( set backupcount=1 )   

echo Copying world to backup folder.
xcopy "%worldname%\*.*" %backupFolder% /s /e /q /y

:: regarding the "^^" - I am using the setlocal EnableDelayedExpansion,
:: this makes the "!" has a special meaning and overrides the 7z ! symbol
:: the ^^ escapes the "!" so it can be used correctly
echo Archiving backup # !backupcount!
echo Processing: %backupFolder%\serverBackup_%backupcount%.7z %backupFolder%
7z a  %backupFolder%\serverBackup_%backupcount%.7z %backupFolder% -xr^^!*.7z > nul
echo DONE!
:: We need to delay interval, but we also need to check to see if the
:: server.up has left, if so, break out of batch file
set counter=0

:timer_loop
:: This loop is designed to pause 1 (ish) sec for "interval" seconds (see above)
:: each iteration(second) will check to see if server.up is still present
:: if not, then the server is down and exit the backup
if exist server.up (

    PING 0.0.0.0 -n 2 > nul
    set /a "counter=!counter!+1"
    if !counter! LSS %interval% GOTO timer_loop       
   
) else (GOTO quit)

GOTO backup



:: if the server.up does not exist, exit backup routine after it finishes loop
:: this way backup is not running while there is no live server
:quit
exit 0


 LIVE VIEW