Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 SQL Server Administration (2005)
 Get error status from Backup procedure

Author  Topic 

patshaw
Posting Yak Master

177 Posts

Posted - 2008-09-30 : 09:24:22
Hi,

As part of a log shipping routine that uses SQLBackup by Red-Gate, I have a cursor that performs the Tran Backups and moves them ready for restoring on the DR machine. I want to trap any errors in the backup process and email notification of this to the DBA's.

In the code below you will see after the backup code I have attempted to use @@ERROR to trap any errors but this appears to always return 0 even if I force an error by including a non-existent database or similar. Can anyone help me find a way to trap an error if one occurs and successfully send the email please.


USE MASTER;

declare @dbname sysname
declare @cmd varchar(2000)
declare @DateTag varchar(20)
DECLARE @Msg varchar(200)
DECLARE @MailSubject varchar(200)
declare csr cursor for
select [DatabaseName] from master.dbo.LogShipping_Out where Active = 1 order by 1
open csr
fetch next from csr into @dbname
while @@fetch_status = 0

begin
set @DateTag = convert(varchar(8),getdate(),112)+replace(convert(varchar(10),getdate(),108),':','')
set @cmd = N'exec master..sqlbackup ''-SQL "backup log [' + @dbname + '] to disk =[D:\TRN_BAKS\' + @dbname + '\LOG_' + @dbname + '_' + @DateTag + '.sqb] WITH COMPRESSION = 3, THREADCOUNT = 4, ERASEFILES = 168h, COPYTO = [\\AMCATDR\D$\TRLOG_LOADING\AMCATSQL_LOGS\' + @dbname + ']" '''

print @cmd
exec (@cmd)

IF (@@ERROR <> 0)
BEGIN
set @MailSubject = N'** LOG SHIPPING ERROR ** - ''' + @dbname + ''' Transaction Log Backup Failed'
set @Msg = N'The Transaction Log Backup for database ''' + @dbname + ''' Failed.'
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Data Admins'
, @recipients = 'datateam@kkk.co.uk'
, @subject = @MailSubject
, @body = @Msg
, @importance = 'high';
END

fetch next from csr into @dbname
end

close csr
deallocate csr

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-09-30 : 10:30:40
I suspect that the BACKUP process prints some status messages after the failure so the value of
@@error will be 0 (for the print messages). As you know @@error is for the previous statement only.
Perhaps you should base your success/failure on the existance of the .bak file you are creating.

EDIT:
added cariage returns

Be One with the Optimizer
TG
Go to Top of Page

patshaw
Posting Yak Master

177 Posts

Posted - 2008-09-30 : 14:12:02
Thanks. What would you suggest would be the best approach for this? Would you query the MSDB system tables and if so which ones, or check for the existence of the file using xp_cmdshell?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-09-30 : 14:27:40
Just put your code into a job step. The job step will fail if the backup fails. On job failure, you can email the DBAs.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

patshaw
Posting Yak Master

177 Posts

Posted - 2008-09-30 : 16:01:27
The down side to this Tara is I would lose the facility to detail the name of the actual database(s) that had failed in backup. As there are about 90 databases included in the log shipping this would have been good value.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-09-30 : 16:12:15
Doesn't the Red Gate log shipping have a log shipping monitor like the built-in log shipping does?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

patshaw
Posting Yak Master

177 Posts

Posted - 2008-09-30 : 16:46:49
Yes it does. It is a standalone program but I must admit I haven't had chance to explore it fully yet. I do know it has an alert system but I don't think this incorporates email alerts which is what I was aiming for. It is just a pity I cannot trigger the email from my initial backup code. I will have to explore this further and see if I can find a way to get it to work. I am sure it is possible as most things are.

Thanks.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-09-30 : 16:48:13
Since this is for SQL Server 2005, why not just use TRY/CATCH? I believe that'll catch the exception.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

patshaw
Posting Yak Master

177 Posts

Posted - 2008-09-30 : 16:55:18
Now that's worth exploring. I'll try this tomorrow and let you know how I get on. Many thanks Tara.
Go to Top of Page
   

- Advertisement -