SQL Server Maintenance Solution

SQL Server Backup

DatabaseBackup is the SQL Server Maintenance Solution’s stored procedure for backing up databases. DatabaseBackup is supported on SQL Server 2005, SQL Server 2008, SQL Server 2008 R2, SQL Server 2012, and SQL Server 2014.

Download

Download MaintenanceSolution.sql. This script creates all the objects and jobs that you need. You can also download the objects as separate scripts.

License

The SQL Server Maintenance Solution is free.

Parameters

Databases

Select databases. The keywords SYSTEM_DATABASES, USER_DATABASES, and ALL_DATABASES are supported. The hyphen character (-) is used to exclude databases, and the percent character (%) is used for wildcard selection. All of these operations can be combined by using the comma (,).

Value Description
SYSTEM_DATABASES All system databases (master, msdb, and model)
USER_DATABASES All user databases
ALL_DATABASES All databases
Db1 The database Db1
Db1, Db2 The databases Db1 and Db2
USER_DATABASES, -Db1 All user databases, except Db1
%Db% All databases that have “Db” in the name
%Db%, -Db1 All databases that have “Db” in the name, except Db1
ALL_DATABASES, -%Db% All databases that do not have “Db” in the name

Directory

Specify backup root directories, which can be local directories or network shares. If you specify multiple directories, then the backup files are striped evenly across the directories. Specify multiple directories by using the comma (,). If no directory is specified, then the SQL Server default backup directory is used.

Value Description
NULL Back up to the SQL Server default backup directory. This is the default.
C:\Backup Back up to the directory C:\Backup.
C:\Backup, D:\Backup Back up to the directories C:\Backup and D:\Backup.
\\Server1\Backup Back up to the network share \\Server1\Backup.
\\Server1\Backup, \\Server2\Backup Back up to the network shares \\Server1\Backup and \\Server2\Backup.

DatabaseBackup creates a directory structure with server name, instance name, database name, and backup type under the backup root directory. If the database is a part of an availability group, then cluster name and availability group name are used instead of server name and instance name.

BackupType

Specify the type of backup: full, differential, or transaction log.

Value Description
FULL Full backup
DIFF Differential backup
LOG Transaction log backup

DatabaseBackup uses the SQL Server BACKUP command: BACKUP DATABASE for the full backup, BACKUP DATABASE WITH DIFFERENTIAL for the differential backup, and BACKUP LOG for the transaction log backup.

Verify

Verify the backup.

Value Description
Y Verify the backup.
N Do not verify the backup. This is the default.

The Verify option in DatabaseBackup uses the SQL Server RESTORE VERIFYONLY command.

CleanupTime

Specify the time, in hours, after which the backup files are deleted. If no time is specified, then no backup files are deleted.

Backup files are deleted after each database is backed up and verified. Backup files are deleted only if the backup and verification of the database were successful.

DatabaseBackup has a check to verify that transaction log backups that are newer than the most recent full or differential backup are not deleted. This is to guarantee that you can always perform a point-in-time restore.

Compress

Compress the backup. If no value is specified, then the backup compression default in sys.configurations is used.

Value Description
NULL Use the backup compression default in sys.configurations. This is the default.
Y Compress the backup.
N Do not compress the backup.

The Compress option in DatabaseBackup uses the COMPRESSION and NO_COMPRESSION options in the SQL Server BACKUP command.

CopyOnly

Perform a copy-only backup.

Value Description
Y Perform a copy-only backup.
N Perform a normal backup. This is the default.

The CopyOnly option in DatabaseBackup uses the COPY_ONLY option in the SQL Server BACKUP command.

ChangeBackupType

Change the backup type if a differential or transaction-log backup cannot be performed.

Value Description
Y Change the backup type if a backup cannot be performed.
N Skip the backup if a backup cannot be performed. This is the default.

DatabaseBackup checks differential_base_lsn in sys.master_files to determine whether a differential backup can be performed. If a differential backup is not possible, then the database is skipped by default. Alternatively, you can set ChangeBackupType to Y to have a full backup performed instead.

DatabaseBackup checks last_log_backup_lsn in sys.database_recovery_status to determine whether a transaction log backup in full or bulk-logged recovery model can be performed. If a transaction log backup is not possible, then the database is skipped by default. Alternatively, you can set ChangeBackupType to Y to have a differential or full backup performed instead.

BackupSoftware

Specify third-party backup software; otherwise, SQL Server native backup is performed.

Value Description
NULL SQL Server native backup (the default)
LITESPEED LiteSpeed for SQL Server
SQLBACKUP Red Gate SQL Backup
SQLSAFE Idera SQL safe backup

CheckSum

Enable backup checksums.

Value Description
Y Enable backup checksums.
N Do not enable backup checksums. This is the default.

The CheckSum option in DatabaseBackup uses the CHECKSUM option in the SQL Server BACKUP command.

BlockSize

Specify the physical blocksize in bytes.

The BlockSize option in DatabaseBackup uses the BLOCKSIZE option in the SQL Server BACKUP command.

BufferCount

Specify the number of I/O buffers to be used for the backup operation.

The BufferCount option in DatabaseBackup uses the BUFFERCOUNT option in the SQL Server BACKUP command.

MaxTransferSize

Specify the largest unit of transfer, in bytes, to be used between SQL Server and the backup media.

The MaxTransferSize option in DatabaseBackup uses the MAXTRANSFERSIZE option in the SQL Server BACKUP command.

NumberOfFiles

Specify the number of backup files. The default is the number of backup directories and the maximum is 64 files.

CompressionLevel

Set the LiteSpeed for SQL Server, Red Gate SQL Backup, or Idera SQL safe backup compression level.

In LiteSpeed for SQL Server, the compression levels 0 to 8 are supported. In Red Gate SQL Backup, levels 0 to 4 are supported, and in Idera SQL safe backup, levels 1 to 4 are supported.

Description

Enter a description for the backup.

The Description option in DatabaseBackup uses the DESCRIPTION option in the SQL Server BACKUP command.

Threads

Specify the LiteSpeed for SQL Server, Red Gate SQL Backup, or Idera SQL safe backup number of threads. The maximum number of threads is 32.

Throttle

Specify the LiteSpeed for SQL Server maximum CPU usage, as a percentage.

Encrypt

Encrypt the backup.

Value Description
Y Encrypt the backup.
N Do not encrypt the backup. This is the default.

The Encrypt option in DatabaseBackup uses the ENCRYPTION option in the SQL Server BACKUP command.

EncryptionAlgorithm

Specify the type of encryption.

Value Description
NULL No encryption (the default)
RC2_40 RC2 40-bit encryption (LiteSpeed for SQL Server)
RC2_56 RC2 56-bit encryption (LiteSpeed for SQL Server)
RC2_112 RC2 112-bit encryption (LiteSpeed for SQL Server)
RC2_128 RC2 128-bit encryption (LiteSpeed for SQL Server)
TRIPLE_DES_3KEY Triple DES encryption (SQL Server 2014 or LiteSpeed for SQL Server)
RC4_128 RC4 128-bit encryption (LiteSpeed for SQL Server)
AES_128 AES 128-bit encryption (SQL Server 2014, LiteSpeed for SQL Server, Red Gate SQL Backup, or Idera SQL safe backup)
AES_192 AES 192-bit encryption (SQL Server 2014 or LiteSpeed for SQL Server)
AES_256 AES 256-bit encryption (SQL Server 2014, LiteSpeed for SQL Server, Red Gate SQL Backup, or Idera SQL safe backup)

The EncryptionAlgorithm option in DatabaseBackup uses the ENCRYPTION and ALGORITHM options in the SQL Server BACKUP command.

ServerCertificate

Server certificate that is used to encrypt the backup.

The ServerCertificate option in DatabaseBackup uses the ENCRYPTION and SERVER CERTIFICATE options in the SQL Server BACKUP command.

ServerAsymmetricKey

Asymmetric key that is used to encrypt the backup.

The ServerAsymmetricKey option in DatabaseBackup uses the ENCRYPTION and SERVER ASYMMETRIC KEY options in the SQL Server BACKUP command.

EncryptionKey

Key that is used to encrypt the backup. This is used with LiteSpeed for SQL Server, Red Gate SQL Backup, and Idera SQL safe backup.

ReadWriteFileGroups

Perform a backup of the primary filegroup and any read/write filegroups.

Value Description
Y Perform a backup of the primary filegroup and any read/write filegroups.
N Perform a normal backup. This is the default.

The ReadWriteFileGroups option in DatabaseBackup uses the READ_WRITE_FILEGROUPS option in the SQL Server BACKUP command.

OverrideBackupPreference

Override the backup preference for availability groups. This option only applies to copy-only full backups and regular transaction log backups.

Value Description
Y Override the backup preference for availability groups.
N Do not override the backup preference for availability groups. This is the default.

LogToTable

Log commands to the table dbo.CommandLog.

Value Description
Y Log commands to the table.
N Do not log commands to the table. This is the default.

Execute

Execute commands. By default, the commands are executed normally. If this parameter is set to N, then the commands are printed only.

Value Description
Y Execute commands. This is the default.
N Only print commands.

Examples

A. Back up all user databases, using checksums and compression; verify the backup; and delete old backup files

EXECUTE dbo.DatabaseBackup
@Databases = 'USER_DATABASES',
@Directory = 'C:\Backup',
@BackupType = 'FULL',
@Verify = 'Y',
@Compress = 'Y',
@CheckSum = 'Y',
@CleanupTime = 24

B. Back up all user databases to a network share, and verify the backup

EXECUTE dbo.DatabaseBackup
@Databases = 'USER_DATABASES',
@Directory = '\\Server1\Backup',
@BackupType = 'FULL',
@Verify = 'Y'

C. Back up all user databases across four network shares, and verify the backup

EXECUTE dbo.DatabaseBackup
@Databases = 'USER_DATABASES',
@Directory = '\\Server1\Backup, \\Server2\Backup, \\Server3\Backup, \\Server4\Backup',
@BackupType = 'FULL',
@Verify = 'Y',
@NumberOfFiles = 4

D.Back up all user databases to 64 files, using checksums and compression and setting the buffer count and the maximum transfer size

EXECUTE dbo.DatabaseBackup
@Databases = 'USER_DATABASES',
@Directory = 'C:\Backup',
@BackupType = 'FULL',
@Compress = 'Y',
@CheckSum = 'Y',
@BufferCount = 50,
@MaxTransferSize = 4194304,
@NumberOfFiles = 64

E. Back up read/write filegroups of all user databases

EXECUTE dbo.DatabaseBackup
@Databases = 'USER_DATABASES',
@Directory = 'C:\Backup',
@BackupType = 'FULL',
@ReadWriteFileGroups = 'Y'

F. Back up the transaction log of all user databases, using the option to change the backup type if a log backup cannot be performed

EXECUTE dbo.DatabaseBackup
@Databases = 'USER_DATABASES',
@Directory = 'C:\Backup',
@BackupType = 'LOG',
@ChangeBackupType = 'Y'

G. Back up all user databases, using compression, encryption, and a server certificate.

EXECUTE dbo.DatabaseBackup @Databases = 'USER_DATABASES',
@Directory = 'C:\Backup',
@BackupType = 'FULL',
@Compress = 'Y',
@Encrypt = 'Y',
@EncryptionAlgorithm = 'AES_256',
@ServerCertificate = 'MyCertificate'

H. Back up all user databases, using compression, encryption, and LiteSpeed for SQL Server, and limiting the CPU usage to 10 percent

EXECUTE dbo.DatabaseBackup
@Databases = 'USER_DATABASES',
@Directory = 'C:\Backup',
@BackupType = 'FULL',
@BackupSoftware = 'LITESPEED',
@Compress = 'Y',
@Encrypt = 'Y',
@EncryptionAlgorithm = 'AES_256',
@EncryptionKey = 'MyPassword',
@Throttle = 10

I. Back up all user databases, using compression, encryption, and Red Gate SQL Backup

EXECUTE dbo.DatabaseBackup
@Databases = 'USER_DATABASES',
@Directory = 'C:\Backup',
@BackupType = 'FULL',
@BackupSoftware = 'SQLBACKUP',
@Compress = 'Y',
@Encrypt = 'Y',
@EncryptionAlgorithm = 'AES_256',
@EncryptionKey = 'MyPassword'

J. Back up all user databases, using compression, encryption, and Idera SQL safe backup

EXECUTE dbo.DatabaseBackup
@Databases = 'USER_DATABASES',
@Directory = 'C:\Backup',
@BackupType = 'FULL',
@BackupSoftware = 'SQLSAFE',
@Compress = 'Y',
@Encrypt = 'Y',
@EncryptionAlgorithm = 'AES_256',
@EncryptionKey = '8tPyzp4i1uF/ydAN1DqevdXDeVoryWRL'

Execution

Execute the stored procedures from SQL Server Agent CmdExec jobs, using sqlcmd and the -b option. Pass the parameters to the stored procedures by name:

sqlcmd -E -S $(ESCAPE_SQUOTE(SRVR)) -d master -Q "EXECUTE dbo.DatabaseBackup @Databases = 'USER_DATABASES', @Directory = 'C:\Backup', @BackupType = 'FULL'" -b

Use SQL Server Agent output files to ensure that you always have a full set of information, in case of an error.

You can use the MaintenanceSolution.sql script to install the SQL Server Maintenance Solution. Then the sqlcmd commands and the output files will be configured for you.