Job one as a DBA is to ensure that the systems under your control are backed up. Actually, job one is to make sure that systems are restorable, but that starts with robust backup jobs.
One job that I always set up on my personal SQL Server instances is a catch-all backup job. A job, that is, that backups up all databases except for ones that are specifically excluded. Why? I don’t always think about creating the backup jobs just because I’ve added a new database. A shortcoming, to be sure, but even on those personal system I want to be protected.
I am a firm believer that backups on enterprise systems should be done via dedicated backup jobs due to the variety of RPO/RTO requirements associated with those systems, but I suppose that a case could be made for a catch-all backup job there as well.
The Catch-All Backup Job
First, we need a table to store a list of databases that will be excluded from the backups. I put this in a database called Admin.
create table dbo.DatabasesExcludedFromBackup ( DatabaseName sysname not null ); insert dbo.DatabasesExcludedFromBackup (DatabaseName) values ('tempdb'); |
Be sure to add other databases that don’t need to be covered this job, including databases that are backed up through other jobs, or databases like AdventureWorks that you can easily recreate from the original source.
Automated Restore Script Generation
I also like to throw in some code to generate restore scripts automatically. We need a couple of tables to store this (also in the Admin database).
create table dbo.BackupType ( BackupType varchar(10) not null, Description nvarchar(100) null, constraint pk_BackupType primary key clustered (BackupType) ); create table dbo.BackupChainType ( BackupChainType varchar(25) not null, Description nvarchar(100) null, constraint pk_BackupChainType primary key clustered (BackupChainType) ); insert dbo.BackupType (BackupType, Description) values ('Diff', 'Differential Backup'), ('Full', 'Full backup'), ('Log', 'Transaction log backup'); insert dbo.BackupChainType (BackupChainType, Description) values ('Full+Diff', 'Full backup plus differential backups'), ('Full+Diff+Log', 'Full backup plus differential backups plus transaction log backups'), ('Full+Log', 'Full backup plus transaction log backups'), ('FullOnly', 'Full backup only'); create table dbo.RestoreScript ( RestoreScriptId int identity(1,1) not null, DatabaseName sysname not null, BackupChainType varchar(25) not null, BackupType varchar(10) not null, TimeGenerated datetime2(7) not null constraint df_RestoreScript__TimeGenerated default (sysdatetime()), ScriptText nvarchar(max) not null, constraint pk_RestoreScript primary key clustered (RestoreScriptId), constraint fk_RestoreScript__BackupChainType foreign key (BackupChainType) references dbo.BackupChainType (BackupChainType), constraint fk_RestoreScript__BackupType foreign key (BackupType) references dbo.BackupType (BackupType) ); |
The Full Backup Job
Next, create a SQL Agent job. I call my job “Backup all databases – FULL” and add a job step with this script. Be sure to set the location of the backup folder at the beginning of the script, and also change all references to the “Admin” database if necessary.
declare @dbName nvarchar(256), @recoveryModel nvarchar(60), @backupTime nvarchar(100), @fileName nvarchar(255), @sql nvarchar(4000), @sqlNoRecovery nvarchar(4000), @destinationFolder nvarchar(256); -- Set this to the appropriate backup folder. ** IMPORTANT ** It must end with a backslash! select @destinationFolder = 'S:\SQL\Backups\Full\'; declare dbCsr cursor for select name, recovery_model_desc from sys.databases where name not in (select DatabaseName from Admin.dbo.DatabasesExcludedFromBackup) and source_database_id is null; open dbCsr; fetch next from dbCsr into @dbName, @recoveryModel; while @@FETCH_STATUS = 0 begin select @backupTime = replace(replace(replace(CONVERT(nvarchar(100), getdate(), 120), '-', ''), ':', ''), ' ', '_'); select @fileName = @destinationFolder + @dbName + '.' + @backupTime + '.bak' select @sql = 'backup database [' + @dbName + '] to disk = ''' + @fileName + ''' with init, compression;'; exec(@sql); delete Admin.dbo.RestoreScript where DatabaseName = @dbName; select @sql = 'restore database [' + @dbName + '] from disk = ''' + @fileName + ''' with replace'; select @sqlNoRecovery = @sql + ', norecovery;'; select @sql += ';'; if @recoveryModel = 'SIMPLE' begin insert Admin.dbo.RestoreScript (DatabaseName, BackupChainType, BackupType, ScriptText) values (@dbName, 'FullOnly', 'Full', @sql); insert Admin.dbo.RestoreScript (DatabaseName, BackupChainType, BackupType, ScriptText) values (@dbName, 'Full+Diff', 'Full', @sqlNoRecovery); end else begin insert Admin.dbo.RestoreScript (DatabaseName, BackupChainType, BackupType, ScriptText) values (@dbName, 'Full+Log', 'Full', @sqlNoRecovery); insert Admin.dbo.RestoreScript (DatabaseName, BackupChainType, BackupType, ScriptText) values (@dbName, 'Full+Diff+Log', 'Full', @sqlNoRecovery); end fetch next from dbCsr into @dbName, @recoveryModel; end close dbCsr; deallocate dbCsr; |
Set an appropriate schedule on the job. For me, once per day works fine.
The Log Backup Job
Now create another job called “Backup all databases – LOG” and add this step:
declare @dbName nvarchar(256), @backupTime nvarchar(100), @fileName nvarchar(255), @sql nvarchar(4000), @destinationFolder nvarchar(256); -- Set this to the appropriate backup folder. ** IMPORTANT ** It must end with a backslash! select @destinationFolder = 'S:\SQL\Backups\Log\'; declare dbCsr cursor for select db.name from sys.databases db cross apply (select top 1 * from msdb.dbo.backupset bs where bs.database_name = db.name and bs.type = 'D' order by bs.backup_finish_date desc) bs where db.recovery_model_desc != 'SIMPLE' and db.name not in (select DatabaseName from Admin.dbo.DatabasesExcludedFromBackup) and db.source_database_id is null; open dbCsr; fetch next from dbCsr into @dbName; while @@FETCH_STATUS = 0 begin select @backupTime = replace(replace(replace(CONVERT(nvarchar(100), getdate(), 120), '-', ''), ':', ''), ' ', '_'); select @fileName = @destinationFolder + @dbName + '.' + @backupTime + '.trn' select @sql = 'backup log [' + @dbName + '] to disk = ''S:\Backup\SQL2014\Log\' + @dbName + '.' + @backupTime + '.trn'' with init, compression'; exec(@sql); select @sql = 'restore log [' + @dbName + '] from disk = ''' + @fileName + ''' with norecovery;'; insert Admin.dbo.RestoreScript (DatabaseName, BackupChainType, BackupType, ScriptText) values (@dbName, 'Full+Log', 'Log', @sql); insert Admin.dbo.RestoreScript (DatabaseName, BackupChainType, BackupType, ScriptText) values (@dbName, 'Full+Diff+Log', 'Log', @sql); fetch next from dbCsr into @dbName; end close dbCsr; deallocate dbCsr; |
Be sure to schedule the job according to your needs. I run log backups once per hour.
Additional Steps
You will want to add an extra step into your backup jobs to purge the old backup files after a few days, otherwise they will keep accumulating forever.
I also add a step to copy the files off-system because I am definitely a believer in getting backups off to another box as quickly as possible, just in case the machine blows up.
The Restore Script
The backup jobs generate restore scripts as they process the backups. You can find a list of the possible restore paths by executing:
select distinct DatabaseName, BackupChainType from Admin.dbo.RestoreScript; |
Then you can pass in a combination of DatabaseName and BackupChainType into a script such as the following:
select ScriptText from Admin.dbo.RestoreScript where DatabaseName = 'Admin' and BackupChainType = 'Full+Log' order by TimeGenerated asc; |
This will output the commands to restore the database to the last log backup. You will need to recover the database manually, and you easily select which logs to restore or add a STOPAT clause to recover to a point in time.
I even have a small SSIS package which runs a Foreach Loop and extract each restore script out to a text file. The few times I have needed to use it, these pre-generated scripts have made the restores trivially simple to execute.
Potential Enhancements
Even though the script tables allow for differential backups in the mix, there’s nothing here that really uses differentials. For my purposes, that’s overkill, but you can certainly add a “Backup all databases – DIFF’ and include logic to generate appropriate restore scripts.