Sie sind auf Seite 1von 23

Backup and Recovery

Topics
• Backup Types
• Backup Compression
• Backups from T-SQL
• Backup History
• Types of Restores
Backup Types
• Full Backup
• Log Backup
• Differential Backup
• Partial Backups
• File Backups
• Copy-Only Backups
Partial Backups
• Partial backups are similar to full backups, except they are designed to back up
only the primary filegroup, any read/write filegroups, and any read-only
filegroups optionally specified. If you create a partial backup of a read-only
database, only the primary filegroup will be included in the backup.
• SQL Server does not support partial backups using the SQL Server Management
Studio GUI. You must use T-SQL to create a partial backup.
--Partial backup
BACKUP DATABASE AdventureWorks2008R2 READ_WRITE_FILEGROUPS
-- [ , <optional_filegroups> ]
TO DISK = 'C:\Backups\AdventureWorks2008R2_part.bak';

--Differential partial backup


BACKUP DATABASE AdventureWorks2008R2 READ_WRITE_FILEGROUPS
-- [ , < optional_filegroups> ]
TO DISK = 'C:\Backups\AdventureWorks2008R2_part_diff.bak'
WITH DIFFERENTIAL;
File Backups
• File backups allow you to create a backup that contains individual files or
filegroups. File backups give you the flexibility to take backups of large
databases based on usage patterns.
• You can create a file backup on a database using the simple recovery
model; however, you must back up all the read/write filegroups at the
same time, so you can restore the database to a consistent point in time.
• The easiest way to back up all the read/write filegroups at the same time is
by using the READ_WRITE_FILEGROUPS option in the BACKUP statement,
which creates a partial backup as discussed in the previous section.
Basically, the simplest way to perform a file backup of a database in Simple
mode is to perform a partial backup, not a file backup, which would require
you to list each individual filegroup.
--Backup the AdventureWorks2008R2_data file in the PRIMARY filegroup
BACKUP DATABASE AdventureWorks2008R2
FILE = 'AdventureWorks2008R2_Data'
TO DISK = 'C:\Backups\AdventureWorks2008R2_Data.bak';
GO

--Backup the PRIMARY filegroup


BACKUP DATABASE AdventureWorks2008R2
FILEGROUP = 'PRIMARY'
TO DISK = 'C:\Backups\AW2008R2_PRIMARY.bak';
GO

--Create a differential backup of the PRIMARY filegroup


BACKUP DATABASE AdventureWorks2008R2
FILEGROUP = 'PRIMARY'
TO DISK = 'C:\Backups\AW2008R2_PRIMARY_diff.bak'
WITH DIFFERENTIAL
Copy-Only Backups
• You can create a copy-only backup to
perform a full or transaction log
backup; this is independent of the
normal backup sequence maintained
using standard backup operations.
Syntax Used to Create Copy-Only Backups
USE master
GO
--Create a copy-only full backup
BACKUP DATABASE AdventureWorks2008R2
TO DISK = 'C:\AdventureWorks2008R2.bak'
WITH COPY_ONLY ;
GO
--Create a copy-only log backup
BACKUP LOG AdventureWorks2008R2
TO DISK = 'C:\AdventureWorks2008R2.trn'
WITH COPY_ONLY;
GO
Backup Compression
• Backup compression is a long-awaited feature added in SQL Server
2008.
• Prior to SQL Server 2008, you had to purchase third-party tools to
achieve backup compression.
• By default, backup compression is turned off at the server level. To
change the default configuration for backup compression, you can
use
USE master
GO
EXEC sp_configure 'backup compression default', '1';
RECONFIGURE WITH OVERRIDE;
You can override the default behavior for backup compression by
specifying WITH COMPRESSION or WITH NO_COMPRESSION when
issuing the backup statement.
USE master
GO
PRINT '-----AdventureWorks2008R2 With Compression-----';
--Create a full backup with compression
BACKUP DATABASE AdventureWorks2008R2
TO DISK = 'C:\Backups\AdventureWorks2008R2_C.bak'
WITH COMPRESSION;
GO
PRINT Char(13) + '-----AdventureWorks2008R2 No Compression-----';
--Create a full backup with no compression
BACKUP DATABASE AdventureWorks2008R2
TO DISK = 'C:\Backups\AdventureWorks2008R2_NC.bak'
WITH NO_COMPRESSION;
GO

Code to Determine the Compression Percentage per


Backup
SELECT database_name,
backup_finish_date,
1 - (compressed_backup_size/backup_size) PercentCompressed
FROM msdb.dbo.backupset
WHERE backup_size > compressed_backup_size;
Backups from T-SQL
• Refer printouts.. And Enjoy 
Backup History
• SQL Server maintains a set of tables that hold the backup history performed on
each server instance in the msdb database.
The backup history tables include:
• backupfile. This contains information about the data and log files for a database
backup.
• backupfilegroup. This contains information about every filegroup in a database
when the backup was performed.
• backupmediafamily. This holds one row for each media family, including the
logical device name, physical device name, and device type.
• backupmediaset. This holds one row for each backup media set. Each media set
can have multiple media families.
• backupset. This holds a single row for each backup set created by a successful
backup operation. The backupset table holds information, such as the database
name, the backup start and end times, and log sequence numbers at the time of
backup.
Script to Remove Backup History in the msdb in Three-
Month Intervals While Maintaining Six Months of History
USE msdb
GO
--Keep 6 months of history,
--but never delete more than 3 months at a time.
DECLARE @dte DATETIME;
SET @dte = (SELECT MIN(backup_start_date)
FROM backupset WITH (nolock));
SET @dte = (SELECT dateadd(mm,3,@dte));
IF (dateadd(mm,-6,getdate()) < @dte )
BEGIN
SET @dte = dateadd(mm,-6,getdate());
END;
PRINT @dte;
EXEC sp_delete_backuphistory @dte;
Types of Restores
• Restoring Full Backups
• Restoring Transaction Logs
• Restoring Differential Backups
• Restoring Files and Filegroups
• Restoring Pages
• Piecemeal Restores
• Restoring Snapshots
• RESTORE HEADERONLY
• RESTORE FILELISTONLY
• RESTORE VERIFYONLY
Restoring Files and Filegroups
• File or filegroup restores enable you to restore a damaged file instead
of restoring an entire database.
• If you have multiple files or filegroups within a database and only one
of those files or filegroups is damaged, then you can restore only the
damaged file or filegroup, minimizing the amount of recovery time
needed.
RESTORE DATABASE AdventureWorks2012
FILE = 'AdventureWorksIndex.ndf',
FILEGROUP = 'Indexes'
FROM DISK = 'c:\backups\AdventureWorksFileGroup.bak'
WITH RECOVERY,
STATS
Restoring Pages
• You can use page restores to restore specific pages that exist within your
database without having to restore an entire database backup.
• In SQL Server, you can identify suspected corrupt pages in the suspect_pages
table in the msdb database.
• If a small number of pages are corrupt, then it may be more efficient to restore
the pages within the file instead of the entire file itself.
• The ability to restore a handful of pages instead of an entire file will help you to
keep more of your database online and available.
USE master
GO
RESTORE DATABASE AdventureWorks2012
PAGE = '4:300'
FROM DISK = 'c:\backups\AdventureWorksFileGroup3.bak'
WITH RECOVERY
Piecemeal Restores
• The piecemeal restore is an option to restore your primary filegroup and indicate
that you will restore your other filegroups at a later time. The main idea is that
you want to get up and running as soon as possible.
• During a piecemeal restore, once the primary filegroup finishes restoring, it will
be online and able to take transactions.
• The remaining filegroups will remain in an offline state until you bring them
online.
• Just specify the database name that you would like to restore, the location of the
restore file, the PARTIAL option, and any other filegroup that you would like
restored with the appropriate recovery options.
• The PARTIAL option lets SQL Server know that you will be restoring the primary
filegroup and any other filegroup specified in the initial statement only.
• After you specify the PARTIAL option in the initial statement, you do not have to
specify it anymore when restoring the remaining filegroups.
SQL Code to Perform a Piecemeal Restore
USE master GO
RESTORE DATABASE AdventureWorks2012
FILEGROUP = 'Primary'
FROM DISK = 'c:\backups\AdventureWorksFileGroup3.bak'
WITH PARTIAL,
RECOVERY,
STATS
Restoring Snapshots
• Options exist in SQL Server 2012 to restore a database from an
existing snapshot.
• Once you restore the database from a snapshot, you are unable to roll
forward any transactions that have occurred since capturing the
snapshot.
• So make sure the snapshot contains all the data that you would like to
restore prior to overwriting your database.
USE master
GO
RESTORE DATABASE Adventureworks
FROM DATABASE_SNAPSHOT = 'AdventureWorks_ss'
RESTORE HEADERONLY
• SQL Server allows you to review all of the backup header information
stored within a backup device.
• To view that information, execute the command RESTORE
HEADERONLY on the database file in question.
• The information will consist of the backed up database name, server
name that the database exists on, the time when the backup was
taken, and a lot more information about each backup header stored
on the device.
USE master
GO
RESTORE HEADERONLY
FROM DISK = 'c:\backups\AdventureWorks_log.bak'
RESTORE FILELISTONLY
• As the name FILELISTONLY indicates, this restore option only displays
the information about the file or filegroup stored within a backup set.

USE master
GO
RESTORE FILELISTONLY
FROM DISK = 'c:\backups\AdventureWorks_log.bak'
RESTORE VERIFYONLY
• The RESTORE VERIFYONLY option within SQL Server allows you to
validate your database backups without actually performing the
restore operation.
• The VERIFYONLY option does additional checking to ensure that the
backup file is complete and the entire data set is readable,
• But it does not check the actual data structures of the backup set.
USE master
GO
RESTORE VERIFYONLY
FROM DISK = 'c:\backups\AdventureWorks_log.bak'
Assignments
• What is Tail Log Backup?
• How to restore it?

• Enjoy 
Thank You

Das könnte Ihnen auch gefallen