47
Backing Up and Restoring Backing Up and Restoring Databases Databases Lesson 16

Backups and Restore -- SQL Server

Embed Size (px)

DESCRIPTION

Backups and Restore -- SQL Server

Citation preview

Page 1: Backups and Restore -- SQL Server

Backing Up and Restoring Backing Up and Restoring DatabasesDatabases

Lesson 16

Page 2: Backups and Restore -- SQL Server

Skills MatrixSkills Matrix

Page 3: Backups and Restore -- SQL Server

Skill MatrixSkill Matrix

Page 4: Backups and Restore -- SQL Server

BackupsBackups• Some features are common to all types of

backup. • All backups in SQL Server are online backups,

which means your users can access the database while you’re backing it up.

• When a data file backup starts, SQL Server records the current LSN from the log file.

• Once the backup completes, SQL Server backs up all the entries in the transaction log from the LSN it recorded at the start of the backup to the current LSN.

Page 5: Backups and Restore -- SQL Server

Backup DevicesBackup Devices• To perform any type of backup, you need a

place to store it. • The medium you’ll use to store a backup is

called a backup device. • Backups are stored on a physical backup

medium or media, which can be a tape drive or a hard disk (local or accessed over a network connection).

• A backup device is a logical construct that provides this definition.

• it represents the backup medium.

Page 6: Backups and Restore -- SQL Server

Types of BackupsTypes of Backups

• You can create two types of backup devices:• Permanent

• Temporary

• Temporary backup devices are created on the fly when you perform the backup.

• You can use permanent backup devices over and over again; you can even append data to them, making them perfect devices for regularly scheduled backups.

Page 7: Backups and Restore -- SQL Server

Full BackupsFull Backups• Just as the name implies, a full backup

records the entire database. • It backs up the database files, the locations

of those files, and portions of the transaction log (from the LSN recorded at the start of the backup to the LSN at the end of the backup).

• All other backup types depend on the existence of a full backup.

• This means you can’t use a differential or transaction log backup if you have never performed a full backup.

Page 8: Backups and Restore -- SQL Server
Page 9: Backups and Restore -- SQL Server

Differential BackupsDifferential Backups

• Differential backups record all the changes made to a database since the last full backup was performed.

• The differential backup gets a little bigger each time it’s performed, but it’s still a great deal smaller than the full backup; so, a differential executes faster than a full backup.

Page 10: Backups and Restore -- SQL Server

Transaction Log BackupsTransaction Log Backups

• Although they rely on the existence of a full backup, transaction log backups don’t back up the database itself.

• This type of backup records only sections of the transaction log; specifically, those activities since the last transaction log backup.

Page 11: Backups and Restore -- SQL Server

Transaction Log BackupsTransaction Log Backups

• When you configure a database to use the Full or Bulk-Logged recovery model, a transaction log backup clears old transactions from the transaction log.

• Therefore, if you performed only full and differential backups on most production databases, the transaction log would eventually fill to 100 percent of the file capacity, and your users would be locked out of the database.

Page 12: Backups and Restore -- SQL Server

Transaction Log BackupsTransaction Log Backups

• When a transaction log becomes 100 percent full, users lose access to the database until an administrator clears the transaction log.

• Perform regular log backups to assure continuous database availability.

Page 13: Backups and Restore -- SQL Server

Filegroup BackupsFilegroup Backups• Full, differential, and transaction log backups are

great for small to large databases, but consider using filegroup backups for databases measured in terabytes.

• Essentially, a filegroup provides a way of storing a database on more than one file, and it gives you the ability to control in which of those files you store objects (such as tables or indexes).

• The files must be backed up as a single unit; you can’t back up the tables one night and the associated indexes the next.

• To perform a filegroup backup, you need to create a filegroup.

Page 14: Backups and Restore -- SQL Server

Copy-Only BackupsCopy-Only Backups

• special backup of a database outside your normal backup scheme.

• Does not affect the logs or database. • You do this using the COPY_ONLY

option of the BACKUP statement.

Page 15: Backups and Restore -- SQL Server

Partial, Full, and Differential BackupsPartial, Full, and Differential Backups• A partial backup is a special type of backup

that you can use only with filegroups. • It backs up only the primary filegroup and

all read-write filegroups. – Read-only filegroups aren’t backed up. – You need to back up read-only filegroups

only occasionally, because they don’t change; thus

• To perform a partial backup, use the READ_WRITE_FILEGROUPS option of the BACKUP statement.

Page 16: Backups and Restore -- SQL Server

Backup CompressionBackup Compression• SQL Server 2008 includes an easy-to-

implement method of incorporating compression when conducting database backups.

• Backups can be set to automatically use compression via a new database option.

• This new option is set at the Database Settings node of the Server Properties.

Page 17: Backups and Restore -- SQL Server

Backup CompressionBackup Compression• This option setting can be overridden by

specifying in the BACKUP command whether or not compression should be performed. BACKUP DATABASE AdventureWorks TO DISK = 'C:\SQLServerBackups\AdventureWorks.Bak' WITH COMPRESSION

• The ability to create backups with compression is only available with the Enterprise and Developer editions of SQL Server 2008. All editions of SQL Server 2008 can restore a compressed backup.

Page 18: Backups and Restore -- SQL Server

Backing Up to Multiple DevicesBacking Up to Multiple Devices

• If you really want to speed up the process, you can perform backups to multiple devices at the same time.

• If you want to do this with tape devices, you need more than one local tape drive in the SQL Server machine.

• This type of backup uses multiple devices in parallel and writes the data in stripes across the media.

Page 19: Backups and Restore -- SQL Server

Multiple Backup CopiesMultiple Backup Copies• Typically, backups are not part of a mirrored

media set, and BACKUP statements simply include a TO clause.

• However, a total of four mirrors are possible per media set.

• For a mirrored media set, you must have Enterprise Edition, and the backup operation must write to multiple groups of backup devices.

• Each group of backup devices comprises a single mirror within the mirrored media set.

• Every mirror must use the same quantity and type of physical backup devices, which must all have the same properties.

Page 20: Backups and Restore -- SQL Server

Multiple Backup CopiesMultiple Backup Copies

• Backing up to multiple devices on the same physical drive degrades performance and eliminates the redundancy desired.

• In production, make sure you involve multiple spindles and perhaps multiple servers.

Page 21: Backups and Restore -- SQL Server

Restoring DatabasesRestoring Databases

• Regularly rehearse recovering your database before the expected disaster occurs.

• Suspect or corrupt databases aren’t the only reasons to perform restores, though.

• You may, for example, need to send a copy of one of your databases to the home office or to a branch office for synchronization.

Page 22: Backups and Restore -- SQL Server

Restoring DatabasesRestoring Databases• The restore operation will likely miss

recovering the last transactions entered by users, so be sure to alert your users of the need to restore.

• Train them how to verify that the last few entries still exist after your restoration effort.

• Some corporations train users to maintain today’s records until tomorrow in case a daily backup and subsequent restore misses all of yesterday’s data entries.

Page 23: Backups and Restore -- SQL Server

Recovery OptionRecovery Option

• The RECOVERY option, when set incorrectly, can thwart all your efforts to restore a database.

• The RECOVERY option tells SQL Server that you’ve finished restoring the database and that users should be allowed back in.

• Use this option only on the last file of the restore process.

Page 24: Backups and Restore -- SQL Server

Restoring to LocationRestoring to Location• SQL Server also remembers where the

original files were located when you backed them up.

• Thus, if you backed up files from the D:\ drive, SQL Server will restore them to the D:\ drive.

• In this instance, you need to use the MOVE…TO option. MOVE…TO lets you back up a database in one location and move it to another location.

Page 25: Backups and Restore -- SQL Server
Page 26: Backups and Restore -- SQL Server

Restore CheckRestore Check

• Finally, before allowing you to restore a database, SQL Server performs a safety check to make sure you aren’t accidentally restoring the wrong database.

• SQL Server compares the database name being restored to the name of the database recorded in the backup device.

Page 27: Backups and Restore -- SQL Server
Page 28: Backups and Restore -- SQL Server

Piecemeal RestorePiecemeal Restore

• Piecemeal restores restore the primary filegroup and (optionally) some secondary filegroups and make them accessible to users.

• You can restore the remaining secondary filegroups later if needed.

Page 29: Backups and Restore -- SQL Server

Online RestoreOnline Restore• Normally a database restore requires exclusive

access to the database. • For larger databases, a restore operation can take

hours. • When the Enterprise Edition of SQL Server is in

use, the online option for restores is available. • Online restores are automatic and require the use

of different filegroups so that different filegroups, files, or even individual pages can be restored.

• This could be extremely useful in the event of a torn or damaged page in a large highly busy database.

Page 30: Backups and Restore -- SQL Server

Backup StrategyBackup Strategy• Plan a backup strategy that details when to

use which type of backup. • If you have a relatively small database, you

can perform just full backups with no other type.

• A full backup only strategy results in a comparatively slow backup when compared to other strategies.

• A full backup only strategy results in a faster restore process than with other strategies because it uses only one storage device.

Page 31: Backups and Restore -- SQL Server

Backup StrategyBackup Strategy

• The transaction log clears only when you perform a transaction log backup.

• With a full-only strategy, your transaction log may fill completely locking your users out of the database.

Page 32: Backups and Restore -- SQL Server

Backup StrategyBackup Strategy• You can avoid this problem in two ways:

– Set the recovery model for the database to Simple.• Instructs SQL Server to completely empty

the log every time it writes to the database from the log (a process called checkpointing).

•You’ll lose up-to-the-minute recoverability because the latest transactions will be deleted every time the server checkpoints.

• If your database crashes, you can restore it only to the time of the last full backup.

Page 33: Backups and Restore -- SQL Server

Backup StrategyBackup Strategy– Second, you can perform the full backup and,

immediately afterward, perform a transaction log backup with the TRUNCATE_ONLY clause. •This option frees space, but risks possible data

loss. •After the log truncates by using either NO_LOG or

TRUNCATE_ONLY, the changes recorded in the truncated portion of the log become unrecoverable.

•Therefore, for recovery purposes, after using either of these options, you must immediately execute BACKUP DATABASE to take a full or differential database backup—not a recommended strategy.

Page 34: Backups and Restore -- SQL Server

Tail LogTail Log• In the event of any database failure, use the

NO_TRUNCATE option with the transaction log backup to save the orphaned log.

• The last active log file is called the tail log.• To record the tail log, the drive holding the

data must survive the disaster. • To assure a tail log copy, consider

redundant log entries to two physical locations as far apart as practical.

• Consider database mirroring as a possible solution.

Page 35: Backups and Restore -- SQL Server

Planning for Full Backups with Planning for Full Backups with Differential BackupsDifferential Backups• A full/differential strategy provides a faster backup

than full alone. • With a full-only backup strategy, you’re backing

up the entire database every time you perform a backup.

• With a full/differential strategy, you’re backing up only the changes made to the database since the last full backup, which should be faster than backing up the whole database.

• The full/differential strategy has the major disadvantage of being slower when restoring than with the full-only strategy, because full/differential sets require you to restore more backup sets.

Page 36: Backups and Restore -- SQL Server

Planning for Full Backups with Planning for Full Backups with Differential BackupsDifferential Backups

• Be aware that differential backups don’t clear the transaction log.

• If you opt for this method, you should clear the transaction log manually by backing up the transaction log.

Page 37: Backups and Restore -- SQL Server

Planning for Full with Transaction Log Planning for Full with Transaction Log BackupsBackups• Consider full/transaction backups with

small or huge databases. • This method offers several advantages. • It’s the best method to keep your

transaction logs clean, because this type of backup purges old transactions from your transaction logs.

• It makes for a fast backup process. • This strategy requires more restoration

time than full alone or full/differential.

Page 38: Backups and Restore -- SQL Server

Planning for Full, Differential, and Planning for Full, Differential, and Transaction Log BackupsTransaction Log Backups

• If you combine all three types of backups, you get the best of all worlds.

• The backup and restore processes are still relatively fast, and you have the advantage of point-in-time restores as well.

Page 39: Backups and Restore -- SQL Server

Planning for Filegroup BackupsPlanning for Filegroup Backups• You learned the mechanics of the filegroup

backup earlier in this lesson, so you know they’re designed to back up small chunks of the database at a time rather than the whole database all at once.

• This may come in handy, for example, with a 700 GB database contained in three files in three separate filegroups.

• When a disk fails, you need to restore the backup of the filegroup that failed and the transaction log backups that occurred after the filegroup was backed up.

Page 40: Backups and Restore -- SQL Server

SummarySummary

• In this lesson, you learned how to back up and restore your databases.

• The first topic was backups. You have many reasons to back up data: natural disaster, hardware malfunction, and even people with malicious intent.

• If you perform regular backups, you can overcome these problems.

Page 41: Backups and Restore -- SQL Server

SummarySummary• You can use four types of backups to help

thwart the evils that could claim your data. First, the full backup, the basis of all other backups, makes a copy of the entire database.

• Next, the differential backup grabs all the changes made to the database since the last full backup.

• Use the transaction log backup for implementing a quick backup strategy, performing point-in-time restores, and clearing the transaction log on a periodic basis.

• Finally, the filegroup backup makes backups of small chunks of very large databases.

Page 42: Backups and Restore -- SQL Server

SummarySummary

• After learning how to back up data, you learned how to restore from those backups. – First you performed a standard database

restore, then you performed the more advanced point-in-time and piecemeal restores.

• Finally, you learned how to create a backup strategy so that you have a better idea of what to back up and when to do it.

Page 43: Backups and Restore -- SQL Server

Summary for Certification Examination Summary for Certification Examination • Understand backup devices. SQL Server

can store backups on hard disk or tape drive; however, because SQL Server doesn’t know about the hardware attached to your machine, you have to tell it by defining a backup device.

• Backup devices are SQL Server objects that represent the available backup media that SQL Server can use for storing backups.

• These can point to hard disk or tape drives and can be permanent or temporary.

Page 44: Backups and Restore -- SQL Server

Summary for Certification Examination Summary for Certification Examination • Know the backup types. • Four backup types are available in SQL Server.

– Full backups back up the entire database. You must perform a full backup before any other type.

– Differential backups record only the changes made to the database since the last full backup.

– Transaction log backups back up and clear the log.

– You can use filegroup backups to back up the contents of one or more filegroups.

Page 45: Backups and Restore -- SQL Server

Summary for Certification Examination Summary for Certification Examination • Know how to restore. • You need to remember which files to

restore—and to set NORECOVERY until the last one.

• Practice this. • Set a time for your team to rehearse,

practice and drill with an actual restore to a separate hard drive.

• Assure that all processes, procedures and techniques work.

Page 46: Backups and Restore -- SQL Server

Summary for Certification Examination Summary for Certification Examination • If you want to return a database to the most recent

content, then you restore the last full backup, the last differential (if any exist), and all the transaction log backups after the last full or differential backup.

• You can also perform a point-in-time backup that allows you to restore up to a specific point in time, but only if you have performed transaction log backups including the tail log.

• You can also perform a piecemeal restore, which allows you to restore the primary and any secondary filegroups to a new database. You can put the database back on line prior to completing the file group restores.

Page 47: Backups and Restore -- SQL Server

Summary for Certification Examination Summary for Certification Examination

• Know how to devise a backup strategy.

• You need to know how to use all this technology to protect your data, and to effectively do that, you must have a backup strategy.

• You need to know when to use each type of backup and how to use those backups to restore your data after a disaster.