Tonight, I will be continuing the series of articles on the control flow tasks available in SQL Server Integration Services (SSIS) to perform Database Administration (DBA) tasks. We will be designing a SSIS package that executes a differential backup on all user databases and save the results to a directory of my choosing.
One of the most important job functions of a DBA is to have a recent backup chain in which a point in time restore can be performed. A backup chain consists of the most recent FULL backup, differential (DIFF) backup, and transaction LOG backup.
I am going to start up Visual Studio and create a project named “diff-user-backup” under the “c:\ssis-depot\” directory. Upon having a blank package, I am cheat by re-using the “full-user-backup” package as a starting point.
Go to the solution explorer and right clicking the SSIS Packages object. There is an option to add existing package. Do not forget to make this package the starting object by right clicking the imported package. Remove the default blank package that was part of the solution. Make sure the resulting package has the correct name by right clicking and renaming the file.
Wow, we just saved ourselves a ton of work!
I am going to list each of the objects in the package below. Some objects will need minor changes.
cmMySqlServer – The database server connection information will remain the same.
cmMySmtpServer – The mail server connection information will remain the same.
tskDiffUserBackup – Switch the backup type to differential for this renamed object.
tskEmailSuccess – Change the mail subject to reflect a transaction log backup.
tskEmailFailure – Change the mail subject to reflect a transaction log backup.
The screen shot below shows the backup database task edit window. The most important modification to be made is changing the backup type.
Now, lets run the package to completion.
I just noticed a success email that was sent to my email account and it appear in MS Outlook application.
I wonder what the SSIS package is actually doing to perform the backup? To discover what T-SQL is being sent to the server, we can open a client/server side trace using SQL Server Profiler. I am going to skip over the mechanics of starting a profiler trace and running the SSIS package in debug mode.
The whole trace file (“diff-user-backup.trc”) along with the SSIS package (“diff-user-backup.dtsx”) are included as a download bundle at the end of the article.
Basically, the TSQL commands can be broken down into three parts. Part one is to create the sub-directories for each database if they do not exist. Part two is to perform a full backup on each database. Part three is to verify the backup that was just taken.
I cleaned up the code for the [MATH] database that was sent by the SSIS package to the database.
-- TASK #1
-- Create the sub-directory if it does not exist
-- TASK #2
-- Backup the database with the choosen options
BACKUP DATABASE [MATH]
TO DISK = N'C:\mssql\backup\MATH\MATH_backup_2012_12_01_082808_1106355.bak'
WITH DIFFERENTIAL , NOFORMAT, NOINIT, NAME = N'MATH_backup_2012_12_01_082808_1106355',
SKIP, REWIND, NOUNLOAD, COMPRESSION, STATS = 10
-- TASK #3
-- Find the recent backup set
declare @backupSetId as int
@backupSetId = position
backup_set_id=(select max(backup_set_id) from msdb..backupset where database_name=N'MATH' )
-- Error out if we do not find a backup
if @backupSetId is null
raiserror(N'Verify failed. Backup information for database ''MATH'' not found.', 16, 1)
-- Verify the backup
FROM DISK = N'C:\mssql\backup\MATH\MATH_backup_2012_12_01_082808_1106355.bak'
WITH FILE = @backupSetId, NOUNLOAD, NOREWIND
In summary, the Backup Database Task can be used to create a package that can be schedule to perform automated DIFFERENTIAL backups. This design pattern includes alerting for both job success and job failure. A best practice for backups is to save the backup file to another server and swipe to either tape, disk, or the cloud.
While this package serves one purpose, how do we have it be re-usable? We can make both the SQL Server connection information, SMTP Server connection information, the BACKUP directory path, the MAIL subject text as project parameters. I will leave this exercise for you to complete.
In my next article, we will exploring a control flow task to purge these backups after the data retention period has expired. I hope you find this package a starting point for automating your differential backups.