Page 1 of 1 1
Topic Options
#185486 - 2008-02-19 08:37 PM Possible to add error conditions and email when in error?
scott_s Offline
Fresh Scripter

Registered: 2004-06-15
Posts: 15
Ok I'm the DBA and have no real experience in Kix, but need to re-write a script code. The existing script doesn't have any type of error handling system. Need to adjust so if the script fails the log file is emailed to given email addresses

I greatly appreciate this...

Here is the code:

/* KiXtart script to copy Database backup files from a production to warm server */


/* Remove old Log File */

$LogFile = "e:\SQL Batch CMDs\Output Log\Copy_full.log"
$subject = '"SmartStream Copy Full @DAY @DATE @TIME"'
IF EXIST ($logfile)
? "Deleting Old Log File"
DEL $logfile
IF @ERROR<>0
$LogItem = Log("Error Deleting $logfile Error number @ERROR","$logfile")
ENDIF
SLEEP 2
ENDIF

/* Create Log File */

? "Today is @DAY"
$LogItem = Log("@DAY @DATE @TIME Begin Process","$logfile")
$LogItem = Log("@DAY @DATE @TIME Start Compression","$logfile")

/* Compress .BAK files into .zip package */


? "@TIME Compressing Files"
SHELL ('c:\progra~1\winzip\wzzip -a -ex -P -r "e:\SQL Backup\ss-full-@DAY-@YDAYNO.zip" "E:\SQL Backup\Data\*.*"')
$LogItem = Log("@DAY @DATE @TIME End Compression","$logfile")
SLEEP 3
$LogItem = Log("@DAY @DATE @TIME Start Copying Files","$logfile")
? "@TIME Copying Files"
SLEEP 8
COPY "e:\SQL Backup\ss-full-@DAY-@YDAYNO.zip" "\\HQGEAC2\e$\SQL Backup"
$LogItem = Log("@DAY @DATE @TIME End Copying Files Error number @ERROR","$logfile")


/* Delete .zip package from production server once .zip package successfully moves to warm server */

DEL ("e:\SQL Backup\ss-full-@day-@ydayno.zip")

$LogItem = Log("@DAY @DATE @TIME Process Complete","$logfile")

SLEEP 20
EXIT

Function Log($Message,Optional $LogFile)
Dim $LogFile
; Check parameters.
$Log=''
$LogFile=''+$LogFile
If 0=VarType($Message) Exit(1) EndIf

If $LogFile
; Check if logfile directory exists..
If RedirectOutput($LogFile,0)=0
? "$Message"
$=RedirectOutput("")
Else Exit(3) EndIf
EndIf
EndFunction

Top
#185487 - 2008-02-19 11:52 PM Re: Possible to add error conditions and email when in error? [Re: scott_s]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Sure it is possible to mail someone if the script has one or more error.

First things first. I made some small modifications to you script. Just to make it comply to best practice scripting. I did not test after the modifications it so be careful implementing it in a production environment.

 Code:
;KiXtart script to Copy Database backup files from a production to warm server

;Remove old Log File
$LogFile = "e:\SQL Batch CMDs\Output Log\Copy_full.log"
$subject = "SmartStream Copy Full " + @DAY + @DATE + @TIME
If Exist ($logfile)
	? "Deleting Old Log File"
	Del $logfile
	If @ERROR<>0
		$LogItem = Log("Error Deleting " + $logfile + " Error number " + @ERROR,$logfile)
	EndIf
	Sleep 2
EndIf

;Create Log File

? "Today is " + @DAY
$LogItem = Log(@DAY + @DATE + @TIME + " Begin Process",$logfile)
$LogItem = Log(@DAY + @DATE + @TIME + " Start Compression",$logfile)

;Compress .BAK files into .zip package

? @TIME + " Compressing Files"
Shell ('"c:\program files\winzip\wzzip -a -ex -P -r e:\SQL Backup\ss-full-"' + @DAY + "-" + @YDAYNO + '".zip E:\SQL Backup\Data\*.*"')
$LogItem = Log(@DAY + @DATE + @TIME + " End Compression",$logfile)
Sleep 3
$LogItem = Log(@DAY + @DATE + @TIME + " Start Copying Files",$logfile)
? @TIME + " Copying Files" 
Sleep 8
Copy "e:\SQL Backup\ss-full-" + @DAY + "-" + @YDAYNO + ".zip" "\\HQGEAC2\e$\SQL Backup" 
$LogItem = Log(@DAY + @DATE + @TIME + " End Copying Files Error number " + @ERROR,$logfile)


;Delete .zip package from production server once .zip package successfully moves to warm server

Del ("e:\SQL Backup\ss-full-" + @day + "-" + @ydayno + ".zip")

$LogItem = Log(@DAY + @DATE + @TIME + " Process Complete",$logfile)

Sleep 20
Exit

Function Log($Message,Optional $LogFile)
	Dim $LogFile
	; Check parameters.
	$Log=''
	$LogFile=''+$LogFile
	If 0=VarType($Message)
		Exit(1)
	EndIf

	If $LogFile
		; Check if logfile directory exists..
		If ReDirectOutput($LogFile,0)=0
			? "$Message"
			$=ReDirectOutput("")
		Else
			Exit(3)
		EndIf
	EndIf
EndFunction 


How to mail someone if there was some kind of error?
Lots of ways to get you there. I use Blat.exe to send e-mail messages from my scripts. Don't have an example at hand here right now but is it very easy to sue blat from a kix script.
http://www.blat.net/
http://www.blat.net/examples/kix.html
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1619 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.027 seconds in which 0.013 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org