Post new topic Reply to topic  [ 31 posts ] 
Author Message
 Post subject: Send notification on successful backup
PostPosted: 2005-11-05 17:18 
Developer

Joined: 2003-11-21 01:09
Posts: 6394
Location: Sweden
Description
This scripts sends a notification to you when a backup has completed. The backup log is appended to the message and deleted from disk.

Requirements
hMailServer 4.2 & 4.3

Usage
1) Copy the below code to your EventHandlers.vbs
2) Edit the settings in the top to suite your needs.
2) In hMailAdmin, go to the Scripts section an reload the script.

Code

Code:
const g_sBNFrom = "hMailServer Backup"
const g_sBNFromAddress = "backupscript@yourdomain.com"
const g_sBNSubject = "Backup completed"
const g_sBNRecipientName = "Your name"
const g_sBNRecipientAddress = "youraddress@yourdomaincom"
const g_sAdminPassword = "<your-hmailserver-password>"

Sub OnBackupCompleted()
   Set oApp = CreateObject("hMailServer.Application")

   ' Give this script permission to access all
   ' hMailServer settings.
   Call oApp.Authenticate("Administrator", g_sAdminPassword)

   sBackupLog = ReadFileAndDelete(oApp.Settings.Backup.LogFile)

   Set oMessage = CreateObject("hMailServer.Message")
   oMessage.From = g_sBNFrom & " <" & g_sBNFromAddress &  ">"
   oMessage.FromAddress = g_sBNFromAddress
   oMessage.Subject = g_sBNSubject
   oMessage.AddRecipient g_sBNRecipientName, g_sBNRecipientAddress
   oMessage.Body = "The backup completed succesfully." & vbNewLine & vbNewLine & sBackupLog
   oMessage.Save
End Sub

Function ReadFileAndDelete(sFile)
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.OpenTextFile(sFile, 1)
   ReadFileAndDelete = f.ReadAll
   f.Close
   fso.DeleteFile(sFile)
   Set f = Nothing
   Set fso = Nothing
End Function


Last edited by martin on 2007-03-13 12:56, edited 2 times in total.

Top
 Profile  
 
 Post subject: Thx
PostPosted: 2006-05-19 12:01 
New user
New user

Joined: 2006-01-28 22:56
Posts: 10
Thx - I like it alot :-)


Top
 Profile  
 
 Post subject:
PostPosted: 2006-05-19 14:00 
Moderator
User avatar

Joined: 2005-03-13 05:42
Posts: 1369
Location: Sydney Australia
Mine is working, but I just want to make sure I did not do something wrong. Was I supposed to just paste it on the end or did it need to somewhere specific in the EventHandlers.vbs ??

Thanks
Michael

_________________
hMailServer 5.4 B1944 external MySQL 5.5
Win 2003 SP2 | IIS 6 | ClamAV 0.97.3 | PHP 5.3.17 | Roundcube Webmail 0.8.2


Top
 Profile  
 
 Post subject:
PostPosted: 2006-05-19 16:15 
Site Admin

Joined: 2005-07-29 16:18
Posts: 13628
Location: UK
Thanks Martin.

I have just added this to my install.
Is there any way to make this happen automatically at the end of the day for instance?

Cheers!

_________________
If at first you don't succeed, bomb disposal probably isn't for you! ヅ


Top
 Profile  
 
 Post subject:
PostPosted: 2006-05-23 00:09 
New user
New user

Joined: 2006-01-28 22:56
Posts: 10
Was looking for a script like that to - An automatic backup script. Maybe a script that makes a backup, and send the backup to a mail. Or just put it in a dir :-)


Top
 Profile  
 
 Post subject:
PostPosted: 2006-05-23 11:29 
New user
New user

Joined: 2006-04-03 22:16
Posts: 24
just a thought (to keep the inbox from filling up):
Would it be possible to have the send notification only on an unsucessfull backup ?


Top
 Profile  
 
 Post subject:
PostPosted: 2006-05-23 20:22 
Developer

Joined: 2003-11-21 01:09
Posts: 6394
Location: Sweden
Yes, by using the OnBackupFailed event.


Top
 Profile  
 
 Post subject:
PostPosted: 2006-05-24 09:39 
New user
New user

Joined: 2006-04-03 22:16
Posts: 24
perfect - thx. for the info martin


Top
 Profile  
 
 Post subject:
PostPosted: 2007-02-02 06:30 
New user
New user

Joined: 2007-01-02 16:14
Posts: 18
Sorry for this post... however I have backed up my system several times with the script and with the following code in the eventhandler, however, I'm not receiving the email. I've reloaded the scripts several times.... not sure what I'm doing wrong. Thanks a bunch. Hopefully it's easy. :) :) :)





' Sub OnClientConnect(oClient)
' End Sub

' Sub OnAcceptMessage(oClient, oMessage)
' End Sub

' Sub OnDeliverMessage(oMessage)
' End Sub

' Sub OnBackupFailed(sReason)
' End Sub

' Sub OnBackupCompleted()
' End Sub

const g_sBNFrom = "hMailServer Backup"
const g_sBNFromAddress = "administrator@eagle-scout.net"
const g_sBNSubject = "Backup completed"
const g_sBNRecipientName = "Mailer Daemon"
const g_sBNRecipientAddress = "bartley@eagle-scout.net"

Sub OnBackupCompleted()
Set oApp = CreateObject("hMailServer.Application")

sBackupLog = ReadFileAndDelete(oApp.Settings.Backup.LogFile)

Set oMessage = CreateObject("hMailServer.Message")
oMessage.From = g_sBNFrom & " <" & g_sBNFromAddress & ">"
oMessage.FromAddress = g_sBNFromAddress
oMessage.Subject = g_sBNSubject
oMessage.AddRecipient g_sBNRecipientName, g_sBNRecipientAddress
oMessage.Body = "The backup completed succesfully." & vbNewLine & vbNewLine & sBackupLog
oMessage.Save
End Sub

Sub OnBackupFailed()
Set oApp = CreateObject("hMailServer.Application")

sBackupLog = ReadFileAndDelete(oApp.Settings.Backup.LogFile)

Set oMessage = CreateObject("hMailServer.Message")
oMessage.From = g_sBNFrom & " <" & g_sBNFromAddress & ">"
oMessage.FromAddress = g_sBNFromAddress
oMessage.Subject = g_sBNSubject
oMessage.AddRecipient g_sBNRecipientName, g_sBNRecipientAddress
oMessage.Body = "The backup Failed." & vbNewLine & vbNewLine & sBackupLog
oMessage.Save
End Sub


Function ReadFileAndDelete(sFile)
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(sFile, 1)
ReadFileAndDelete = f.ReadAll
f.Close
fso.DeleteFile(sFile)
Set f = Nothing
Set fso = Nothing
End Function


Top
 Profile  
 
 Post subject:
PostPosted: 2007-02-02 07:28 
New user
New user

Joined: 2007-01-02 16:14
Posts: 18
Nevermind, it's working now, not sure what changed...... ugh.... thanks for looking though.


Top
 Profile  
 
 Post subject:
PostPosted: 2007-02-02 15:19 
New user
New user

Joined: 2007-01-02 16:14
Posts: 18
But then again, no it's not... sorry for the inconsistent post, something else sent me the message.


Top
 Profile  
 
 Post subject:
PostPosted: 2007-02-04 22:56 
New user
New user

Joined: 2007-01-02 16:14
Posts: 18
I found the problem, I'm thanking martin for a post on another matter, which when applied to this script fixes my problem where the email did not send upon completion of the backup.

If you're running 4.3 or later, you need to:

Quote:
You need to add
Call oApp.Authenticate("Administrator", "your-main-hmailserver-password")
after the line
Set oApp = CreateObject("hMailServer.Application")


Top
 Profile  
 
 Post subject:
PostPosted: 2007-03-07 13:18 
Senior user
Senior user
User avatar

Joined: 2007-02-07 15:24
Posts: 472
Location: Western Australia
is there a way to include the reason of failure when sending a message on backup fail?
Probably something simple.


Top
 Profile  
 
 Post subject: Re: Send notification on successful backup
PostPosted: 2008-12-04 00:58 
Senior user
Senior user

Joined: 2006-08-01 21:24
Posts: 728
Location: Padova, Italy
for a strange reason with v5 this script have some issue.
i receive the email with the wrote " backup succesfully" but instead having the backup log i've got this :

ÿþ2

the backup log file is readable ( or so seems ) and i havent touched anything .


with v4 all goes with v5 no.


Top
 Profile  
 
 Post subject: Re: Send notification on successful backup
PostPosted: 2008-12-04 01:05 
Developer

Joined: 2003-11-21 01:09
Posts: 6394
Location: Sweden
The script is for v4. Not for v5.

You may be able to get it right by replacing
Set f = fso.OpenTextFile(sFile, 1)
with
Set f = fso.OpenTextFile(sFile, 1, False, -1)


Top
 Profile  
 
 Post subject: Re: Send notification on successful backup
PostPosted: 2008-12-04 01:06 
Senior user
Senior user

Joined: 2006-08-01 21:24
Posts: 728
Location: Padova, Italy
oh ok!
tomorrow i'll test!! thanks martin! :D


Top
 Profile  
 
 Post subject: Re: Send notification on successful backup
PostPosted: 2008-12-04 22:56 
New user
New user

Joined: 2008-12-04 22:51
Posts: 9
Very nice script! Thanks!

But is there a way to attach the created Backup to the Email we send? That would be great!

I know that we have to use oMessage.attachment but I don´t know how to define the backup filename. Can you help me with that?


Top
 Profile  
 
 Post subject: Re: Send notification on successful backup
PostPosted: 2008-12-06 21:58 
New user
New user

Joined: 2008-12-04 22:51
Posts: 9
Can anybody help me?


Top
 Profile  
 
 Post subject: Re: Send notification on successful backup
PostPosted: 2008-12-06 23:44 
Senior user
Senior user

Joined: 2006-08-01 21:24
Posts: 728
Location: Padova, Italy
mmm
do you want to send the backup via email???damn.it's weird...


Top
 Profile  
 
 Post subject: Re: Send notification on successful backup
PostPosted: 2008-12-07 00:04 
New user
New user

Joined: 2008-12-04 22:51
Posts: 9
right i want to attach the backup as well! :shock:


Top
 Profile  
 
 Post subject: Re: Send notification on successful backup
PostPosted: 2008-12-07 00:21 
Senior user
Senior user

Joined: 2006-08-01 21:24
Posts: 728
Location: Padova, Italy
it'ss strange.
anyway i think you have to do some scripting.sorry i cant help you..i'm not able to do .


Top
 Profile  
 
 Post subject: Re: Send notification on successful backup
PostPosted: 2008-12-07 11:15 
New user
New user

Joined: 2008-12-04 22:51
Posts: 9
what´s so strange about it? I only want to get the Backup file if I start the backup from the hmail webinterface. That way I do not have to connect to the server to download it.


Top
 Profile  
 
 Post subject: Re: Send notification on successful backup
PostPosted: 2008-12-07 13:10 
Moderator
User avatar

Joined: 2007-06-14 05:12
Posts: 9385
Location: 'The Outback' Australia
Schnurps wrote:
what´s so strange about it? I only want to get the Backup file if I start the backup from the hmail webinterface. That way I do not have to connect to the server to download it.

If you don't download it before the next server backup, then your next backup will double the size because the backup will also include a copy of the backup e-mail.

New day (day 3), the size of the backup will be quadrupled, ie Original Size, plus backup same size, plus backup of both previous backups.

Day 4 - 8 times original etc

You only have to be unable to download the backup for a week for the backup to be 128 times original size. If the databse size exceeds 1.5 Gb then the built-in backup will fail. So unless your original database is less than 150 MB then your backup will fail if your backups aren't collected for a week.

Much better option is to modify the StartBackup.vbs to create multiple copies, or perhaps include a scripted upload to an off site server.

But if you really NEED to include the backup, then you could add it as an attachment where the e-mail is created by using http://www.hmailserver.com/documentatio ... ttachments ADD.

_________________
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
Documentation


Top
 Profile  
 
 Post subject: Re: Send notification on successful backup
PostPosted: 2008-12-07 23:27 
New user
New user

Joined: 2008-12-04 22:51
Posts: 9
ok i haven´t thought about that.... But I am sending the mail to an external email adress. I also wanted to delete the backup after sending and I am not creating a backup every day!

It´s no Problem to attach a file itself but the filename is different each time I create a backup. What can I do about that?


Top
 Profile  
 
 Post subject: Re: Send notification on successful backup
PostPosted: 2008-12-08 01:43 
Moderator
User avatar

Joined: 2007-06-14 05:12
Posts: 9385
Location: 'The Outback' Australia
Schnurps wrote:
It´s no Problem to attach a file itself but the filename is different each time I create a backup. What can I do about that?

Dynamically create the file name for attaching when the backup is done. The backup - filename just includes date and time of backup

_________________
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
Documentation


Top
 Profile  
 
 Post subject: Re: Send notification on successful backup
PostPosted: 2008-12-10 21:50 
New user
New user

Joined: 2008-12-04 22:51
Posts: 9
I tried to do that but it didn´t work can someone help me to build this filename?


Top
 Profile  
 
 Post subject: Re: Send notification on successful backup
PostPosted: 2008-12-12 20:16 
New user
New user

Joined: 2008-12-04 22:51
Posts: 9
nobody?


Top
 Profile  
 
 Post subject: Re: Send notification on successful backup
PostPosted: 2008-12-12 21:06 
Developer

Joined: 2003-11-21 01:09
Posts: 6394
Location: Sweden
Loop over the files in the backup folder and pick the newest one. Make sure that the directory you backup to only contains backups files.


Top
 Profile  
 
 Post subject: Re: Send notification on successful backup
PostPosted: 2008-12-12 22:33 
New user
New user

Joined: 2008-12-04 22:51
Posts: 9
ok I solved the Problem !! Thanks for yout hint!


Top
 Profile  
 
 Post subject: Re: Send notification on successful backup
PostPosted: 2009-02-28 17:35 
Moderator
User avatar

Joined: 2005-03-13 05:42
Posts: 1369
Location: Sydney Australia
Can we get an update for this script to work with V5

Thanks
Michael

_________________
hMailServer 5.4 B1944 external MySQL 5.5
Win 2003 SP2 | IIS 6 | ClamAV 0.97.3 | PHP 5.3.17 | Roundcube Webmail 0.8.2


Top
 Profile  
 
 Post subject: Re: Send notification on successful backup
PostPosted: 2009-03-02 02:02 
Moderator
User avatar

Joined: 2007-06-14 05:12
Posts: 9385
Location: 'The Outback' Australia
Done! I just posted mine, which was a derivative of this one.

viewtopic.php?f=20&t=14280

_________________
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
Documentation


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 31 posts ] 


Who is online

Users browsing this forum: No registered users and 3 guests



Search for:
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group