You dont test for errors in your service stop/starting, copying or the zipping. And I KNOW that it is important because I have seen rare occasions where errors have occurred (despite it working normally day after day).
Here is a
script that does pretty much the same as you are suggesting but with notable additions:
1, Use robocopy mirroring to a temporary 'copy' directory of the data directory. It is this temporary directory that is then Zipped.
2, ERROR CHECKING and response after each action
3, added spamassassin definition update (you can REMove if not applicable)
4, This 7zip command is slightly different from your original (amend as you wish)
5, My date/timestamp is UK formatted, not US.
I have 9.5GB data directory and with this method my Mailservice is
offline for 30 seconds (is that too much?) whilst ensuring FULL integrity of mail files+database
Note this is a HEAVILY cutdown version without any whistles or bells. The full version does trash email cleardowns, offsite storage uploading and does nice little emailing bits and bobs with colours and twinkles reporting on the results (but I have removed this functionality as you wanted BASIC. So I
may have left in the
script some things that seemingly dont have any purpose - I was rushing).
Anyway, FWIW:
Code: Select all
rem --- SET LOCATIONS (without trailing '\') ----
Set BACKUPdir=D:\Backups
Set BACKUPTEMPdir=D:\BackupTemp
set DATABASEEXEdir=C:\Program Files\MySQL\MySQL Server 5.5\bin
set MAILDATAdir=C:\Program Files (x86)\hMailServer\Data
set SPAMASSASSINdir=C:\Program Files (x86)\JAM Software\SpamAssassin for Windows
set 7ZIPdir=C:\Program Files\7-Zip
rem **** FILL OUT VARIABLES ABOVE **** -----------------------------
set str=%time:~0,8%&rem
set str=%str: =0%
set str=%str::=%
set inDate=%date:~6,4%-%date:~3,2%-%date:~0,2%-%str%
REM The above set for UK region, due to date format differences.
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 section=1st
:1st
:maildata
net stop hmailserver
set FailedHmailservice=Ok
if errorlevel 1 set Failed=true& set FailedHmailservice=Failed to stop
:2nd
net stop spamassassin
set FailedSAService=Ok
if errorlevel 1 set Failed=true& set FailedSAService=Failed to stop
:3rd
set section=3rd
if "%FailedHmailservice%" == "Failed to stop" goto 5th
robocopy "%MAILDATAdir%" %BACKUPTEMPdir%\hMailData /mir /ndl /r:43200 /np /w:1
goto ROBOerrorcheck
:4th
cd %DATABASEEXEdir%
mysqldump -u"root" -p"itsasecret" -q -A -l --add-drop-table -P3306 >"%BACKUPTEMPdir%\hMailData\MYSQLDump_%inDate%.mysql"
:5th
@rem Update Spamassassin before restarting service
set FailedSAUpdate=Ok
%SPAMASSASSINdir%\
sa-update.exe -v --nogpg --channelfile UpdateChannels.txt
if errorlevel 1 set Failed=true& set FailedSAUpdate=Failed
:6th
@rem Restart Spamassassin and Hmailserver services
if "%FailedSAService%" NEQ "Ok" goto 7th
net start spamassassin
if errorlevel 1 set Failed=true& set FailedSAService=Failed to restart
:7th
if "%FailedHmailservice%" NEQ "Ok" goto 8th
net start hmailserver
if errorlevel 1 set Failed=true& set FailedHmailservice=Failed to restart
:8th
REM :: Zip temporary directory
cd %7ZIPdir%
set FailedZip=Ok
7z a -tzip "%BACKUPdir%\Serverdata_%inDate%" %BACKUPTEMPdir%\*
if errorlevel 1 set Failed=true& set FailedZip=Failed
:test
set result=%date% %time% Backup procedure FAILED!!! Zip: %FailedZip%, SA Update: %FailedSAUpdate%, SA Service: %FailedSAService%.
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*** & goto end
if errorlevel 15 echo OKCOPY + FAIL + MISMATCHES + XTRA & goto end
if errorlevel 14 echo FAIL + MISMATCHES + XTRA & goto end
if errorlevel 13 echo OKCOPY + FAIL + MISMATCHES & goto end
if errorlevel 12 echo FAIL + MISMATCHES & goto end
if errorlevel 11 echo OKCOPY + FAIL + XTRA & goto end
if errorlevel 10 echo FAIL + XTRA & goto end
if errorlevel 9 echo OKCOPY + FAIL & goto end
if errorlevel 8 echo FAIL & goto end
if errorlevel 7 echo OKCOPY + MISMATCHES + XTRA & goto end
if errorlevel 6 echo MISMATCHES + XTRA & goto end
if errorlevel 5 echo OKCOPY + MISMATCHES & goto end
if errorlevel 4 echo MISMATCHES & goto end
if errorlevel 3 echo OKCOPY + XTRA & goto end
if errorlevel 2 echo XTRA & goto end
if errorlevel 1 echo OKCOPY & goto end
if errorlevel 0 echo No Change & 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
echo %result%
echo %resulttext%
:GoEmail
Rem !! DO EMAIL OF RESULTS HERE with'result' and 'resulttext' variables !!