I wrote this backup script and post it here in order to help starters get under way without too much worry. It backs up your data and Zips it to an archive folder, whilst removing previous zip archive files older than a defined period.

It is designed for systems using the INTERNAL database or a MySQL database only. (It is NOT written for any other database (as I do not know the methods for those databases - but feel free to change accordingly to suit. However, a quick google for MSSQL equivalent of dumping the database suggests this: https://www.howtogeek.com/50295/backup- ... mand-line/ - this will be the direct replacement for the full "mysqldump" command as found in the ":4th"of BackupMail.bat below).

It is an 'easy start' script offering requiring just a review of a few tailored variable at the top of the scripts.

It works very well and does the following:

• backs up the hmaildata directory
• backs up the database keeping it inline with the hmaildata files
• updates spamassassin definitions if Spamassassin for Windows is installed and in use
• backs up other important custom data directories you wish to include
• clears down emails from the TRASH folders (or any others you wish to include) - tailorable from Zero to X days old
• performs and reports checks on the service stop/restarts to ensure continuity of data

* The data+database+customer datadrive goes into a single date-stamped ZIP file with a linked log file (eg "Serverdata_2015-03-10-200000.zip" and "Backup_2015-03-10-200000.log").

* The cleardown script gives you a nice email informing you of what happened detailing the accounts, and the quantities relating to their accounts that have been processed. (Copy and save as emailclearup.vbs - dont forget to change your parameters at implementation.)

* The whole backup routine gives you a FULL run down of what happened and emails you at the end (with variable SUBJECT depending on success/failure) with red highlighting of failures
eg

a, You will see in the section called :GoEmail that I use a 3rd party program called BLAT.exe to send the email notifications out - this standalone program needs to be downloaded and installed to use the script. You can change the reference to whatever your choice of emailer program is (you might use a CDO script or an HMS emailing script you already have written) - just replicate the appropriate parameters in the :GoEmail section on the calling command line in the BackupMail.Bat script.

b, It also uses 7zip that is shipped with Hmailserver (in the Bin directory) to perform the compression - again you can use or change to suit as you wish. See notes below.

c, It also utilises Windows Server 'ROBOCOPY' for doing the data copy (mirror). This is standard on most MS Server editions of windows but can easily be obtained for other OS's if you dont already have it.

d, The EMAILCLEARUP.VBS script also needs to be saved (to the same scripts directory) for being called upon.

IMPLEMENTATION

1, Download and save the 2 scripts (BackupMail.Bat and Emailcleardown.vbs) and save them into a SINGLE holding 'script' folder from where they will be called.

3, REVIEW AND MODIFY the various variables at the top of both scripts - BE CAREFUL TO GET THEM RIGHT. Also, take a careful read through the BAT script and fill out the extra sections (such as ":7th", ":8th" etc) to add additional copying or processing of other directories in to the script where required.

4, Call the BackupMail.bat daily (with Task Scheduler)

The basis of the backup works by initially taking a MIRROR copy of the current data folder, so the first run could take some time and you will need to be aware of this for disk space purposes ie, you will need another 100% space allocation of the current data on whatever drive you specify under the %BACKUPTEMPdir% variable. Subsequent runs, however, will be a lot quicker as only 'incremental' changes a replicated. On my system it backs up 10.5GB of data and zips it in about 20 minutes in total whist the mail service is only offline for only around 30 seconds. Its important to leave this temporary directory populated for this speed benefit.

NOTE:
There is one thing you should note: the Zip file is currently as a type ZIP compression and not the 7zip native '7z' compression. The reason for this is because it is A LOT quicker than 7Z BUT it is not so compact. To explain better, note that my total unzipped directory starts at 10.5GB but

as a ZIP:

time to zip:17 minutessize: 7.17GB

as a 7z:

time to zip: 51 minutessize:6.4GB

So you can see there is a trade off between compression and speed. You can choose and if you want the 7Z type for higher compression then make the changes to the script as necessary by modifying the line accordingly in the BackupMail.bat (search for "7za a" for find the line):

If you change the script to a 7Z archive then remember that you will need a suitable 7Z archive compatible software that is able to read and uncompress 7Z files in the event it needs to be restores. You may choose to use the full 7Zip software (http://www.7-zip.org/download.html).

At least I hope you find it useful as a starting point. You can always add to or remove more or irrelevant sections to suit but it should be sufficient as it is (if you follow the instructions and tailor the variables accordingly) to get you going with the important data.

The script is designed for installations where the database is on the same server as Hmailserver. However, if you have a MySQL database on a separate external server then you will need to make a slight adjustment to the scripts (courtesy of Mattv8, here):

Get the "mysqldump.exe" command line utility

Visit https://dev.mysql.com/downloads/mysql/ and download the latest version of MySQL community server as a zip archive. Once the zip file has downloaded, extract it and browse to the bin folder. Copy the file "mysqldump.exe" to somewhere convenient on your server. Note the directory that you copy it to.

Changes to the script BackupMail.bat

Next, you will need to make the following adjustments to the BAT script:

Specify the MYSQLBINdir to the directory you just copied "mysqldump.exe" to.

Add a new variable called MYSQLDBhost. Point this new variable to the ip address or hostname of your MySQL server. I just added this to the MySQL database section, which should now look something like the following:

rem ** IF DBtype = MySQL set the following variables.
rem ** Make sure mysqldump.exe is in the script folder, or update line 37 of this script.
set MYSQLBINdir=C:\Program Files (x86)\hMailServer\Backup
set MYSQLDBuser=root
set MYSQLDBpass=password
set MYSQLDBhost=192.168.0.123
set MYSQLDBport=3306

Some people might choose to save disk space by also clearing down their Hmailserver LOGS directory; in doing so you can perhaps save over to 90% in disk space initially taken up by the logs). The following code identifies the current location of the LOGS, zips the log files to a .7z compressed file, and then deletes the logs.

If wish this functionality then add the following code in to the Batch file somewhere similar to section "8th" or "9th" (or in to a separate batch file of your choice):

NOTE: THE FOLLOWING CORRECTIONS HAVE NOW ALREADY BEEN ADDED IN TO THE ABOVE SCRIPT. YOU DO NOT NEED TO MAKE THE CHANGES. This post is left here for tracking and narrative.

Jimimaseye

*******************************************

Thanks very much for your backup solution!
I was using till now the internal backup, but recently it was failing because of the size limit

I had to correct some problems in the script before it was running smootly on my system. So maybe my corrections will help other people.
1. first problem was the name of the backup logging file. With my regional settings for the date, it was a non valid filename as result
So I replaced the code

I will amend the code to reflect your changes parts (1) and (2) - the missing quotes (2) are a mistake and the solution for universal time/date formatting (1) will help make the script usable wherever (hopefully. I trust your research. ) I have gone one further and shanged your final line to not rely on the %str% variable (which I will removed).

I chose not to make amends for exit code 4 on spamassassin because I feel its better to be made aware that there was no SA update performed as it is likely to be less the norm (my experience is that at least 6 our of 10 times there will be an update). The idea being that if you get a week or more notifications of 'no updates available' then it should raise a concern and if it continues then MAYBE there is a problem that needs researching (whereas simply replying on it and never being told you would never find the problems). Anyway, your post will alert others to the choice they can make in the script.

EDIT:The updates script has now been amended and also the download ZIP will be changed to reflect the new code.

MANY people are using them. As you can see from the thread (the initial post the the previous post), I only updated it 9th January - hence you are the first to download the new ones. (Before I updated, there was 49 downloads of the attachment - and that doesnt account for people that may have just copy/pasted the code themselves).

It run perfectly.Not a single problem. I already added it on the daily task scheduler. Thanks.
I am also checking the clear LOG FILES bat but my hMailserver in on c:\hMailserver so i don't think it will work for me without some changes.

steveHmail wrote:It run perfectly.Not a single problem. I already added it on the daily task scheduler. Thanks.
I am also checking the clear LOG FILES bat but my hMailserver in on c:\hMailserver so i don't think it will work for me without some changes.

Yes. Just add the HMAILSERVERprogdir variable in place of the program directory and it will be ok.

So this is the modified version (add to the main backupdata.bat script):

This part of the feature relies on the MySQL programs to be installed (as the 'mysqldump' is a mysql program). You could install MySQL locally - you dont need to be hosting the database. Add -h host_name (where host_name will be your remote mysql server machine) to the 'mysqldump' command to connect to the database.

jimimaseye wrote:This part of the feature relies on the MySQL programs to be installed (as the 'mysqldump' is a mysql program). You could install MySQL locally - you dont need to be hosting the database. Add -h host_name (where host_name will be your remote mysql server machine) to the 'mysqldump' command to connect to the database.

If there was an update (and successfully installed), then it will not be seen as a 'failure'. I leave the status as a 'failiure' in there because of the possibility that the 'failure' was a DNS issue or network problem (or something else) and not simply because of it being "no fresh updates" (IOW its common but you should always be mindful to have a quick glance to ensure it is because of 'no fresh updates')

If there was an update (and successfully installed), then it will not be seen as a 'failure'. I leave the status as a 'failiure' in there because of the possibility that the 'failure' was a DNS issue or network problem (or something else) and not simply because of it being "no fresh updates" (IOW its common but you should always be mindful to have a quick glance to ensure it is because of 'no fresh updates')

Understood. Thanks again, I really appreciate all the help I am getting from these forums.

I'm currently building a new server and intend to use this method for backup on the new server, so I wanted to test it a bit before using it in production. Based on my testing, I'd like to suggest the 2 following changes to the batch script.

1) You should be able to set the SCRIPTdir variable to the value %~dp0 and thereby automatically detect the path from where the script is being executed from. That gives the user one less variable to worry about. See https://stackoverflow.com/questions/382 ... in-windows for reference.

2) If your SCRIPTdir and HMAILSERVERprogdir is NOT on the same drive, the zip part will fail as the default "cd" command will not change the drive but only the folder. In the current script, the 7za command is called without the path which means the script won't be able to execute the command so that part of the script fails. You can solve it in 2 ways: Either add /D to the "cd" command so it also changes the drive or add "%HMAILSERVERprogdir%"\Bin when calling 7za.

I've made the above 2 changes to my script (added /D to the "cd" command), and it seems to be working fine.

Question: Is there a restore part (guide) for this script as well? Not that I hope I'll ever need it, but it would be nice to know exactly what the expected/best practice procedure would be if you need to restore either partially (i.e. just one domain or account) or fully (recover from catastrophic system failure).

mibyge wrote:Hi
Question: Is there a restore part (guide) for this script as well? Not that I hope I'll ever need it, but it would be nice to know exactly what the expected/best practice procedure would be if you need to restore either partially (i.e. just one domain or account) or fully (recover from catastrophic system failure).

I have attached a guide to the original post for you.

Regarding your other comments and suggestions: thanks. I will review them and apply as necessary when I have a little more time.

Alternatively, follow these instructions to correct the error in your BAT file:

1, Using your editor, search for "if errorlevel 1" where the end of the line ends with "& echo %FailedHmailservice%! >> %BackLog%"
2, Separate the line at "& echo" so it becomes on a new line (and remove the & ampersand)
eg
this:

For those of us with external mysql databases, and don't have MySQL server installed on the hmail host machine, this might be helpful. I had to make a few minor changes to the script to get it to work.

Get the "mysqldump.exe" command line utility
Visit https://dev.mysql.com/downloads/mysql/ and download the latest version of MySQL community server as a zip archive. Once the zip file has downloaded, extract it and browse to the bin folder. Copy the file "mysqldump.exe" to somewhere convenient on your server. Note the directory that you copy it to.

Changes to the script BackupMail.bat
Next, you will need to make the following adjustments to jimimaseye's script:

Specify the MYSQLBINdir to the directory you just copied "mysqldump.exe" to.

Add a new variable called MYSQLDBhost. Point this new variable to the ip address or hostname of your MySQL server. I just added this to the MySQL database section, which should now look something like the following:

rem ** IF DBtype = MySQL set the following variables.
rem ** Make sure mysqldump.exe is in the script folder, or update line 37 of this script.
set MYSQLBINdir=C:\Program Files (x86)\hMailServer\Backup
set MYSQLDBuser=root
set MYSQLDBpass=password
set MYSQLDBhost=192.168.0.123
set MYSQLDBport=3306

Windows showed strange behaviour on starting the hmailserver service reporting errorlevel 2 although eventually it does start (I think errorlevel 2 might be 'delayed'). Consequently the script would then think it is 'out of order' and not send the final email out results. This change uses alternative method to check the hmailserver service is running for emailing the results instead of relying on the previous start service errorlevel.

Thanks, I have implemented and will monitor. I am having another issue with a few lines that I added myself to copy the backups to an external drive. Basically, it throws up an error every day and says failed but the copy has been completed correctly.

Everything was fine for a few days, and now I came in again this morning, no backup and no-one was getting emails. As soon as I logged on to the server, then emails were all delivered, and everything is back to normal.

Something is happening with the backup and hmailserver is not restarting. We are only getting the emails from the few hours that the server was down as I am using our webhost as a mail2.domain.com as a backup and hmailserver periodically checks these mailboxes.

Everything was fine for a few days, and now I came in again this morning, no backup and no-one was getting emails. As soon as I logged on to the server, then emails were all delivered, and everything is back to normal.

Ah, young Padawan.

This doesnt make in sense in context to what you originally say the problem is. When you say "logged on to the server" - do you mean as a Windows users or actually in to HmailAdmin? and once logged on, what do you then do?

Everything was fine for a few days, and now I came in again this morning, no backup and no-one was getting emails. As soon as I logged on to the server, then emails were all delivered, and everything is back to normal.

Ah, young Padawan.

This doesnt make in sense in context to what you originally say the problem is. When you say "logged on to the server" - do you mean as a Windows users or actually in to HmailAdmin? and once logged on, what do you then do?

Sorry Master

I log on as a Windows user, and prior to starting hMailAdmin, the mail server appears to start running again.

Thats very peculiar. As far as I know, the running of a service (in this case the Hmailserver service), has no reliance on whether anyone logins in to windows or not. A NET START [service] should happen irrespective.

However, I know that TASK SCHEDULER jobs may depend on whether someone is logged in or not. And given that you are (probably) running this backup script via a Task Scheduler job, then perhaps the cause is somewhere around this. You should check that you have it set to run even if not logged on (and saved using windows Administrator level credentials):

I log on as a Windows user, and prior to starting hMailAdmin, the mail server appears to start running again.

"Appears to".

Look at EVENT VIEWER and maybe also the service itself (after you have logged on) to see the startup time of the service - does it tie in with the exact moment that you login in? Maybe if your task job is waiting for you to log in then it is actually waiting to START the job (with a NET STOP) and then progrsses to NET START a few seconds later?

The problem lies in determining if/why the service fails to stop in a controlled manner (ie, not errorlevel 0). And I suspect the other error might have something to do with it.

Thanks jimimaseye, that explains a lot. I wrongly assumed the OK! in the log, was actually it saying it had shot down the server correctly, but it appears not as it was missing the line "The hMailServer service was stopped successfully."

I will investigate further. In the 6 months I have been running the script, it has failed 4 times, and the first was the end of March, so this is a new error. I will investigate further.

Thanks jimimaseye, that explains a lot. I wrongly assumed the OK! in the log, was actually it saying it had shot down the server correctly, but it appears not as it was missing the line "The hMailServer service was stopped successfully."
.
.
.

I occasionally see windows misbehave and get itself in a tizzy with the stopping/starting of the services (similar to your experience) and its usually just after a windows update and during the 'reboot required' phase.

Thanks jimimaseye, that explains a lot. I wrongly assumed the OK! in the log, was actually it saying it had shot down the server correctly, but it appears not as it was missing the line "The hMailServer service was stopped successfully."
.
.
.

I occasionally see windows misbehave and get itself in a tizzy with the stopping/starting of the services (similar to your experience)

No error feedback message or NET HELPMSG xxxx error.
And a search for errorlevel 2 shows nothing useful except that all values are likely to be value 2: https://ss64.com/nt/net-service.html

And yet by the time the point of emailing arrived in the script it had restarted (and successfully emailed the log). Note that this was only possible due to my recent updates to the script which ignores the above error and tests for the service being started anyway (at time of emailing).

This script works great. Just one question. I received the emails after it finished. The "backup daemon" one has the SA update error (for no update). However, at the bottom of the message it says, "See attached log file below for details." but the are no attachments. Is this an oversight in the code or am I actually missing the attachment?

It looks like the archive files are way too small. The 7z files are from the HMS-included backup script. The zips are from this script.

Any idea what that's about? My scheduled task is set to run from SYSTEM and the manual runs were from a cmd window ran as administrator. So I don't think it's a permissions thing.

For whatever it's worth, the backup-temp folder is about 40 MB. My data dir is about 1.5 GB.

BackupMail.bat was copied from the post (not downloaded). The first run was "successful", although now I see it was not, then I added the bit about the logs (the separate part near the bottom of the first post) but I screwed something up, so I copied the script again and the only changes are the user variables. The log appears to show all messages copied.