HOW TO: Ready-To-Go Backup and Cleardown script

This section contains user-submitted tutorials.
signz
New user
New user
Posts: 29
Joined: 2022-01-29 21:05

Re: HOW TO: Ready-To-Go Backup and Cleardown script

Post by signz » 2022-03-18 14:36

Perfect.

Thank you.

User avatar
jimimaseye
Moderator
Moderator
Posts: 10053
Joined: 2011-09-08 17:48

Re: HOW TO: Ready-To-Go Backup and Cleardown script

Post by jimimaseye » 2022-03-19 11:52

signz wrote:
2022-03-18 14:36
Perfect.

Thank you.
How did it go last night after the changes?
5.7 on test.
SpamassassinForWindows 3.4.0 spamd service
AV: Clamwin + Clamd service + sanesecurity defs : https://www.hmailserver.com/forum/viewtopic.php?f=21&t=26829

signz
New user
New user
Posts: 29
Joined: 2022-01-29 21:05

Re: HOW TO: Ready-To-Go Backup and Cleardown script

Post by signz » 2022-03-19 22:33

Looking at my logs. All is being copied as expected. HMailserver stops and starts fine and zip is present and all correct.

Top job.

signz
New user
New user
Posts: 29
Joined: 2022-01-29 21:05

Re: HOW TO: Ready-To-Go Backup and Cleardown script

Post by signz » 2022-03-20 11:36

I've also change I bit of my code to read
:3rd
set section=3rd
if "%FailedHmailservice:~0,14%" == "Failed to stop" goto 5th
robocopy "%MAILDATAdir%" "%BACKUPTEMPdir%"\Data /mir /ndl /r:43200 /np /w:1 >> %BackLog%
goto ROBOerrorcheck

:4th
if "%DBtype%" == "mysql" (
erase /Q "%BACKUPTEMPdir%"\Data\*.mysql >> %BackLog%
"%MYSQLBINdir%"\mysqldump -u"%MYSQLDBuser%" -p"%MYSQLDBpass%" -q -A -l --add-drop-table -PMYSQLDBport >"%BACKUPTEMPdir%\Data\MYSQLDump_%inDate%.mysql"
) ELSE (
xcopy /f /I /Y "%HMAILSERVERprogdir%"\Bin\hmailserver.ini "%BACKUPTEMPdir%"\Bin\ >> %BackLog%
robocopy "%InternalDATABASEdir%" "%BACKUPTEMPdir%"\Database /mir /ndl /r:43200 /np /w:1 >> %BackLog%
goto ROBOerrorcheck
)
Now the hmaildata folder is just called Data.

So when restoring it will save me time I can just unzip all strait into directory and overwrite the existing files.

signz
New user
New user
Posts: 29
Joined: 2022-01-29 21:05

Re: HOW TO: Ready-To-Go Backup and Cleardown script

Post by signz » 2022-03-24 14:26

Hi I have tweeked your code so the following just happens.

hmailserver stops
copies files to temp folder
hmailserver starts
Removed files older than 5 days
creates zip
completes log

Im not needing to use BLAT or Emailclearup.vbs. As I can Check Log for problems on our network system at anytime.

Is it correct to you?

Code: Select all

rem   #### CONFIG START ####
rem  *******  FILL OUT VARIABLES BELOW  ***************************

rem ---  SET LOCATIONS (without trailing '\')  ----
Set BACKUPdir=D:\Email_Backups\Backup Files
Set BACKUPTEMPdir=C:\EmailBackupTemp
set MAILDATAdir=C:\Program Files (x86)\hMailServer\data
set HMAILSERVERprogdir=C:\Program Files (x86)\hMailServer

rem  Spamassassin installed and in use: enter "yes" or "no" (lowercase!). Also set program directory if 'yes'.
set SA_In_Use=no
set SPAMASSASSINdir=C:\Program Files (x86)\JAM Software\SpamAssassin for Windows

rem  enter "internal" or "mysql" (lowercase!) as the database type in use
set DBtype=internal

rem ** IF DBtype = 'internal' set the following variables
set InternalDATABASEdir=C:\Program Files (x86)\hMailServer\Database

rem ** IF DBtype = MySQL set the following variables
set MYSQLBINdir=C:\Program Files\MySQL\MySQL Server 5.5\bin
set MYSQLDBuser=root
set MYSQLDBpass=yoursqlpassword
set MYSQLDBport=3306

rem ---  HOW MANY recent backup Zips do you want to retain?  ----
set DAYSZIPTOKEEP=5

rem  ****  FILL OUT VARIABLES ABOVE  *******************************
rem   #### CONFIG END ####

Set SCRIPTdir=%~dp0

for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set inDate=%ldt:~0,4%-%ldt:~4,2%-%ldt:~6,2%-%ldt:~8,6%
set BackLog="%BACKUPdir%\Backup_%inDate%.log"
set Failed=false
set FailedScript=Not performed
set FailedZip=Not performed
set FailedSAUpdate=Not performed
set FailedSAService=Not performed
set FailedDNSService=Not performed
set FailedHmailservice=Not performed
set outf="%SCRIPTdir%\body.html"

echo ^<table border='0'^> > %outf%

set colorfill=font color="red"
set fontBlack=!--
set noBold=!--

echo Backup Start: %date% %time% > %BackLog%

REM :: Perform backup to temporary directory

set section=1st

:1st
:maildata
echo %time% Stopping Hmailserver service...>> %BackLog%&net stop hmailserver >> %BackLog%
set FailedHmailservice=Ok
set bold=%noBold%&set fontcolor=%fontBlack%
if errorlevel 1 set Failed=true&set FailedHmailservice=Failed to stop. Backup of emails not performed!&set bold=b&set fontcolor=%colorfill%
echo %FailedHmailservice%! >> %BackLog%
echo ^<tr^>^<td^>^<%fontcolor%^>^<%bold%^>Hmailserver service shutdown:^</td^>^<td^>^<%fontcolor%^>^<%bold%^>%FailedHmailservice%^</td^>^</tr^> >>%outf%

:2nd
if not "%SA_In_Use%" == "yes" goto 3rd
echo %time% Stopping Spamassassin service...>> %BackLog%&net stop spamassassin >> %BackLog%
set FailedSAService=Ok
set bold=%noBold%&set fontcolor=%fontBlack%
if errorlevel 1 set Failed=true&set FailedSAService=Failed to stop&set bold=b&set fontcolor=%colorfill%
echo %FailedSAService%! >> %BackLog%
echo ^<tr^>^<td^>^<%fontcolor%^>^<%bold%^>Spamassassin service shutdown:^</td^>^<td^>^<%fontcolor%^>^<%bold%^>%FailedSAService%^</td^>^</tr^> >>%outf%

@rem Update Spamassassin before restarting service
if "%FailedSAService%" == "Failed to stop" goto 3rd
echo %time% Performing Spamassassin Update check...>> %BackLog%
set FailedSAUpdate=Ok
cd /D "%SPAMASSASSINdir%"
sa-update.exe -v --nogpg --channelfile UpdateChannels.txt >> %BackLog%
set bold=%noBold%&set fontcolor=%fontBlack%
if errorlevel 1 set Failed=true&set FailedSAUpdate=Failed&set bold=b&set fontcolor=%colorfill%
echo ^<tr^>^<td^>^<%fontcolor%^>^<%bold%^>Spam Assassin Def Update:^</td^>^<td^>^<%fontcolor%^>^<%bold%^>%FailedSAUpdate%^</td^>^</tr^> >>%outf%

:3rd
set section=3rd
if "%FailedHmailservice:~0,14%" == "Failed to stop" goto 5th
robocopy "%MAILDATAdir%" "%BACKUPTEMPdir%"\Data /mir /ndl /r:43200 /np /w:1 >> %BackLog%
goto ROBOerrorcheck

:4th
if "%DBtype%" == "mysql" (
   erase /Q "%BACKUPTEMPdir%"\Data\*.mysql >> %BackLog%
   "%MYSQLBINdir%"\mysqldump -u"%MYSQLDBuser%" -p"%MYSQLDBpass%" -q -A -l --add-drop-table -PMYSQLDBport >"%BACKUPTEMPdir%\Data\MYSQLDump_%inDate%.mysql"
) ELSE (
   xcopy /f /I /Y "%HMAILSERVERprogdir%"\Bin\hmailserver.ini "%BACKUPTEMPdir%"\Bin\ >> %BackLog%
   robocopy "%InternalDATABASEdir%" "%BACKUPTEMPdir%"\Database /mir /ndl /r:43200 /np /w:1 >> %BackLog%
   goto ROBOerrorcheck
)

:5th
@rem Restart Spamassassin and Hmailserver services
if not "%FailedSAService%" == "Ok" goto 6th
echo %time% Starting Spamassassin service...>> %BackLog%&net start spamassassin >> %BackLog%
set bold=%noBold%&set fontcolor=%fontBlack%
if errorlevel 1 set Failed=true&set FailedSAService=Failed to restart&set bold=b&set fontcolor=%colorfill%
echo %FailedSAService%! >> %BackLog%
echo ^<tr^>^<td^>^<%fontcolor%^>^<%bold%^>Spam Assassin Service startup: ^</td^>^<td^>^<%fontcolor%^>^<%bold%^>%FailedSAService%^</td^>^</tr^> >>%outf%

:6th
if not "%FailedHmailservice%" == "Ok" set section=7th & goto 7th
echo %time% Starting Hmailserver service... >> %BackLog%&net start hmailserver >> %BackLog%
set bold=%noBold%&set fontcolor=%fontBlack%
if errorlevel 1 set Failed=true&set FailedHmailservice=Failed to restart&set bold=b&set fontcolor=%colorfill%
echo %FailedHmailservice%! Errorlevel=%errorlevel% >> %BackLog%
echo ^<tr^>^<td^>^<%fontcolor%^>^<%bold%^>Hmailserver service startup:^</td^>^<td^>^<%fontcolor%^>^<%bold%^>%FailedHmailservice%^</td^>^</tr^> >>%outf%

REM --- Use the following sections 7th to 9th as example of adding other Data directories in the backup (remove the REM's)  ---
:7th
:Sagedata
rem robocopy "D:\SageAccountData" "%BACKUPTEMPdir%"\SageAccountData /mir /ndl /r:43200 /np /w:1 >> %BackLog%
rem goto ROBOerrorcheck

:8th
:Miscdata
rem robocopy "D:\Miscdata" "%BACKUPTEMPdir%"\Miscdata /mir /ndl /r:43200 /np /w:1 >> %BackLog%
rem goto ROBOerrorcheck

:9th
rem robocopy "D:\anotherdir" "%BACKUPTEMPdir%"\anotherdir /mir /ndl /r:43200 /np /w:1 >> %BackLog%
rem goto ROBOerrorcheck

REM --- End of Example sections  ---

:10th
REM::  Clear down existing backup Zips over DAYSZIPTOKEEP
echo. & echo Clearing old archives
forfiles.exe /s /p "%BACKUPdir%" /m *.* /d -%DAYSZIPTOKEEP% /c "cmd /c echo Deleting over %DAYSZIPTOKEEP% days....@path & del /q @path" >> %BackLog%

REM :: Zip temporary directory and email log file
cd /D "%HMAILSERVERprogdir%"\Bin >> %BackLog%
echo %date% %time% 7Zip Creating archive %BACKUPdir%\Serverdata_%inDate%.zip >> %BackLog%
set FailedZip=Ok
7za a -tzip "%BACKUPdir%\Serverdata_%inDate%" "%BACKUPTEMPdir%\*"
set bold=%noBold%&set fontcolor=%fontBlack%
if errorlevel 1 set Failed=true&set FailedZip=Failed&set bold=b&set fontcolor=%colorfill%
echo ^<tr^>^<td^>^<%fontcolor%^>^<%bold%^>Zip:^</td^>^<td^>^<%fontcolor%^>^<%bold%^>%FailedZip%^</td^>^</tr^> >>%outf%

:test
set result=%date% %time% Backup procedure FAILED!!! Zip: %FailedZip%, Script: %FailedScript%, SA Update: %FailedSAUpdate%, SA Service: %FailedSAService%, Hmailserver service: %FailedHmailservice%.
set resulttext="!!! Backup completed WITH ERRORS.  CHECK LOG FILE FOR DETAILS!!!"
if not %Failed%==false goto Testend
:success
set result=%date% %time% Backup 7Zip Archive created - no errors encountered.
set resulttext="Backup completed. See attachment log file."
goto Testend

:ROBOerrorcheck
if errorlevel 16 echo ***FATAL ERROR*** >> %BackLog% & goto end
if errorlevel 15 echo OKCOPY + FAIL + MISMATCHES + XTRA >> %BackLog% & goto end
if errorlevel 14 echo FAIL + MISMATCHES + XTRA >> %BackLog% & goto end
if errorlevel 13 echo OKCOPY + FAIL + MISMATCHES >> %BackLog% & goto end
if errorlevel 12 echo FAIL + MISMATCHES>> %BackLog% & goto end
if errorlevel 11 echo OKCOPY + FAIL + XTRA >> %BackLog% & goto end
if errorlevel 10 echo FAIL + XTRA >> %BackLog% & goto end
if errorlevel 9 echo OKCOPY + FAIL >> %BackLog% & goto end
if errorlevel 8 echo FAIL >> %BackLog% & goto end
if errorlevel 7 echo OKCOPY + MISMATCHES + XTRA >> %BackLog% & goto end
if errorlevel 6 echo MISMATCHES + XTRA >> %BackLog% & goto end
if errorlevel 5 echo OKCOPY + MISMATCHES >> %BackLog% & goto end
if errorlevel 4 echo MISMATCHES >> %BackLog% & goto end
if errorlevel 3 echo OKCOPY + XTRA >> %BackLog% & goto end
if errorlevel 2 echo XTRA >> %BackLog% & goto end
if errorlevel 1 echo OKCOPY  >> %BackLog% & goto end
if errorlevel 0 echo No Change >> %BackLog% & goto end
:end
if errorlevel 8 set Failed=true
if %section%==9th set section=10th
if %section%==8th set section=9th
if %section%==7th set section=8th
if %section%==6th set section=7th
if %section%==5th set section=6th
if %section%==4th set section=5th
if %section%==3rd set section=4th
if %section%==2nd set section=3rd
if %section%==1st set section=2nd
goto %section%

:Testend
if not %FailedZip%==Failed forfiles /p "%BACKUPdir%" /m Serverdata_%inDate%.zip /d +0 /c "cmd /c echo Created: @path @fsize" >> %BackLog%
echo ^<tr^>^<td^>^<br^>^</td^>^</tr^>^</table^>See attached log file below for details. >>%outf%
echo %result% >> %BackLog%

:Finish
Thank you in advance

User avatar
jimimaseye
Moderator
Moderator
Posts: 10053
Joined: 2011-09-08 17:48

Re: HOW TO: Ready-To-Go Backup and Cleardown script

Post by jimimaseye » 2022-03-24 15:30

Think so (on a quick read).
5.7 on test.
SpamassassinForWindows 3.4.0 spamd service
AV: Clamwin + Clamd service + sanesecurity defs : https://www.hmailserver.com/forum/viewtopic.php?f=21&t=26829

Post Reply