Send notification on successful backup

This section contains scripts that hMailServer has contributed with. hMailServer 4 is needed to use these.
Post Reply
User avatar
martin
Developer
Developer
Posts: 6837
Joined: 2003-11-21 01:09
Location: Sweden
Contact:

Send notification on successful backup

Post by martin » 2005-11-05 17:18

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: Select all

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.

zaggi
New user
New user
Posts: 10
Joined: 2006-01-28 22:56

Thx

Post by zaggi » 2006-05-19 12:01

Thx - I like it alot :-)

User avatar
Slug
Moderator
Moderator
Posts: 1369
Joined: 2005-03-13 05:42
Location: Sydney Australia
Contact:

Post by Slug » 2006-05-19 14:00

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
Missing Hmailserver ... Now running Debian servers

^DooM^
Site Admin
Posts: 13861
Joined: 2005-07-29 16:18
Location: UK

Post by ^DooM^ » 2006-05-19 16:15

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! ヅ

zaggi
New user
New user
Posts: 10
Joined: 2006-01-28 22:56

Post by zaggi » 2006-05-23 00:09

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 :-)

nikolajhendel
New user
New user
Posts: 24
Joined: 2006-04-03 22:16

Post by nikolajhendel » 2006-05-23 11:29

just a thought (to keep the inbox from filling up):
Would it be possible to have the send notification only on an unsucessfull backup ?

User avatar
martin
Developer
Developer
Posts: 6837
Joined: 2003-11-21 01:09
Location: Sweden
Contact:

Post by martin » 2006-05-23 20:22

Yes, by using the OnBackupFailed event.

nikolajhendel
New user
New user
Posts: 24
Joined: 2006-04-03 22:16

Post by nikolajhendel » 2006-05-24 09:39

perfect - thx. for the info martin

bartley
New user
New user
Posts: 18
Joined: 2007-01-02 16:14

Post by bartley » 2007-02-02 06:30

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

bartley
New user
New user
Posts: 18
Joined: 2007-01-02 16:14

Post by bartley » 2007-02-02 07:28

Nevermind, it's working now, not sure what changed...... ugh.... thanks for looking though.

bartley
New user
New user
Posts: 18
Joined: 2007-01-02 16:14

Post by bartley » 2007-02-02 15:19

But then again, no it's not... sorry for the inconsistent post, something else sent me the message.

bartley
New user
New user
Posts: 18
Joined: 2007-01-02 16:14

Post by bartley » 2007-02-04 22:56

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")

User avatar
danny6167
Senior user
Senior user
Posts: 472
Joined: 2007-02-07 15:24
Location: Western Australia
Contact:

Post by danny6167 » 2007-03-07 13:18

is there a way to include the reason of failure when sending a message on backup fail?
Probably something simple.

westdam
Senior user
Senior user
Posts: 728
Joined: 2006-08-01 21:24
Location: Padova, Italy
Contact:

Re: Send notification on successful backup

Post by westdam » 2008-12-04 00:58

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.

User avatar
martin
Developer
Developer
Posts: 6837
Joined: 2003-11-21 01:09
Location: Sweden
Contact:

Re: Send notification on successful backup

Post by martin » 2008-12-04 01:05

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)

westdam
Senior user
Senior user
Posts: 728
Joined: 2006-08-01 21:24
Location: Padova, Italy
Contact:

Re: Send notification on successful backup

Post by westdam » 2008-12-04 01:06

oh ok!
tomorrow i'll test!! thanks martin! :D

Schnurps
New user
New user
Posts: 9
Joined: 2008-12-04 22:51

Re: Send notification on successful backup

Post by Schnurps » 2008-12-04 22:56

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?

Schnurps
New user
New user
Posts: 9
Joined: 2008-12-04 22:51

Re: Send notification on successful backup

Post by Schnurps » 2008-12-06 21:58

Can anybody help me?

westdam
Senior user
Senior user
Posts: 728
Joined: 2006-08-01 21:24
Location: Padova, Italy
Contact:

Re: Send notification on successful backup

Post by westdam » 2008-12-06 23:44

mmm
do you want to send the backup via email???damn.it's weird...

Schnurps
New user
New user
Posts: 9
Joined: 2008-12-04 22:51

Re: Send notification on successful backup

Post by Schnurps » 2008-12-07 00:04

right i want to attach the backup as well! :shock:

westdam
Senior user
Senior user
Posts: 728
Joined: 2006-08-01 21:24
Location: Padova, Italy
Contact:

Re: Send notification on successful backup

Post by westdam » 2008-12-07 00:21

it'ss strange.
anyway i think you have to do some scripting.sorry i cant help you..i'm not able to do .

Schnurps
New user
New user
Posts: 9
Joined: 2008-12-04 22:51

Re: Send notification on successful backup

Post by Schnurps » 2008-12-07 11:15

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.

User avatar
mattg
Moderator
Moderator
Posts: 21268
Joined: 2007-06-14 05:12
Location: 'The Outback' Australia

Re: Send notification on successful backup

Post by mattg » 2008-12-07 13:10

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.
https://www.hmailserver.com/documentation

Schnurps
New user
New user
Posts: 9
Joined: 2008-12-04 22:51

Re: Send notification on successful backup

Post by Schnurps » 2008-12-07 23:27

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?

User avatar
mattg
Moderator
Moderator
Posts: 21268
Joined: 2007-06-14 05:12
Location: 'The Outback' Australia

Re: Send notification on successful backup

Post by mattg » 2008-12-08 01:43

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.
https://www.hmailserver.com/documentation

Schnurps
New user
New user
Posts: 9
Joined: 2008-12-04 22:51

Re: Send notification on successful backup

Post by Schnurps » 2008-12-10 21:50

I tried to do that but it didn´t work can someone help me to build this filename?

Schnurps
New user
New user
Posts: 9
Joined: 2008-12-04 22:51

Re: Send notification on successful backup

Post by Schnurps » 2008-12-12 20:16

nobody?

User avatar
martin
Developer
Developer
Posts: 6837
Joined: 2003-11-21 01:09
Location: Sweden
Contact:

Re: Send notification on successful backup

Post by martin » 2008-12-12 21:06

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.

Schnurps
New user
New user
Posts: 9
Joined: 2008-12-04 22:51

Re: Send notification on successful backup

Post by Schnurps » 2008-12-12 22:33

ok I solved the Problem !! Thanks for yout hint!

User avatar
Slug
Moderator
Moderator
Posts: 1369
Joined: 2005-03-13 05:42
Location: Sydney Australia
Contact:

Re: Send notification on successful backup

Post by Slug » 2009-02-28 17:35

Can we get an update for this script to work with V5

Thanks
Michael
Missing Hmailserver ... Now running Debian servers

User avatar
mattg
Moderator
Moderator
Posts: 21268
Joined: 2007-06-14 05:12
Location: 'The Outback' Australia

Re: Send notification on successful backup

Post by mattg » 2009-03-02 02:02

Done! I just posted mine, which was a derivative of this one.

http://www.hmailserver.com/forum/viewto ... 20&t=14280
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

Post Reply