Backups: Doing it right so you can recover quickly!– Automating the backups

Published Mar 23 2019 12:11 PM 282 Views
Microsoft
First published on TECHNET on May 13, 2011

[Prior Post] [Next Post]


Getting backups scheduled appropriately tends to be a slow process with a lot of keying and mouse clicks.  The process can be simplified by the use of a couple of scripts that will backup every database on your SQL Server (including databases added to your SQL Server later! )


There are three types of backups:



  • Full backup – complete copy of the database

  • Differential backup – the records that have changed since the last full backup

    • Note: Differential is not just the records changed since the last diffferential



  • Transaction log backup – the records that have changed since the last log backup.

    • Does not apply to databases with a SIMPLE recovery model




The pattern that I coded below is to:



  • Do a full backup every weekend

  • Do a differential backup every night

  • Do a transaction log backup every hour during the working weekday.


If this pattern is not satisfactory for you needs, feel free to leave a comment on what you need and I will provide it in a follow up post.


I am providing you the option of doing two types of backups:



  • Rollover backup -- the data overwrites older data that has expired (21 days of data is kept).

  • Weekly-distinct backup – the backup is done to a different file every week and you have unlimited backups.


Roll-over backups allow the latest version to be recovered only, with the advantage that you reuse disk space. Weekly-distinct backups have the disadvantage of consuming more and more disk space, but they allow you to recover to any point in the past , as long as you have a backup for it. The table below illustrates the backup files you will see for database “ISV”.



Rollover Backup


Weekly Distinct Backup
ISV Backup.bak ISV 20110430.bak
ISV 20110507.bak
ISV 20110514.bak
ISV 20110521.bak
ISV 20110528.bak

The choice often depends on any potential need to rollback to a point of time. Typical needs driving weekly distinct backups are:



  • Detecting fraud by a sophisticated user.

  • Ability to recover lost data that was accidentally deleted but not discovered immediately.


The Scripts Summary


The scripts build on each other, so I will describe them from the bottom up.



  • [SQLISV_DoBackup] – backups one database as described above. You supply the name of the database.

  • [SQLISV_BackupAllDatabases] – backs up all of the databases on the SQL server.

  • [SQLISV_SchduleBackupAllDatabases] – adds the jobs to execute [SQLISV_BackupAllDatabases] regularly.


The scripts are adaptive . If you add a database, or the server is down when a backup should happen, the scripts adapt to these conditions.


SQLISV_DoBackup Script


This script takes four parameters that you may wish to change the default values for:



  • @DBName – the name of the database

  • @BackupPath – the physical path to the folder to use (with an ending \ )

  • @MinLogSize – sets how big the transaction log should be before a log backup occurs

  • @Overwrite – determines if a roll-over or weekly-distinct backup is done.

  • @CheckDb -- determines if the database integrity is checked before doing a backup

    • 2 is the recommended production setting (you want backups to be perfect)

    • 0 is the default until you have timed how long DBCC CHECKDB takes (if may take days)




There are many options available as shown in help . I tried to keep things simple but I decided to put compression on because it detects disk write errors.


CREATE PROC [dbo].[SQLISV_DoBackup]
@DBName Sysname=null,
@BackupPath nvarchar(255)='P:\Backups\',
@MinLogSize int =1,
@Overwrite bit = 1,
@CheckDb tinyint = 0
-- 0: Nocheck
-- 1:Check on FULL
-- 2: Check on FULL and Differertial
AS
SET NOCOUNT ON
DECLARE @BackupFileName nvarchar(max),
@WeekDate Date,
@Cmd Nvarchar(max),
@WithOptions Nvarchar(max),
@DayOffset int,
@Hour int,
@SimpleRecoverModel bit = 0,
@FullBackupHour int=1,
@FullBackupDayOffset int=1

IF @DBName is Null
SET @DBName=DB_Name()
IF @DBName in ('tempdb')
BEGIN
Print 'TempDb is not backed up'
RETURN
END

IF (SELECT DATABASEPROPERTYEX(@DBName,'Recovery'))='SIMPLE'
SET @SimpleRecoverModel =1
SET @Hour=DatePart(hh,GetDate())
SET @WeekDate=GetDate()
SET @DayOffset=DatePart(dw,@WeekDate)
SET @WeekDate=DateAdd(dy,-@DayOffset,@WeekDate)
IF @Overwrite = 0
BEGIN
SET @BackupFileName= @BackupPath
+ REPLACE(@@SERVERNAME,'\','-')+' '
+ @DBName +' '
+ Convert(varchar(8),@WeekDate, 112)
+'.bak'
SET @WithOptions ='COMPRESSION'
END
ELSE
BEGIN
SET @BackupFileName= @BackupPath
+ REPLACE(@@SERVERNAME,'\','-')+' '
+ @DBName +' Backup'
+'.bak'
SET @WithOptions ='COMPRESSION, RETAINDAYS=21'
END
-- Borrowed from early post, we want to determine if log is above threshold
IF NOT EXISTS(SELECT name from sysobjects where name='SQLISV_LogSpace' and type='P')
BEGIN
SET @CMD='CREATE Proc SQLISV_LogSpace AS DBCC SQLPERF(logspace)'
print @Cmd
EXEC (@Cmd)
END

CREATE TABLE #LogSpace
(
DBName sysname,
logSize decimal(18,5),
logUsed decimal(18,5),
status int
)
INSERT INTO #LogSpace exec SQLISV_LogSpace

-- Always do a full backup on a new file
IF NOT EXISTS (SELECT 1 FROM msdb.dbo.backupset
JOIN msdb.dbo.backupmediafamily
ON backupset.media_set_id = backupmediafamily.media_set_id
AND is_copy_only = 0 -- Is NOT COPY ONLY
WHERE database_name = @DBName
AND [type]='D' -- Is FULL BACKUP
AND @BackupFileName=physical_device_name -- to Right Location
AND DateDiff(hh,backup_start_date,GetDate()) < 7 * 24 +1
-- Full backups must occur at least once every 7 days.
)
OR -- At specified time and day of week
(@FullBackupHour=@Hour AND @FullBackupDayOffset = @DayOffset)
BEGIN
IF @CheckDB > 0
BEGIN
SET @CMD='DBCC CHECKDB (['+@DBName + ']) WITH NO_INFOMSGS'
print @Cmd
EXEC (@CMD)
END
SET @CMD='BACKUP DATABASE ['+@DBName + '] TO DISK = '''
+ @BackupFileName+''' WITH '+@WithOptions
print @Cmd
EXEC (@CMD)
IF @DBName in ('Master','model','msdb')
OR @SimpleRecoverModel =1 -- Can't backup LOG on SIMPLE
return
SET @CMD='BACKUP LOG ['+@DBName + '] TO DISK = '''
+ @BackupFileName+''' WITH '+@WithOptions
Print @Cmd
EXEC (@CMD)
RETURN
END
IF @DBName in ('Master','model','msdb')
BEGIN
Print @DBName
Print 'Master,model,msdb has been backedup this week'
RETURN
END
-- DIFFERENTIAL IS DONE AT 1 AM (or next opportunity if machine is down at 1AM)
IF not exists(SELECT 1
FROM msdb.dbo.backupset
JOIN msdb.dbo.backupmediafamily
ON backupset.media_set_id = backupmediafamily.media_set_id
AND is_copy_only = 0
WHERE [type]='I' -- Differential Backup
AND database_name = @DBName
AND @BackupFileName=physical_device_name
-- No more than one every 12 hrs
AND DateDiff(hh,backup_start_date,GetDate()) < 12
-- Should happen at 1AM but if machine offline, then next time
AND @hour BETWEEN 1 AND 7 -- Between 1AM and 7AM
)
BEGIN
IF @CheckDB > 1
BEGIN
SET @CMD='DBCC CHECKDB (['+@DBName + ']) WITH NO_INFOMSGS'
print @Cmd
EXEC (@CMD)
END
SET @CMD='BACKUP DATABASE ['+@DBName + '] TO DISK = '''
+ @BackupFileName+''' WITH DIFFERENTIAL, ' +@WithOptions
Print @Cmd
EXEC (@CMD)
RETURN
END
ELSE
IF EXISTS(SELECT 1 FROM #LogSpace
WHERE Cast(logSize* logUsed/100 as decimal(18,2)) > @MinLogSize
AND DBName=@DBName)
BEGIN
IF not exists(SELECT 1 FROM msdb.dbo.backupset
JOIN msdb.dbo.backupmediafamily
ON backupset.media_set_id = backupmediafamily.media_set_id
AND is_copy_only = 0
WHERE [type]='L'
AND DateDiff(mm,GetDate(),backup_start_date) > 50
AND database_name = @DBName
AND @BackupFileName=physical_device_name)
AND @SimpleRecoverModel =0 -- Can't backup LOG on SIMPLE
BEGIN

SET @CMD='BACKUP LOG ['+@DBName + '] TO DISK = '''
+ @BackupFileName+''' WITH '+@WithOptions
Print @Cmd
EXEC (@CMD)
RETURN
END
ELSE
BEGIN
Print 'Log is too small - delaying'
RETURN
END
END
Print 'Nothing happened'
GO
CREATE PROC [dbo].[SQLISV_BackupAllDatabases]
@BackupPath nvarchar(255)='P:\Backups\',
@MinLogSize int =1000
AS
DECLARE @DBID int=1,
@DBName Sysname
DECLARE @DBTable table (DBID int identity(1,1),DBName sysname)
INSERT INTO @DBTable (DBName)
SELECT Name FROM sys.databases
WHILE 1=1
BEGIN
SELECT @DBName=DBName FROM @DBTable WHERE @DBID=DBID
IF @@ROWCOUNT > 0
BEGIN TRY
SET @DBID +=1
EXEC [SQLTools].dbo.[SQLISV_DoBackup]
@DBName=@DBName,
@BackupPath=@BackupPath,
@MinLogSize=@MinLogSize
END TRY
BEGIN CATCH
PRINT ERROR_MESSAGE()
END CATCH
ELSE
BREAK
END
GO


When this executes, I print out the backup TSQL commands executed to facilitate debugging.



SQLISV_BackupAllDatabases Script


This script walks the databases and calls the above script for each one. I assumed @Overwrite is defaulted to the appropriate value above and retained only two of the parameters.



USE [SQLTools]
GO
CREATE PROC [dbo].[SQLISV_BackupAllDatabases]
@BackupPath nvarchar(255)='P:\Backups\',
@MinLogSize int =1000
AS
DECLARE @DBID int=1,
@DBName Sysname
DECLARE @DBTable table (DBID int identity(1,1),DBName sysname)
INSERT INTO @DBTable (DBName)
SELECT Name FROM sys.databases
WHILE 1=1
BEGIN
SELECT @DBName=DBName FROM @DBTable WHERE @DBID=DBID
IF @@ROWCOUNT > 0
BEGIN TRY
SET @DBID +=1
EXEC [SQLTools].dbo.[SQLISV_DoBackup]
@DBName=@DBName,
@BackupPath=@BackupPath,
@MinLogSize=@MinLogSize
END TRY
BEGIN CATCH
PRINT ERROR_MESSAGE()
END CATCH
ELSE
BREAK
END
GO


I would suggest changing the Catch block to send a notification out, for example an email.


SQLISV_ScheduleBackupAllDatabases Script


The script below will add jobs to call the above script if they do not exist and replace jobs if they do exist.



USE [SQLTools]
GO

CREATE PROC [dbo].[SQLISV_ScheduleBackupAllDatabases]
AS
BEGIN
IF Exists(SELECT 1 FROM msdb.dbo.sysjobs
WHERE name='SQLISV_BackupAllDatabases')
Exec msdb.dbo.sp_delete_job @job_name='SQLISV_BackupAllDatabases'
Exec msdb.dbo.sp_add_job @job_name='SQLISV_BackupAllDatabases',
@notify_level_eventlog=3
Exec msdb.dbo.sp_add_jobstep @job_name='SQLISV_BackupAllDatabases',
@Step_Name='SQLISV_BackupAllDatabases_1',
@Command='EXEC [SQLTools].[dbo].[SQLISV_BackupAllDatabases]'
IF Exists(Select 1 from msdb.dbo.sysschedules
WHERE name='SQLISV_BackupAllDatabases')
EXEC msdb.dbo.sp_delete_schedule @schedule_name = 'SQLISV_BackupAllDatabases'
Exec msdb.dbo.sp_add_schedule @schedule_name = 'SQLISV_BackupAllDatabases',
@freq_type = 4, --Daily
@freq_interval=1,
@freq_subday_type=0x8, -- Hour
@freq_subday_interval=1
EXEC msdb.dbo.sp_attach_schedule
@job_name = N'SQLISV_BackupAllDatabases',
@schedule_name = N'SQLISV_BackupAllDatabases' ;
END
GO
EXEC [dbo].[SQLISV_ScheduleBackupAllDatabases]


Did it work?


In the SQLISV_ScheduleBackupAllDatabases code above, I specified  @notify_level_eventlog=3. This setting means that successful and unsuccessful backups are recorded in the Event Log as shown below.



Some of the backups failed, so I needed to investigate why. I can manually execute the backup and attempt to identify what the problem is. An example of this process is shown below.



The code above tests for this condition and omits the log backups for databases with a SIMPLE recovery model. This was a contrived failure. If you encounter any failures, please comment below.


Technical Issues


The following are some technical points that may be of some interest.



  • The code includes the Server Instance name in the backup name. This is needed to prevent namespace collision if there are two instances on the same machine, or if one backup location is used by several servers. Each instance would have the following standard databases: [msdb], [model] and [master]

  • I have kept the use of backup options lean so we can focus on the process. You may wish to review these options in help and modify the scripts to your needs.


Bottom Line


My experience is that getting backups configured is tedious work, prone to errors. This post shows you how you can automate the entire process by just downloading the TSQL script here and executing it once on each server. The backup pattern may not be perfect, but it is better than what I have often seen.  The scripts above should be robust, and best of all, if you add a database to SQL Server it will be included in the backups automatically.


SQLTools: Where to Save these Utilities?


I had a discussion with Shaun Tinline-Jones of the SQL Server Customer Advisory Team - SQL Server Best Practices team(SQLCAT) about where to locate this procedure and other like it. Should it be in [msdb], [model],[master], an existing ISV related database or a new database. Our conclusion was a new database named [SQLTools] was best (instead of [SQLISV] or [SQLCAT]).


To create this database


?


Create Database [SQLTools]
Go
%3CLINGO-SUB%20id%3D%22lingo-sub-383871%22%20slang%3D%22en-US%22%3EBackups%3A%20Doing%20it%20right%20so%20you%20can%20recover%20quickly!%E2%80%93%20Automating%20the%20backups%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-383871%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3E%20First%20published%20on%20TECHNET%20on%20May%2013%2C%202011%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CP%3E%3CA%20href%3D%22http%3A%2F%2Fblogs.technet.com%2Fb%2Fsql_server_isv%2Farchive%2F2011%2F05%2F06%2Fbackups-doing-it-right-so-you-can-recovery-quickly-don-t-keep-your-eggs-in-one-basket.aspx%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3E%5BPrior%20Post%5D%20%3C%2FA%3E%20%5BNext%20Post%5D%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EGetting%20backups%20scheduled%20appropriately%20tends%20to%20be%20a%20slow%20process%20with%20a%20lot%20of%20keying%20and%20mouse%20clicks.%26nbsp%3B%20The%20process%20can%20be%20simplified%20by%20the%20use%20of%20a%20couple%20of%20scripts%20that%20will%20backup%20every%20database%20on%20your%20SQL%20Server%20(including%20%3CEM%3E%20%3CSTRONG%3E%20databases%20added%20to%20your%20SQL%20Server%20later!%20%3C%2FSTRONG%3E%20%3C%2FEM%3E%20)%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EThere%20are%20three%20types%20of%20backups%3A%3C%2FP%3E%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3EFull%20backup%20%E2%80%93%20complete%20copy%20of%20the%20database%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3E%3CA%20href%3D%22http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fms175526.aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EDifferential%20backup%20%3C%2FA%3E%20%E2%80%93%20the%20records%20that%20have%20changed%20since%20the%20last%20%3CSTRONG%3E%20full%20backup%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3ENote%3A%20Differential%20is%20not%20just%20the%20records%20changed%20since%20the%20last%20diffferential%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3E%3CA%20href%3D%22http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fms175477.aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ETransaction%20log%20backup%20%3C%2FA%3E%20%E2%80%93%20the%20records%20that%20have%20changed%20since%20the%20last%26nbsp%3Blog%26nbsp%3Bbackup.%20%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3EDoes%20not%20apply%20to%20databases%20with%20a%20SIMPLE%20recovery%20model%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%3CP%3EThe%20pattern%20that%20I%20coded%20below%20is%20to%3A%3C%2FP%3E%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3EDo%20a%20full%20backup%20every%20weekend%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3EDo%20a%20differential%20backup%20every%20night%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3EDo%20a%20transaction%20log%20backup%20every%20hour%20during%20the%20working%20weekday.%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%3CP%3EIf%20this%20pattern%20is%20not%20satisfactory%20for%20you%20needs%2C%20feel%20free%20to%20leave%20a%20comment%20on%20what%20you%20need%20and%20I%20will%20provide%20it%20in%20a%20follow%20up%20post.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EI%26nbsp%3Bam%20providing%26nbsp%3Byou%20the%20option%20of%20doing%20two%20types%20of%20backups%3A%3C%2FP%3E%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3ERollover%20backup%20--%20the%20data%20overwrites%20older%20data%20that%20has%20expired%20(21%20days%20of%20data%20is%20kept).%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3EWeekly-distinct%20backup%20%E2%80%93%20the%20backup%20is%20done%20to%20a%20different%20file%20every%20week%20and%20you%20have%20unlimited%20backups.%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%3CP%3ERoll-over%20backups%20allow%20the%20%3CEM%3E%20latest%20%3C%2FEM%3E%20version%20to%20be%20recovered%20only%2C%20with%20the%20advantage%20that%20you%20reuse%20disk%20space.%20Weekly-distinct%20backups%20have%20the%20disadvantage%20of%20consuming%20more%20and%20more%20disk%20space%2C%20but%20they%20allow%20you%20to%20recover%20to%20%3CEM%3E%20any%20point%20in%20the%20past%20%3C%2FEM%3E%20%2C%20as%20long%20as%20you%20have%20a%20backup%20for%20it.%20The%20table%20below%20illustrates%20the%20backup%20files%20you%20will%20see%20for%20database%20%E2%80%9CISV%E2%80%9D.%3C%2FP%3E%3CBR%20%2F%3E%3CTABLE%3E%0A%20%20%20%3CTBODY%3E%3CTR%3E%3CBR%20%2F%3E%3CP%3ERollover%20Backup%3C%2FP%3E%3CBR%20%2F%3E%20Weekly%20Distinct%20Backup%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3EISV%20Backup.bak%3C%2FTD%3E%0A%20%20%20%20%3CTD%3EISV%2020110430.bak%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E%3C%2FTD%3E%0A%20%20%20%20%3CTD%3EISV%2020110507.bak%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E%3C%2FTD%3E%0A%20%20%20%20%3CTD%3EISV%2020110514.bak%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E%3C%2FTD%3E%0A%20%20%20%20%3CTD%3EISV%2020110521.bak%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%20%3CTR%3E%0A%20%20%20%20%3CTD%3E%3C%2FTD%3E%0A%20%20%20%20%3CTD%3EISV%2020110528.bak%3C%2FTD%3E%0A%20%20%20%3C%2FTR%3E%0A%20%20%3C%2FTBODY%3E%3C%2FTABLE%3E%3CBR%20%2F%3E%3CP%3EThe%20choice%20often%20depends%20on%20any%20potential%20need%20to%20rollback%20to%20a%20point%20of%20time.%20Typical%20needs%20driving%20weekly%20distinct%20backups%20are%3A%3C%2FP%3E%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3EDetecting%20fraud%20by%20a%20sophisticated%20user.%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3EAbility%20to%20recover%20lost%20data%20that%20was%20accidentally%20deleted%20but%20not%20discovered%20immediately.%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%3CH2%20id%3D%22toc-hId-1680695748%22%20id%3D%22toc-hId-1706491941%22%3EThe%20Scripts%20Summary%3C%2FH2%3E%3CBR%20%2F%3E%3CP%3EThe%20scripts%20build%20on%20each%20other%2C%20so%20I%20will%20describe%20them%20from%20the%20bottom%20up.%3C%2FP%3E%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3E%5BSQLISV_DoBackup%5D%20%E2%80%93%20backups%20one%20database%20as%20described%20above.%20You%20supply%20the%20name%20of%20the%20database.%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3E%5BSQLISV_BackupAllDatabases%5D%20%E2%80%93%20backs%20up%20%3CSTRONG%3E%20all%20%3C%2FSTRONG%3E%20of%20the%20databases%20on%20the%20SQL%20server.%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3E%5BSQLISV_SchduleBackupAllDatabases%5D%20%E2%80%93%20adds%20the%20jobs%20to%20execute%20%5BSQLISV_BackupAllDatabases%5D%20regularly.%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%3CP%3EThe%20scripts%20are%20%3CEM%3E%20adaptive%20%3C%2FEM%3E%20.%20If%20you%20add%20a%20database%2C%20or%20the%20server%20is%20down%20when%20a%20backup%20should%20happen%2C%20the%20scripts%20adapt%20to%20these%20conditions.%3C%2FP%3E%3CBR%20%2F%3E%3CH3%20id%3D%22toc-hId--1067974718%22%20id%3D%22toc-hId--1042178525%22%3ESQLISV_DoBackup%20Script%3C%2FH3%3E%3CBR%20%2F%3E%3CP%3EThis%20script%20takes%20four%20parameters%20that%20you%20may%20wish%20to%20change%20the%20default%20values%20for%3A%3C%2FP%3E%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3E%40DBName%20%E2%80%93%20the%20name%20of%20the%20database%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3E%40BackupPath%20%E2%80%93%20the%20physical%20path%20to%20the%20folder%20to%20use%20(with%20an%20ending%20%5C%20)%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3E%40MinLogSize%20%E2%80%93%20sets%20how%20big%20the%20transaction%20log%20should%20be%20before%20a%20log%20backup%20occurs%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3E%40Overwrite%20%E2%80%93%20determines%20if%20a%20roll-over%20or%20weekly-distinct%20backup%20is%20done.%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3E%40CheckDb%20--%20determines%20if%20the%20database%20integrity%20is%20checked%20before%20doing%20a%20backup%20%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3E2%20is%20the%20recommended%20production%20setting%20(you%20want%20backups%20to%20be%20perfect)%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3E0%20is%20the%20default%20until%20you%20have%20timed%20how%20long%20DBCC%20CHECKDB%20takes%20(if%20may%20take%20days)%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%3CP%3EThere%20are%20many%20options%20available%20as%20shown%20in%20%3CA%20href%3D%22http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fms186865.aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%20help%20%3C%2FA%3E%20.%20I%20tried%20to%20keep%20things%20simple%20but%20I%20decided%20to%20put%20compression%20on%20because%20it%20detects%20disk%20write%20errors.%3C%2FP%3E%3CBR%20%2F%3E%3CDIV%3ECREATE%20PROC%20%5Bdbo%5D.%5BSQLISV_DoBackup%5D%20%3CBR%20%2F%3E%20%40DBName%20Sysname%3Dnull%2C%20%3CBR%20%2F%3E%20%40BackupPath%20nvarchar(255)%3D'P%3A%5CBackups%5C'%2C%20%3CBR%20%2F%3E%20%40MinLogSize%20int%20%3D1%2C%20%3CBR%20%2F%3E%20%40Overwrite%20bit%20%3D%201%2C%20%3CBR%20%2F%3E%20%40CheckDb%20tinyint%20%3D%200%20%3CBR%20%2F%3E%20--%200%3A%20Nocheck%20%3CBR%20%2F%3E%20--%201%3ACheck%20on%20FULL%20%3CBR%20%2F%3E%20--%202%3A%20Check%20on%20FULL%20and%20Differertial%20%3CBR%20%2F%3E%20AS%20%3CBR%20%2F%3E%20SET%20NOCOUNT%20ON%20%3CBR%20%2F%3E%20DECLARE%20%40BackupFileName%20nvarchar(max)%2C%20%3CBR%20%2F%3E%20%40WeekDate%20Date%2C%20%3CBR%20%2F%3E%20%40Cmd%20Nvarchar(max)%2C%20%3CBR%20%2F%3E%20%40WithOptions%20Nvarchar(max)%2C%20%3CBR%20%2F%3E%20%40DayOffset%20int%2C%20%3CBR%20%2F%3E%20%40Hour%20int%2C%20%3CBR%20%2F%3E%20%40SimpleRecoverModel%20bit%20%3D%200%2C%20%3CBR%20%2F%3E%20%40FullBackupHour%20int%3D1%2C%20%3CBR%20%2F%3E%20%40FullBackupDayOffset%20int%3D1%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20IF%20%40DBName%20is%20Null%20%3CBR%20%2F%3E%20SET%20%40DBName%3DDB_Name()%20%3CBR%20%2F%3E%20IF%20%40DBName%20in%20('tempdb')%20%3CBR%20%2F%3E%20BEGIN%20%3CBR%20%2F%3E%20Print%20'TempDb%20is%20not%20backed%20up'%20%3CBR%20%2F%3E%20RETURN%20%3CBR%20%2F%3E%20END%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20IF%20(SELECT%20DATABASEPROPERTYEX(%40DBName%2C'Recovery'))%3D'SIMPLE'%20%3CBR%20%2F%3E%20SET%20%40SimpleRecoverModel%20%3D1%20%3CBR%20%2F%3E%20SET%20%40Hour%3DDatePart(hh%2CGetDate())%20%3CBR%20%2F%3E%20SET%20%40WeekDate%3DGetDate()%20%3CBR%20%2F%3E%20SET%20%40DayOffset%3DDatePart(dw%2C%40WeekDate)%20%3CBR%20%2F%3E%20SET%20%40WeekDate%3DDateAdd(dy%2C-%40DayOffset%2C%40WeekDate)%20%3CBR%20%2F%3E%20IF%20%40Overwrite%20%3D%200%20%3CBR%20%2F%3E%20BEGIN%20%3CBR%20%2F%3E%20SET%20%40BackupFileName%3D%20%40BackupPath%20%3CBR%20%2F%3E%20%2B%20REPLACE(%40%40SERVERNAME%2C'%5C'%2C'-')%2B'%20'%20%3CBR%20%2F%3E%20%2B%20%40DBName%20%2B'%20'%20%3CBR%20%2F%3E%20%2B%20Convert(varchar(8)%2C%40WeekDate%2C%20112)%20%3CBR%20%2F%3E%20%2B'.bak'%20%3CBR%20%2F%3E%20SET%20%40WithOptions%20%3D'COMPRESSION'%20%3CBR%20%2F%3E%20END%20%3CBR%20%2F%3E%20ELSE%20%3CBR%20%2F%3E%20BEGIN%20%3CBR%20%2F%3E%20SET%20%40BackupFileName%3D%20%40BackupPath%20%3CBR%20%2F%3E%20%2B%20REPLACE(%40%40SERVERNAME%2C'%5C'%2C'-')%2B'%20'%20%3CBR%20%2F%3E%20%2B%20%40DBName%20%2B'%20Backup'%20%3CBR%20%2F%3E%20%2B'.bak'%20%3CBR%20%2F%3E%20SET%20%40WithOptions%20%3D'COMPRESSION%2C%20RETAINDAYS%3D21'%20%3CBR%20%2F%3E%20END%20%3CBR%20%2F%3E%20--%20Borrowed%20from%20early%20post%2C%20we%20want%20to%20determine%20if%20log%20is%20above%20threshold%20%3CBR%20%2F%3E%20IF%20NOT%20EXISTS(SELECT%20name%20from%20sysobjects%20where%20name%3D'SQLISV_LogSpace'%20and%20type%3D'P')%20%3CBR%20%2F%3E%20BEGIN%20%3CBR%20%2F%3E%20SET%20%40CMD%3D'CREATE%20Proc%20SQLISV_LogSpace%20AS%20DBCC%20SQLPERF(logspace)'%20%3CBR%20%2F%3E%20print%20%40Cmd%20%3CBR%20%2F%3E%20EXEC%20(%40Cmd)%20%3CBR%20%2F%3E%20END%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20CREATE%20TABLE%20%23LogSpace%20%3CBR%20%2F%3E%20(%20%3CBR%20%2F%3E%20DBName%20sysname%2C%20%3CBR%20%2F%3E%20logSize%20decimal(18%2C5)%2C%20%3CBR%20%2F%3E%20logUsed%20decimal(18%2C5)%2C%20%3CBR%20%2F%3E%20status%20int%20%3CBR%20%2F%3E%20)%20%3CBR%20%2F%3E%20INSERT%20INTO%20%23LogSpace%20exec%20SQLISV_LogSpace%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20--%20Always%20do%20a%20full%20backup%20on%20a%20new%20file%20%3CBR%20%2F%3E%20IF%20NOT%20EXISTS%20(SELECT%201%20FROM%20msdb.dbo.backupset%20%3CBR%20%2F%3E%20JOIN%20msdb.dbo.backupmediafamily%20%3CBR%20%2F%3E%20ON%20backupset.media_set_id%20%3D%20backupmediafamily.media_set_id%20%3CBR%20%2F%3E%20AND%20is_copy_only%20%3D%200%20--%20Is%20NOT%20COPY%20ONLY%20%3CBR%20%2F%3E%20WHERE%20database_name%20%3D%20%40DBName%20%3CBR%20%2F%3E%20AND%20%5Btype%5D%3D'D'%20--%20Is%20FULL%20BACKUP%20%3CBR%20%2F%3E%20AND%20%40BackupFileName%3Dphysical_device_name%20--%20to%20Right%20Location%20%3CBR%20%2F%3E%20AND%20DateDiff(hh%2Cbackup_start_date%2CGetDate())%20%26lt%3B%207%20*%2024%20%2B1%20%3CBR%20%2F%3E%20--%20Full%20backups%20must%20occur%20at%20least%20once%20every%207%20days.%20%3CBR%20%2F%3E%20)%20%3CBR%20%2F%3E%20OR%20--%20At%20specified%20time%20and%20day%20of%20week%20%3CBR%20%2F%3E%20(%40FullBackupHour%3D%40Hour%20AND%20%40FullBackupDayOffset%20%3D%20%40DayOffset)%20%3CBR%20%2F%3E%20BEGIN%20%3CBR%20%2F%3E%20IF%20%40CheckDB%20%26gt%3B%200%20%3CBR%20%2F%3E%20BEGIN%20%3CBR%20%2F%3E%20SET%20%40CMD%3D'DBCC%20CHECKDB%20(%5B'%2B%40DBName%20%2B%20'%5D)%20WITH%20NO_INFOMSGS'%20%3CBR%20%2F%3E%20print%20%40Cmd%20%3CBR%20%2F%3E%20EXEC%20(%40CMD)%20%3CBR%20%2F%3E%20END%20%3CBR%20%2F%3E%20SET%20%40CMD%3D'BACKUP%20DATABASE%20%5B'%2B%40DBName%20%2B%20'%5D%20TO%20DISK%20%3D%20'''%20%3CBR%20%2F%3E%20%2B%20%40BackupFileName%2B'''%20WITH%20'%2B%40WithOptions%20%3CBR%20%2F%3E%20print%20%40Cmd%20%3CBR%20%2F%3E%20EXEC%20(%40CMD)%20%3CBR%20%2F%3E%20IF%20%40DBName%20in%20('Master'%2C'model'%2C'msdb')%20%3CBR%20%2F%3E%20OR%20%40SimpleRecoverModel%20%3D1%20--%20Can't%20backup%20LOG%20on%20SIMPLE%20%3CBR%20%2F%3E%20return%20%3CBR%20%2F%3E%20SET%20%40CMD%3D'BACKUP%20LOG%20%5B'%2B%40DBName%20%2B%20'%5D%20TO%20DISK%20%3D%20'''%20%3CBR%20%2F%3E%20%2B%20%40BackupFileName%2B'''%20WITH%20'%2B%40WithOptions%20%3CBR%20%2F%3E%20Print%20%40Cmd%20%3CBR%20%2F%3E%20EXEC%20(%40CMD)%20%3CBR%20%2F%3E%20RETURN%20%3CBR%20%2F%3E%20END%20%3CBR%20%2F%3E%20IF%20%40DBName%20in%20('Master'%2C'model'%2C'msdb')%20%3CBR%20%2F%3E%20BEGIN%20%3CBR%20%2F%3E%20Print%20%40DBName%20%3CBR%20%2F%3E%20Print%20'Master%2Cmodel%2Cmsdb%20has%20been%20backedup%20this%20week'%20%3CBR%20%2F%3E%20RETURN%20%3CBR%20%2F%3E%20END%20%3CBR%20%2F%3E%20--%20DIFFERENTIAL%20IS%20DONE%20AT%201%20AM%20(or%20next%20opportunity%20if%20machine%20is%20down%20at%201AM)%20%3CBR%20%2F%3E%20IF%20not%20exists(SELECT%201%20%3CBR%20%2F%3E%20FROM%20msdb.dbo.backupset%20%3CBR%20%2F%3E%20JOIN%20msdb.dbo.backupmediafamily%20%3CBR%20%2F%3E%20ON%20backupset.media_set_id%20%3D%20backupmediafamily.media_set_id%20%3CBR%20%2F%3E%20AND%20is_copy_only%20%3D%200%20%3CBR%20%2F%3E%20WHERE%20%5Btype%5D%3D'I'%20--%20Differential%20Backup%20%3CBR%20%2F%3E%20AND%20database_name%20%3D%20%40DBName%20%3CBR%20%2F%3E%20AND%20%40BackupFileName%3Dphysical_device_name%20%3CBR%20%2F%3E%20--%20No%20more%20than%20one%20every%2012%20hrs%20%3CBR%20%2F%3E%20AND%20DateDiff(hh%2Cbackup_start_date%2CGetDate())%20%26lt%3B%2012%20%3CBR%20%2F%3E%20--%20Should%20happen%20at%201AM%20but%20if%20machine%20offline%2C%20then%20next%20time%20%3CBR%20%2F%3E%20AND%20%40hour%20BETWEEN%201%20AND%207%20--%20Between%201AM%20and%207AM%20%3CBR%20%2F%3E%20)%20%3CBR%20%2F%3E%20BEGIN%20%3CBR%20%2F%3E%20IF%20%40CheckDB%20%26gt%3B%201%20%3CBR%20%2F%3E%20BEGIN%20%3CBR%20%2F%3E%20SET%20%40CMD%3D'DBCC%20CHECKDB%20(%5B'%2B%40DBName%20%2B%20'%5D)%20WITH%20NO_INFOMSGS'%20%3CBR%20%2F%3E%20print%20%40Cmd%20%3CBR%20%2F%3E%20EXEC%20(%40CMD)%20%3CBR%20%2F%3E%20END%20%3CBR%20%2F%3E%20SET%20%40CMD%3D'BACKUP%20DATABASE%20%5B'%2B%40DBName%20%2B%20'%5D%20TO%20DISK%20%3D%20'''%20%3CBR%20%2F%3E%20%2B%20%40BackupFileName%2B'''%20WITH%20DIFFERENTIAL%2C%20'%20%2B%40WithOptions%20%3CBR%20%2F%3E%20Print%20%40Cmd%20%3CBR%20%2F%3E%20EXEC%20(%40CMD)%20%3CBR%20%2F%3E%20RETURN%20%3CBR%20%2F%3E%20END%20%3CBR%20%2F%3E%20ELSE%20%3CBR%20%2F%3E%20IF%20EXISTS(SELECT%201%20FROM%20%23LogSpace%20%3CBR%20%2F%3E%20WHERE%20Cast(logSize*%20logUsed%2F100%20as%20decimal(18%2C2))%20%26gt%3B%20%40MinLogSize%20%3CBR%20%2F%3E%20AND%20DBName%3D%40DBName)%20%3CBR%20%2F%3E%20BEGIN%20%3CBR%20%2F%3E%20IF%20not%20exists(SELECT%201%20FROM%20msdb.dbo.backupset%20%3CBR%20%2F%3E%20JOIN%20msdb.dbo.backupmediafamily%20%3CBR%20%2F%3E%20ON%20backupset.media_set_id%20%3D%20backupmediafamily.media_set_id%20%3CBR%20%2F%3E%20AND%20is_copy_only%20%3D%200%20%3CBR%20%2F%3E%20WHERE%20%5Btype%5D%3D'L'%20%3CBR%20%2F%3E%20AND%20DateDiff(mm%2CGetDate()%2Cbackup_start_date)%20%26gt%3B%2050%20%3CBR%20%2F%3E%20AND%20database_name%20%3D%20%40DBName%20%3CBR%20%2F%3E%20AND%20%40BackupFileName%3Dphysical_device_name)%20%3CBR%20%2F%3E%20AND%20%40SimpleRecoverModel%20%3D0%20--%20Can't%20backup%20LOG%20on%20SIMPLE%20%3CBR%20%2F%3E%20BEGIN%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20SET%20%40CMD%3D'BACKUP%20LOG%20%5B'%2B%40DBName%20%2B%20'%5D%20TO%20DISK%20%3D%20'''%20%3CBR%20%2F%3E%20%2B%20%40BackupFileName%2B'''%20WITH%20'%2B%40WithOptions%20%3CBR%20%2F%3E%20Print%20%40Cmd%20%3CBR%20%2F%3E%20EXEC%20(%40CMD)%20%3CBR%20%2F%3E%20RETURN%20%3CBR%20%2F%3E%20END%20%3CBR%20%2F%3E%20ELSE%20%3CBR%20%2F%3E%20BEGIN%20%3CBR%20%2F%3E%20Print%20'Log%20is%20too%20small%20-%20delaying'%20%3CBR%20%2F%3E%20RETURN%20%3CBR%20%2F%3E%20END%20%3CBR%20%2F%3E%20END%20%3CBR%20%2F%3E%20Print%20'Nothing%20happened'%20%3CBR%20%2F%3E%20GO%20%3CBR%20%2F%3E%20CREATE%20PROC%20%5Bdbo%5D.%5BSQLISV_BackupAllDatabases%5D%20%3CBR%20%2F%3E%20%40BackupPath%20nvarchar(255)%3D'P%3A%5CBackups%5C'%2C%20%3CBR%20%2F%3E%20%40MinLogSize%20int%20%3D1000%20%3CBR%20%2F%3E%20AS%20%3CBR%20%2F%3E%20DECLARE%20%40DBID%20int%3D1%2C%20%3CBR%20%2F%3E%20%40DBName%20Sysname%20%3CBR%20%2F%3E%20DECLARE%20%40DBTable%20table%20(DBID%20int%20identity(1%2C1)%2CDBName%20sysname)%20%3CBR%20%2F%3E%20INSERT%20INTO%20%40DBTable%20(DBName)%20%3CBR%20%2F%3E%20SELECT%20Name%20FROM%20sys.databases%20%3CBR%20%2F%3E%20WHILE%201%3D1%20%3CBR%20%2F%3E%20BEGIN%20%3CBR%20%2F%3E%20SELECT%20%40DBName%3DDBName%20FROM%20%40DBTable%20WHERE%20%40DBID%3DDBID%20%3CBR%20%2F%3E%20IF%20%40%40ROWCOUNT%20%26gt%3B%200%20%3CBR%20%2F%3E%20BEGIN%20TRY%20%3CBR%20%2F%3E%20SET%20%40DBID%20%2B%3D1%20%3CBR%20%2F%3E%20EXEC%20%5BSQLTools%5D.dbo.%5BSQLISV_DoBackup%5D%20%3CBR%20%2F%3E%20%40DBName%3D%40DBName%2C%20%3CBR%20%2F%3E%20%40BackupPath%3D%40BackupPath%2C%20%3CBR%20%2F%3E%20%40MinLogSize%3D%40MinLogSize%20%3CBR%20%2F%3E%20END%20TRY%20%3CBR%20%2F%3E%20BEGIN%20CATCH%20%3CBR%20%2F%3E%20PRINT%20ERROR_MESSAGE()%20%3CBR%20%2F%3E%20END%20CATCH%20%3CBR%20%2F%3E%20ELSE%20%3CBR%20%2F%3E%20BREAK%20%3CBR%20%2F%3E%20END%20%3CBR%20%2F%3E%20GO%20%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CDIV%3E%0A%20%20%3C%2FDIV%3E%3CBR%20%2F%3E%3CP%3EWhen%20this%20executes%2C%20I%20print%20out%20the%20backup%20TSQL%20commands%20executed%20to%20facilitate%20debugging.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F97462iD58A7F32DD3DAA82%22%20%2F%3E%3C%2FP%3E%3CBR%20%2F%3E%3CH3%20id%3D%22toc-hId-674835617%22%20id%3D%22toc-hId-700631810%22%3ESQLISV_BackupAllDatabases%20Script%3C%2FH3%3E%3CBR%20%2F%3E%3CP%3EThis%20script%20walks%20the%20databases%20and%20calls%20the%20above%20script%20for%20each%20one.%20I%20assumed%20%40Overwrite%20is%20defaulted%20to%20the%20appropriate%20value%20above%20and%20retained%20only%20two%20of%20the%20parameters.%3C%2FP%3E%3CBR%20%2F%3E%3CDIV%3E%3CBR%20%2F%3E%3CDIV%3EUSE%20%5BSQLTools%5D%20%3CBR%20%2F%3E%20GO%20%3CBR%20%2F%3E%20CREATE%20PROC%20%5Bdbo%5D.%5BSQLISV_BackupAllDatabases%5D%20%3CBR%20%2F%3E%20%40BackupPath%20nvarchar(255)%3D'P%3A%5CBackups%5C'%2C%20%3CBR%20%2F%3E%20%40MinLogSize%20int%20%3D1000%20%3CBR%20%2F%3E%20AS%20%3CBR%20%2F%3E%20DECLARE%20%40DBID%20int%3D1%2C%20%3CBR%20%2F%3E%20%40DBName%20Sysname%20%3CBR%20%2F%3E%20DECLARE%20%40DBTable%20table%20(DBID%20int%20identity(1%2C1)%2CDBName%20sysname)%20%3CBR%20%2F%3E%20INSERT%20INTO%20%40DBTable%20(DBName)%20%3CBR%20%2F%3E%20SELECT%20Name%20FROM%20sys.databases%20%3CBR%20%2F%3E%20WHILE%201%3D1%20%3CBR%20%2F%3E%20BEGIN%20%3CBR%20%2F%3E%20SELECT%20%40DBName%3DDBName%20FROM%20%40DBTable%20WHERE%20%40DBID%3DDBID%20%3CBR%20%2F%3E%20IF%20%40%40ROWCOUNT%20%26gt%3B%200%20%3CBR%20%2F%3E%20BEGIN%20TRY%20%3CBR%20%2F%3E%20SET%20%40DBID%20%2B%3D1%20%3CBR%20%2F%3E%20EXEC%20%5BSQLTools%5D.dbo.%5BSQLISV_DoBackup%5D%20%3CBR%20%2F%3E%20%40DBName%3D%40DBName%2C%20%3CBR%20%2F%3E%20%40BackupPath%3D%40BackupPath%2C%20%3CBR%20%2F%3E%20%40MinLogSize%3D%40MinLogSize%20%3CBR%20%2F%3E%20END%20TRY%20%3CBR%20%2F%3E%20BEGIN%20CATCH%20%3CBR%20%2F%3E%20PRINT%20ERROR_MESSAGE()%20%3CBR%20%2F%3E%20END%20CATCH%20%3CBR%20%2F%3E%20ELSE%20%3CBR%20%2F%3E%20BREAK%20%3CBR%20%2F%3E%20END%20%3CBR%20%2F%3E%20GO%20%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CP%3EI%20would%20suggest%20changing%20the%20Catch%20block%20to%20send%20a%20notification%20out%2C%20for%20example%20an%20email.%3C%2FP%3E%3CBR%20%2F%3E%3CH3%20id%3D%22toc-hId--1877321344%22%20id%3D%22toc-hId--1851525151%22%3ESQLISV_ScheduleBackupAllDatabases%20Script%3C%2FH3%3E%3CBR%20%2F%3E%3CP%3EThe%20script%20below%20will%20add%20jobs%20to%20call%20the%20above%20script%20%3CEM%3E%20if%20they%20do%20not%20exist%20%3C%2FEM%3E%20and%20replace%20jobs%20if%20they%20do%20exist.%3C%2FP%3E%3CBR%20%2F%3E%3CDIV%3E%3CBR%20%2F%3E%3CDIV%3EUSE%20%5BSQLTools%5D%20%3CBR%20%2F%3E%20GO%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20CREATE%20PROC%20%5Bdbo%5D.%5BSQLISV_ScheduleBackupAllDatabases%5D%20%3CBR%20%2F%3E%20AS%3C%2FDIV%3E%0A%20%20%20%3CDIV%3EBEGIN%20%3CBR%20%2F%3E%3C%2FDIV%3E%0A%20%20%20%3CDIV%3EIF%20Exists(SELECT%201%20FROM%20msdb.dbo.sysjobs%20%3CBR%20%2F%3E%20WHERE%20name%3D'SQLISV_BackupAllDatabases')%20%3CBR%20%2F%3E%20Exec%20msdb.dbo.sp_delete_job%20%40job_name%3D'SQLISV_BackupAllDatabases'%20%3CBR%20%2F%3E%20Exec%20msdb.dbo.sp_add_job%20%40job_name%3D'SQLISV_BackupAllDatabases'%2C%20%3CBR%20%2F%3E%20%40notify_level_eventlog%3D3%20%3CBR%20%2F%3E%20Exec%20msdb.dbo.sp_add_jobstep%20%40job_name%3D'SQLISV_BackupAllDatabases'%2C%20%3CBR%20%2F%3E%20%40Step_Name%3D'SQLISV_BackupAllDatabases_1'%2C%20%3CBR%20%2F%3E%20%40Command%3D'EXEC%20%5BSQLTools%5D.%5Bdbo%5D.%5BSQLISV_BackupAllDatabases%5D'%20%3CBR%20%2F%3E%20IF%20Exists(Select%201%20from%20msdb.dbo.sysschedules%20%3CBR%20%2F%3E%20WHERE%20name%3D'SQLISV_BackupAllDatabases')%20%3CBR%20%2F%3E%20EXEC%20msdb.dbo.sp_delete_schedule%20%40schedule_name%20%3D%20'SQLISV_BackupAllDatabases'%20%3CBR%20%2F%3E%20Exec%20msdb.dbo.sp_add_schedule%20%40schedule_name%20%3D%20'SQLISV_BackupAllDatabases'%2C%20%3CBR%20%2F%3E%20%40freq_type%20%3D%204%2C%20--Daily%20%3CBR%20%2F%3E%20%40freq_interval%3D1%2C%20%3CBR%20%2F%3E%20%40freq_subday_type%3D0x8%2C%20--%20Hour%20%3CBR%20%2F%3E%20%40freq_subday_interval%3D1%20%3CBR%20%2F%3E%20EXEC%20msdb.dbo.sp_attach_schedule%20%3CBR%20%2F%3E%20%40job_name%20%3D%20N'SQLISV_BackupAllDatabases'%2C%20%3CBR%20%2F%3E%20%40schedule_name%20%3D%20N'SQLISV_BackupAllDatabases'%20%3B%20%3CBR%20%2F%3E%3CDIV%3EEND%3C%2FDIV%3E%0A%20%20%20%20%3CDIV%3EGO%3C%2FDIV%3E%0A%20%20%20%20%3CDIV%3EEXEC%20%5Bdbo%5D.%5BSQLISV_ScheduleBackupAllDatabases%5D%20%3CBR%20%2F%3E%3C%2FDIV%3E%0A%20%20%20%3C%2FDIV%3E%3CBR%20%2F%3E%3C%2FDIV%3E%3CBR%20%2F%3E%3CH2%20id%3D%22toc-hId-62002496%22%20id%3D%22toc-hId-87798689%22%3EDid%20it%20work%3F%3C%2FH2%3E%3CBR%20%2F%3E%3CP%3EIn%20the%20SQLISV_ScheduleBackupAllDatabases%20code%20above%2C%20I%20specified%26nbsp%3B%26nbsp%3B%40notify_level_eventlog%3D3.%20This%20setting%20means%20that%20successful%20and%20unsuccessful%20backups%20are%20recorded%20in%20the%20Event%20Log%20as%20shown%20below.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F97463iD0C41D255EE4C9AF%22%20%2F%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ESome%20of%20the%20backups%20failed%2C%20so%20I%20needed%20to%20investigate%20why.%20I%20can%20manually%20execute%20the%20backup%20and%20attempt%20to%20identify%20what%20the%20problem%20is.%20An%20example%20of%20this%20process%20is%20shown%20below.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F97464i61E547D9B33627FA%22%20%2F%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EThe%20code%20above%20tests%20for%20this%20condition%20and%20omits%20the%20log%20backups%20for%20databases%20with%20a%26nbsp%3BSIMPLE%20recovery%20model.%20This%20was%20a%20contrived%20failure.%20If%20you%20encounter%20any%20failures%2C%20please%20comment%20below.%3C%2FP%3E%3CBR%20%2F%3E%3CH2%20id%3D%22toc-hId-1804812831%22%20id%3D%22toc-hId-1830609024%22%3ETechnical%20Issues%3C%2FH2%3E%3CBR%20%2F%3E%3CP%3EThe%20following%20are%20some%20technical%20points%20that%20may%20be%20of%20some%20interest.%3C%2FP%3E%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3EThe%20code%20includes%20the%20Server%20Instance%20name%20in%20the%20backup%20name.%20This%20is%20needed%20to%20prevent%20namespace%20collision%20if%20there%20are%20two%20instances%20on%20the%20same%20machine%2C%20or%20if%26nbsp%3Bone%20backup%20location%20is%20used%20by%20several%20servers.%20Each%20instance%20would%20have%26nbsp%3Bthe%20following%20standard%20databases%3A%26nbsp%3B%5Bmsdb%5D%2C%20%5Bmodel%5D%20and%20%5Bmaster%5D%20%3CBR%20%2F%3E%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F97465i0BFA3F8AEA4B97A9%22%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3EI%20have%20kept%20the%20use%20of%20backup%20options%20lean%20so%20we%20can%20focus%20on%20the%20process.%20You%20may%20wish%20to%20review%20these%20options%20in%20%3CA%20href%3D%22http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fms186865.aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%20help%20%3C%2FA%3E%20and%20modify%20the%20scripts%20to%20your%20needs.%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%3CH2%20id%3D%22toc-hId--747344130%22%20id%3D%22toc-hId--721547937%22%3EBottom%20Line%3C%2FH2%3E%3CBR%20%2F%3E%3CP%3EMy%20experience%20is%20that%20getting%20backups%20configured%26nbsp%3Bis%20tedious%20work%2C%20prone%20to%20errors.%20This%20post%20shows%20you%20how%20you%20can%20automate%20the%20entire%20process%20by%20just%20%3CSTRONG%3E%20downloading%20the%20TSQL%20script%20%3CA%20href%3D%22https%3A%2F%2Fmsdnshared.blob.core.windows.net%2Fmedia%2FTNBlogsFS%2Fprod.evol.blogs.technet.com%2FCommunityServer.Blogs.Components.WeblogFiles%2F00%2F00%2F00%2F85%2F48%2F5037.BackupScripts.sql.txt%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3E%20here%20%3C%2FA%3E%20%3C%2FSTRONG%3E%20and%20executing%20it%20once%20on%20each%20server.%20The%20backup%20pattern%20may%20not%20be%20perfect%2C%20but%20it%20is%20better%20than%20what%20I%20have%20often%20seen.%26nbsp%3B%20The%20scripts%20above%20should%20be%20robust%2C%20and%20best%20of%20all%2C%20if%20you%20add%20a%20database%20to%20SQL%20Server%20it%20will%20be%20included%20in%20the%20backups%20automatically.%3C%2FP%3E%3CBR%20%2F%3E%3CH2%20id%3D%22toc-hId-995466205%22%20id%3D%22toc-hId-1021262398%22%3E%3CA%3ESQLTools%3A%20%3C%2FA%3E%20Where%20to%20Save%20these%20Utilities%3F%3C%2FH2%3E%3CBR%20%2F%3E%3CP%3EI%20had%20a%20discussion%20with%20Shaun%20Tinline-Jones%20of%20the%20%3CA%20href%3D%22http%3A%2F%2Fsqlcat.com%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3E%20SQL%20Server%20Customer%20Advisory%20Team%20-%20SQL%20Server%20Best%20Practices%20%3C%2FA%3E%20team(SQLCAT)%20about%20where%20to%20locate%20this%20procedure%20and%20other%20like%20it.%20Should%20it%20be%20in%20%5Bmsdb%5D%2C%20%5Bmodel%5D%2C%5Bmaster%5D%2C%20an%20existing%26nbsp%3BISV%20related%26nbsp%3Bdatabase%20or%20a%20new%20database.%20Our%20conclusion%20was%20a%20new%20database%20named%20%5BSQLTools%5D%20was%20best%20(instead%20of%20%5BSQLISV%5D%20or%20%5BSQLCAT%5D).%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ETo%20create%20this%20database%3C%2FP%3E%3CBR%20%2F%3E%3CDIV%20id%3D%22_mcePaste%22%3E%3F%3C%2FDIV%3E%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%3CDIV%3ECreate%20Database%20%5BSQLTools%5D%20%3CBR%20%2F%3E%20Go%3C%2FDIV%3E%0A%20%20%3CDIV%3E%0A%20%20%3C%2FDIV%3E%0A%20%0A%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-383871%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20TECHNET%20on%20May%2013%2C%202011%20%5BPrior%20Post%5D%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%5BNext%20Post%5DGetting%20backups%20scheduled%20appropriately%20tends%20to%20be%20a%20slow%20process%20with%20a%20lot%20of%20keying%20and%20mouse%20clicks.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-383871%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESQLServerISV%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Version history
Last update:
‎Mar 23 2019 12:11 PM
Updated by: