30.06.2013 Views

SQL Server Backup and Restore - Simple Talk

SQL Server Backup and Restore - Simple Talk

SQL Server Backup and Restore - Simple Talk

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 8: Database <strong>Backup</strong> <strong>and</strong> <strong>Restore</strong> with <strong>SQL</strong> <strong>Backup</strong> Pro<br />

+ N''', DISKRETRYINTERVAL = 30,<br />

DISKRETRYCOUNT = 10, COMPRESSION = 3,<br />

THREADCOUNT = 2"'<br />

WHEN N'DIFF'<br />

THEN N'-<strong>SQL</strong> "BACKUP DATABASES [' + @DatabaseList<br />

+ N'] TO DISK = ''' + @<strong>Backup</strong>FileLocation<br />

+ N'.sqb'' WITH MAILTO_ONERRORONLY = '''<br />

+ @EmailOnFailure<br />

+ N''', DISKRETRYINTERVAL = 30,<br />

DISKRETRYCOUNT = 10, COMPRESSION = 3,<br />

THREADCOUNT = 2, DIFFERENTIAL"'<br />

END<br />

EXECUTE master..sqlbackup @<strong>SQL</strong><strong>Backup</strong>Comm<strong>and</strong>, @ExitCode OUTPUT,<br />

@ErrorCode OUTPUT<br />

-- If our backup operations return any errors or warnings execute below<br />

IF ( @ExitCode >= 500<br />

OR @ErrorCode > 0<br />

)<br />

BEGIN<br />

-- Raise an error to fail your backup job<br />

RAISERROR(N'<strong>Backup</strong> operation error', 10,1)<br />

END<br />

Listing 8-9: Reusable database backup code.<br />

The script starts by declaring the required variables, <strong>and</strong> the sets the values of the four<br />

confugurable variables, as below.<br />

• @<strong>Backup</strong>FileLocation – The backup path for our database backup files. This<br />

should be pointed at the centralized storage location. Also notice that the<br />

servername variable is used as a subdirectory. It is common practice to separate<br />

backup files by server, so this will use each server name as a subdirectory to store that<br />

set of backup files.<br />

• @EmailOnFailure – We always want to know when backups fail. In some environments,<br />

manually checking all database backup operations is just not feasible. Having<br />

alerts sent to the DBA team on failure is a good measure to have in place. Be sure to<br />

265

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!