SQL Server Backup Commands


SQL Server Backup Commands offers many options for creating backups. We will talk about each of these backup options and how to perform these backups using SSMS and T-SQL.

The different types of backups that you can create are as follows:

SQL Server Backup Commands
SQL Server BACKUP DATABASE commands
There are only two commands for backup, the primary is BACKUP DATABASE. This allows you to do a complete backup of your database as well as differential, file, etc. backups depending on the options that you use.

The BACKUP DATABASE command gives you many options for creating backups. Following are different examples.

Create a full backup to disk
The command is BACKUP DATABASE databaseName. The “TO DISK” option specifies that the backup should be written to disk and the location and filename to create the backup is specified.

BACKUP DATABASE AdventureWorks </span>
<span style="color: #3366ff;"> TO DISK = 'C:\AdventureWorks.BAK'</span>
<span style="color: #3366ff;"> GO

Create a differential backup
This command adds the “WITH DIFFERENTIAL” option.

BACKUP DATABASE AdventureWorks </span>
<span style="color: #3366ff;">TO DISK = 'C:\AdventureWorks.BAK' </span>
<span style="color: #3366ff;">WITH DIFFERENTIAL </span>
<span style="color: #3366ff;">GO

Create a file level backup
This command uses the “WITH FILE” option to specify a file backup. You need to specify the logical filename within the database which can be obtained by using the command sp_helpdb ‘databaseName’, specifying the name of your database.

BACKUP DATABASE TestBackup FILE = 'TestBackup' </span>
<span style="color: #3366ff;">TO DISK = 'C:\TestBackup_TestBackup.FIL'</span>
<span style="color: #3366ff;">GO

Create a filegroup backup
This command uses the “WITH FILEGROUP” option to specify a filegroup backup. You need to specify the filegroup name from the database which can be obtained by using the command sp_helpdb ‘databaseName’, specifying the name of your database.

BACKUP DATABASE TestBackup FILEGROUP = 'ReadOnly'
<span style="color: #3366ff;">TO DISK = 'C:\TestBackup_ReadOnly.FLG'</span>
<span style="color: #3366ff;">GO

Create a full backup to multiple disk files
This command uses the “DISK” option multiple times to write the backup to three equally sized smaller files instead of one large file.

BACKUP DATABASE AdventureWorks </span>
<span style="color: #3366ff;">TO DISK = 'C:\AdventureWorks_1.BAK',</span>
<span style="color: #3366ff;">DISK = 'D:\AdventureWorks_2.BAK',</span>
<span style="color: #3366ff;">DISK = 'E:\AdventureWorks_3.BAK'</span>
<span style="color: #3366ff;">GO

Create a full backup with a password
This command creates a backup with a password that will need to be supplied when restoring the database.

BACKUP DATABASE AdventureWorks
<span style="color: #3366ff;">TO DISK = 'C:\AdventureWorks.BAK'</span>
<span style="color: #3366ff;">WITH PASSWORD = 'Q!W@E#R$'</span>
<span style="color: #3366ff;">GO

Create a full backup with progress stats
This command creates a full backup and also displays the progress of the backup. The default is to show progress after every 10%.

BACKUP DATABASE AdventureWorks </span>
<span style="color: #3366ff;">TO DISK = 'C:\AdventureWorks.BAK'</span>
<span style="color: #3366ff;">WITH STATS</span>
<span style="color: #3366ff;">GO

Here is another option showing stats after every 1%.

BACKUP DATABASE AdventureWorks </span>
<span style="color: #3366ff;">TO DISK = 'C:\AdventureWorks.BAK'</span>
<span style="color: #3366ff;">WITH STATS = 1</span>
<span style="color: #3366ff;">GO

Create a backup and give it a description
This command uses the description option to give the backup a name. This can later be used with some of the restore commands to see what is contained with the backup. The maximum size is 255 characters.

BACKUP DATABASE AdventureWorks </span>
<span style="color: #3366ff;">TO DISK = 'C:\AdventureWorks.BAK'</span>
<span style="color: #3366ff;">WITH DESCRIPTION = 'Full backup for AdventureWorks'</span>
<span style="color: #3366ff;">GO

Create a mirrored backup
This option allows you to create multiple copies of the backups, preferably to different locations.

BACKUP DATABASE AdventureWorks </span>
<span style="color: #3366ff;">TO DISK = 'C:\AdventureWorks.BAK'</span>
<span style="color: #3366ff;">MIRROR TO DISK =  'D:\AdventureWorks_mirror.BAK'</span>
<span style="color: #3366ff;">WITH FORMAT</span>
<span style="color: #3366ff;">GO

Specifying multiple options
This next example shows how you can use multiple options at the same time.

BACKUP DATABASE AdventureWorks </span>
<span style="color: #3366ff;">TO DISK = 'C:\AdventureWorks.BAK'</span>
<span style="color: #3366ff;">MIRROR TO DISK =  'D:\AdventureWorks_mirror.BAK'</span>
<span style="color: #3366ff;">WITH FORMAT, STATS, PASSWORD = 'Q!W@E#R$'</span>
<span style="color: #3366ff;">GO

SQL Server BACKUP LOG command

There are only two commands for backup, the primary is BACKUP DATABASE which backs up the entire database and BACKUP LOG which backs up the transaction log. The following will show different options for doing transaction log backups.

The BACKUP LOG command gives you many options for creating transaction log backups. Following are different examples.

Create a simple transaction log backup to disk
The command is BACKUP LOG databaseName. The “TO DISK” option specifies that the backup should be written to disk and the location and filename to create the backup is specified. The file extension is “TRN”. This helps me know it is a transaction log backup, but it could be any extension you like. Also, the database has to be in the FULL or Bulk-Logged recovery model and at least one Full backup has to have occurred.

BACKUP LOG AdventureWorks </span>
<span style="color: #3366ff;">TO DISK = 'C:\AdventureWorks.TRN'</span>
<span style="color: #3366ff;">GO

Create a log backup with a password
This command creates a log backup with a password that will need to be supplied when restoring the database.

BACKUP LOG AdventureWorks </span>
<span style="color: #3366ff;">TO DISK = 'C:\AdventureWorks.TRN'</span>
<span style="color: #3366ff;">WITH PASSWORD = 'Q!W@E#R$'</span>
<span style="color: #3366ff;">GO

Create a log backup with progress stats
This command creates a log backup and also displays the progress of the backup. The default is to show progress after every 10%.

BACKUP LOG AdventureWorks </span>
<span style="color: #3366ff;">TO DISK = 'C:\AdventureWorks.TRN'</span>
<span style="color: #3366ff;">WITH STATS</span>
<span style="color: #3366ff;">GO

Here is another option showing stats after every 1%.

BACKUP LOG AdventureWorks </span>
<span style="color: #3366ff;">TO DISK = 'C:\AdventureWorks.TRN'</span>
<span style="color: #3366ff;">WITH STATS = 1</span>
<span style="color: #3366ff;">GO

Create a backup and give it a description
This command uses the description option to give the backup a name. This can later be used with some of the restore commands to see what is contained with the backup. The maximum size is 255 characters.

BACKUP LOG AdventureWorks </span>
<span style="color: #3366ff;">TO DISK = 'C:\AdventureWorks.TRN'</span>
<span style="color: #3366ff;">WITH DESCRIPTION = 'Log backup for AdventureWorks'</span>
<span style="color: #3366ff;">GO

Create a mirrored backup
This option allows you to create multiple copies of the backups, preferably to different locations.

BACKUP LOG AdventureWorks </span>
<span style="color: #3366ff;">TO DISK = 'C:\AdventureWorks.TRN'</span>
<span style="color: #3366ff;">MIRROR TO DISK =  'D:\AdventureWorks_mirror.TRN'</span>
<span style="color: #3366ff;">WITH FORMAT</span>
<span style="color: #3366ff;">GO

Specifying multiple options
This example shows how you can use multiple options at the same time.

BACKUP LOG AdventureWorks </span>
<span style="color: #3366ff;">TO DISK = 'C:\AdventureWorks.TRN'</span>
<span style="color: #3366ff;">MIRROR TO DISK =  'D:\AdventureWorks_mirror.TRN'</span>
<span style="color: #3366ff;">WITH FORMAT, STATS, PASSWORD = 'Q!W@E#R$'</span>
<span style="color: #3366ff;">GO

<<Click here to see all posts>>

Leave a Reply

Your email address will not be published. Required fields are marked *

Prove You Are Human Time limit is exhausted. Please reload CAPTCHA.