To create a database backup
The INIT
clause is not required if the FORMAT clause is specified.
Important Use extreme caution when using the FORMAT
or INIT clauses of the BACKUP statement, as this will destroy any backups
previously stored on the backup media.
This
example backs up the entire MyNwind database to tape:
USE MyNwind
GO
BACKUP DATABASE MyNwind
TO TAPE = '\\.\Tape0'
WITH FORMAT,
NAME = 'Full Backup of MyNwind'
GO
To restore a database backup
Important The
system administrator restoring the database backup must be the only person
currently using the database to be restored.
This example restores the MyNwind database
backup from tape:
USE master
GO
RESTORE DATABASE MyNwind
FROM TAPE = '\\.\Tape0'
GO
To restart an interrupted backup operation
This example restarts an interrupted database backup
operation:
-- Create a database backup of the MyNwind database
BACKUP DATABASE MyNwind
TO MyNwind_1
-- The backup operation halts due to power outage.
-- Repeat the original BACKUP statement specifying WITH RESTART
BACKUP DATABASE MyNwind
TO MyNwind_1
WITH RESTART
To restart an interrupted restore operation
Important The
system administrator restoring the backup must be the only person currently
using the database to be restored.
This example restarts an interrupted restore
operation:
-- Restore a database backup of the MyNwind database
RESTORE DATABASE MyNwind
FROM MyNwind_1
GO
-- The restore operation halted prematurely.
-- Repeat the original RESTORE statement specifying WITH RESTART
RESTORE DATABASE MyNwind
FROM MyNwind_1
WITH RESTART
GO
To create a transaction log backup
The INIT clause is not required if the FORMAT clause
is specified.
Important Use
extreme caution when using the FORMAT or INIT clauses of the BACKUP statement
as this will destroy any backups previously stored on the backup media.
This example creates a transaction log backup for the
MyNwind database to the previously created named backup device, MyNwind_log1:
BACKUP LOG MyNwind
TO MyNwind_log1
GO
To create a backup of the currently active
transaction log
This clause allows the active part of the transaction
log to be backed up even if the database is inaccessible, provided that the
transaction log file(s) is accessible and undamaged.
The INIT clause is not required if the FORMAT clause
is specified.
Important Use
extreme caution when using the FORMAT or INIT clauses of the BACKUP statement
as this will destroy any backups previously stored on the backup media.
This example backs up the currently active
transaction log for the MyNwind database even though MyNwind has
been damaged and is inaccessible. The transaction log, however, is undamaged
and accessible:
BACKUP LOG MyNwind
TO MyNwind_log1
WITH NO_TRUNCATE
GO
It is not possible to apply a transaction log backup:
To apply a transaction log backup
This example applies a transaction log backup to the MyNwind
database.
RESTORE LOG MyNwind
FROM MyNwind_log1
WITH RECOVERY
GO
This example applies multiple transaction log backups
to the MyNwind database.
RESTORE LOG MyNwind
FROM MyNwind_log1
WITH NORECOVERY
GO
RESTORE LOG MyNwind
FROM MyNwind_log2
WITH NORECOVERY
GO
RESTORE LOG MyNwind
FROM MyNwind_log3
WITH RECOVERY
GO
To create a differential database backup
Important It
is not possible to create a differential database backup unless the database
has been backed up first.
The INIT clause is not required if the FORMAT clause
is specified.
Important Use
extreme caution when using the FORMAT or INIT clauses of the BACKUP statement
as this will destroy any backups previously stored on the backup media.
This example creates a full and a differential
database backup for the MyNwind database.
-- Create a full database backup first.
BACKUP DATABASE MyNwind
TO MyNwind_1
WITH INIT
GO
-- Time elapses.
-- Create a differential database backup, appending the backup
-- to the backup device containing the database backup.
BACKUP DATABASE MyNwind
TO MyNwind_1
WITH DIFFERENTIAL
GO
To restore a differential database backup
This example restores a database and differential
database backup of the MyNwind database.
-- Assume the database is lost at this point. Now restore the full
-- database. Specify the original full backup and NORECOVERY.
-- NORECOVERY allows subsequent restore operations to proceed.
RESTORE DATABASE MyNwind
FROM MyNwind_1
WITH NORECOVERY
GO
-- Now restore the differential database backup, the second backup on
-- the MyNwind_1 backup device.
RESTORE DATABASE MyNwind
FROM MyNwind_1
WITH FILE = 2,
RECOVERY
GO
This example restores a database, differential
database, and transaction log backup of the MyNwind database.
-- Assume the database is lost at this point. Now restore the full
-- database. Specify the original full backup and NORECOVERY.
-- NORECOVERY allows subsequent restore operations to proceed.
RESTORE DATABASE MyNwind
FROM MyNwind_1
WITH NORECOVERY
GO
-- Now restore the differential database backup, the second backup on
-- the MyNwind_1 backup device.
RESTORE DATABASE MyNwind
FROM MyNwind_1
WITH FILE = 2,
NORECOVERY
GO
-- Now restore each transaction log backup created after
-- the differential database backup.
RESTORE LOG MyNwind
FROM MyNwind_log1
WITH NORECOVERY
GO
RESTORE LOG MyNwind
FROM MyNwind_log2
WITH RECOVERY
GO
To recover a database without restoring
This example recovers the MyNwind database
without restoring from a backup.
-- Restore database using WITH RECOVERY.
RESTORE DATABASE MyNwind
WITH RECOVERY
To restore to the point of failure
This example backs up the currently active
transaction log of the MyNwind database, even though MyNwind is
inaccessible, and then restores the database to the point of failure using
previously created backups:
-- Back up the currently active transaction log.
BACKUP LOG MyNwind
TO MyNwind_log2
WITH NO_TRUNCATE
GO
-- Restore the database backup.
RESTORE DATABASE MyNwind
FROM MyNwind_1
WITH NORECOVERY
GO
-- Restore the first transaction log backup.
RESTORE LOG MyNwind
FROM MyNwind_log1
WITH NORECOVERY
GO
-- Restore the final transaction log backup.
RESTORE LOG MyNwind
FROM MyNwind_log2
WITH RECOVERY
GO
Setting up a standby server generally involves
creating a database backup and periodic transaction log backups at the primary
server, and then applying those backups, in sequence, to the standby server.
The standby server is left in a read-only state between restores. When the
standby server needs to be made available for use, any outstanding transaction
log backups, including the backup of the active transaction log, from the
primary server, are applied to the standby server and the database is
recovered.
To create backups on the primary server
To set up and maintain the standby server
To bring the standby server online (primary server
failed)
This example sets up the MyNwind database on a
standby server. The database can be used in read-only mode between
restore operations.
-- Restore the initial database backup on the standby server.
USE master
GO
RESTORE DATABASE MyNwind
FROM MyNwind_1
WITH STANDBY = 'c:\undo.ldf'
GO
-- Apply the first transaction log backup.
RESTORE LOG MyNwind
FROM MyNwind_log1
WITH STANDBY = 'c:\undo.ldf'
GO
-- Apply the next transaction log backup.
RESTORE LOG MyNwind
FROM MyNwind_log2
WITH STANDBY = 'c:\undo.ldf'
GO
-- Repeat for each transaction log backup created on the
-- primary server.
--
-- Time elapses.. .. ..
--
-- The primary server has failed. Back up the
-- active transaction log on the primary server.
BACKUP LOG MyNwind
TO MyNwind_log3
WITH NO_TRUNCATE
GO
-- Apply the final (active) transaction log backup
-- to the standby server. All preceding transaction
-- log backups must have been already applied.
RESTORE LOG MyNwind
FROM MyNwind_log3
WITH STANDBY = 'c:\undo.ldf'
GO
-- Recover the database on the standby server,
-- making it available for normal operations.
RESTORE DATABASE MyNwind
WITH RECOVERY
GO
To restore to a point in time
This example restores a database to its state as of
10:00 A.M. on July 1, 1998, and illustrates a restore operation involving
multiple logs and multiple backup devices.
-- Restore the database backup.
RESTORE DATABASE MyNwind
FROM MyNwind_1, MyNwind_2
WITH NORECOVERY
GO
RESTORE LOG MyNwind
FROM MyNwind_log1
WITH RECOVERY, STOPAT = 'Jul 1, 1998 10:00 AM'
GO
RESTORE LOG MyNwind
FROM MyNwind_log2
WITH RECOVERY, STOPAT = 'Jul 1, 1998 10:00 AM'
GO
To restore the master database
This example restores the master database
backup from tape without using a permanent (named) backup device.
USE master
GO
RESTORE DATABASE master
FROM TAPE = '\\.\Tape0'
GO
To back up files and filegroups
This example performs a backup operation with files
and filegroups for the MyNwind database.
-- Back up the MyNwind file(s) and filegroup(s)
BACKUP DATABASE MyNwind
FILE = 'MyNwind_data_1',
FILEGROUP = 'new_customers',
FILE = 'MyNwind_data_2',
FILEGROUP = 'first_qtr_sales'
TO MyNwind_1
GO
To restore files and filegroups
Important The
system administrator restoring the files and filegroups must be the only person
currently using the database to be restored.
The transaction log backups, if applied, must cover
the time when the files and filegroups were backed up until the end of log
(unless ALL database files are restored).
This example restores the files and filegroups for
the MyNwind database. Two transaction logs will also be applied, to
restore the database to the current time.
USE master
GO
-- Restore the files and filesgroups for MyNwind.
RESTORE DATABASE MyNwind
FILE = 'MyNwind_data_1',
FILEGROUP = 'new_customers',
FILE = 'MyNwind_data_2',
FILEGROUP = 'first_qtr_sales'
FROM MyNwind_1
WITH NORECOVERY
GO
-- Apply the first transaction log backup.
RESTORE LOG MyNwind
FROM MyNwind_log1
WITH NORECOVERY
GO
-- Apply the last transaction log backup.
RESTORE LOG MyNwind
FROM MyNwind_log2
WITH RECOVERY
GO
To restore files and filegroups over existing files
Important The
system administrator restoring the files and filegroups must be the only person
currently using the database to be restored.
The transaction log backups, if applied, must cover
the time when the files and filegroups were backed up.
This example restores the files and filegroups for
the MyNwind database, and replaces any existing files of the same name.
Two transaction logs will also be applied to restore the database to the
current time.
USE master
GO
-- Restore the files and filesgroups for MyNwind.
RESTORE DATABASE MyNwind
FILE = 'MyNwind_data_1',
FILEGROUP = 'new_customers',
FILE = 'MyNwind_data_2',
FILEGROUP = 'first_qtr_sales'
FROM MyNwind_1
WITH NORECOVERY,
REPLACE
GO
-- Apply the first transaction log backup.
RESTORE LOG MyNwind
FROM MyNwind_log1
WITH NORECOVERY
GO
-- Apply the last transaction log backup.
RESTORE LOG MyNwind
FROM MyNwind_log2
WITH RECOVERY
GO
To restore files to a new location
Important The
system administrator restoring the files must be the only person currently
using the database to be restored.
The transaction log backups, if applied, must cover
the time when the files and filegroups were backed up.
This example restores two of the files for the MyNwind
database that were originally located on the C:\ drive to new locations on the
D: \drive. Two transaction logs will also be applied to restore the database to
the current time. The RESTORE FILELISTONLY statement is used to determine the
number and logical and physical names of the files in the database being
restored.
USE master
GO
-- First determine the number and names of the files in the backup.
RESTORE FILELISTONLY
FROM MyNwind_1
-- Restore the files for MyNwind.
RESTORE DATABASE MyNwind
FROM MyNwind_1
WITH NORECOVERY,
MOVE 'MyNwind_data_1' TO 'D:\MyData\MyNwind_data_1.mdf',
MOVE 'MyNwind_data_2' TO 'D:\MyData\MyNwind_data_2.ndf'
GO
-- Apply the first transaction log backup.
RESTORE LOG MyNwind
FROM MyNwind_log1
WITH NORECOVERY
GO
-- Apply the last transaction log backup.
RESTORE LOG MyNwind
FROM MyNwind_log2
WITH RECOVERY
GO
To restore a database with a new name
The transaction log backups, if applied, must cover
the time when the files were backed up.
This example creates a new database called MyNwind2_Test.
MyNwind2_Test is a copy of the existing MyNwind2 database that
comprises two files: MyNwind2_data and MyNwind2_log. Because the MyNwind2
database already exists, the files in the backup need to be moved during the
restore operation. The RESTORE FILELISTONLY statement is used to determine the
number and names of the files in the database being restored.
USE master
GO
-- First determine the number and names of the files in the backup.
-- MyNwind_2 is the name of the backup device.
RESTORE FILELISTONLY
FROM MyNwind_2
-- Restore the files for MyNwind2_Test.
RESTORE DATABASE MyNwind2_Test
FROM MyNwind_2
WITH RECOVERY,
MOVE 'MyNwind2_data' TO 'D:\MyData\MyNwind2_Test_data.mdf',
MOVE 'MyNwind2_log' TO 'D:\MyData\MyNwind2_Test_log.ldf'
GO