Page 1 of 1

issue with starting and stopping services in script

Posted: 2011-06-09 02:44
by associates
Hi,

I have written a simple VB script that would stop the services of hMailServer and MySQL, then do the back up on hMailServer Data as well as the Data in MySQL Database, and then finally restarted the services.

The script goes as follows

Code: Select all

Set objShell = CreateObject("WScript.Shell")
objshell.run("net stop ""hMailServer""")
objshell.run("net stop ""mysql""")

sourceDataStr = "C:\Program Files\hMailServer\Data"
destDataStr = "E:\hMailServer\Data"

sourceSQLStr = "C:\AppServ\MySQL\data\hmailserver_db"
destSQLStr = "E:\hMailServer\mysql daily backup"

' Back up Email Data
strDataCmd = "robocopy " & Chr(34) & sourceDataStr & Chr(34) & " " & Chr(34) & destDataStr & Chr(34) & " /M"
objShell.Run strDataCmd

' Back up mysql database
strSQLCmd = "robocopy " & Chr(34) & sourceSQLStr & Chr(34) & " " & Chr(34) & destSQLStr & Chr(34) & " /MIR"
objShell.Run strSQLCmd

objshell.run("net start ""hMailServer""")
objshell.run("net start ""mysql""")

WScript.Quit
When I run the script in the dos command prompt, it stops the hMailServer as well as MySQL services. Then after having done the task, the hMailServer will not get re-started because there is a message in the dos windows that says hMailServer needs to be started first. It is awaiting a user intervention to say a Yes or No to start the hMailServer.

Since I schedule the task to run overnight, the hMailServer will not get started until I come in the next day. I wonder if anyone might be able to share some of your expertise with me.

Any help would be greatly appreciated.

Thank you

Re: issue with starting and stopping services in script

Posted: 2011-06-09 03:38
by mattg
I'd set the hmailserver service to depend on the MySQL service in services. That way hmailserver will automatically start MySQL service first.
associates wrote:It is awaiting a user intervention to say a Yes or No to start the hMailServer.
I don't believe that this is the case.

The other method is to stop your hMailserver using the COM API rather than stopping the service itself.

here is my batch file which has been going for a couple of years now without issue.
(You may need to check the port number of the MySQL connection)

Code: Select all

erase /Q C:\Backuptemp\*.mysql
net stop "hmailserver"
robocopy "c:\Program Files\hMailserver\Data" c:\BackupTemp /mir
copy "c:\Program Files\hMailserver\bin\hmailserver.ini" c:\BackupTemp /Y
cd C:\Program Files\MySQL\MySQL Server 5.0\bin
mysqldump -u"root" -p"[REDACTED]" -q -A -l --add-drop-table -P3307 >"C:\backupTemp\MYSQLDump %date:~10,4%-%date:~7,2%-%date:~4,2% %time:~0,2%%time:~3,2%.mysql"
net start "hmailserver"

cd C:\Program Files\7-Zip
7z a -tzip "C:\Backups\hMailserver %date:~10,4%-%date:~7,2%-%date:~4,2%" C:\BackupTemp\*
Oh, and I use 7Zip to zip the files.

Re: issue with starting and stopping services in script

Posted: 2011-06-09 07:52
by associates
Thanks Mattg for your reply and for sharing your code.

In case of restoring emails, given that we now have the mysql data from the mysqldump and the hmailserver data, do I need to run DataDirectorySynchronizer to sync data again? or just restore mysql table and putting the data back to hmailserver would be sufficient enough for hmailserver to get back online.

Thank you

Re: issue with starting and stopping services in script

Posted: 2011-06-09 08:14
by mattg
What I normally do is load the backup onto a spare computer, connect to it with a IMAP account, from a mail client that is also connected to the main server, and either copy across directly, or simply forward the message to the user again.

You could just copy the mail message into the appropriate folder and run the Database Directory Synchroniser, but that is likely to a long time. Might be suitable for an entire account that has been lost....

All items picked up by the data directory synchroniser will be added to the 'INBOX'

Re: issue with starting and stopping services in script

Posted: 2011-06-10 00:52
by associates
Thanks again Mattg for your info.

Yes, I got a feeling that it would put all the mails into inbox after running the database directory synchroniser (which is not what I want because some users have folders other than just Inbox. Anyway, thanks for the clarification.

I actually wanted to do just like what you have got there: a spare computer. I have already set up a spare computer that has hMailServer and squirrelmail running. It's got Apache 2 running as well. Pretty much like the mail server. So if I can get this backup to work on the spare machine, it'd be wonderful.

Anyway, I should now be going to learn to put the mysql data for emails (after running mysqldump) back to the MySQL database on the spare machine and see if I can see all the emails as in the actual mail server machine.

Thank you for being very helpful