Best Practices For Backup and Restore in SQL Server 2005: by Javier Loria
Best Practices For Backup and Restore in SQL Server 2005: by Javier Loria
Best Practices For Backup and Restore in SQL Server 2005: by Javier Loria
2 BEST PRACTICES FOR BACKUP AND RESTORE IN SQL SERVER 2005 courtesy of Dell
security, performance and manageability. Make sure Backup Types
your plan considers these requirements.
The most relevant requirement to consider is security. SQL Server 2005 offers three basic types of backups:
Security is the most sensitive operational requirement Database (Full), Differential, and Log backups.
to contemplate, because backups are more exposed to
physical attacks than the databases they hold. Consider Full Backup
the following examples1: A full backup makes a copy of all used database pages in
1) Someone takes the backup and makes a copy. This is a the database, meaning it copies all the information (Data
confidentiality threat. and indexes) of the database, and excludes all empty data
2) Someone overwrites the content of the backup. This is pages. Full backups also duplicate the part of the transac-
an integrity threat. tion log that includes all open transactions and transac-
3) Someone steals the tapes. This is an availability threat. tions that occurred during the backup process. This allows
the restore process to achieve transactional integrity. The
We will discuss later in this white paper some of the mea- following code is an example of Full Database Backup
sures you may take to reduce these threats. command:
1 This example was authored by the Application Consulting & Engineering (ACE) Team (http://blogs.msdn.com/threatmodeling/). The ACE Team created the
Threat Analysis and Modeling Tool, that can be downloaded from http://msdn2.microsoft.com/en-us/security/aa570413.aspx
courtesy of DELL BEST PRACTICES FOR BACKUP AND RESTORE IN SQL SERVER 2005 3
Figure 1 Backup Database Window in SSMS 2005
4 BEST PRACTICES FOR BACKUP AND RESTORE IN SQL SERVER 2005 courtesy of Dell
File Backup Partial Backup
File backups do not copy the database; instead the file Partial backup is similar to File and Filegroup backups;
backup aim is to back up only one or more files that are they do not back up the entire database. Partial backups
part the database. always back up the Primary Filegroup and every Read and
Write Filegroup that is part of the database. Besides the
Databases in SQL server 2005 have three different types fact that partial backups simplify the backup statement to
of files: Primary, Secondary, or Log Files. Primary and back up only read and write filegroups, they also offer the
Secondary files hold database pages; in contrast, Log files advantage of allowing this type of backup when using a
hold the transaction log. File backups can make copies of simple recovery model in the database. To perform a par-
pages from Primary and Secondary Files. tial backup, use the Read_Write_Filegroups option of the
Backup command as the following example does:
This type of backup can be used to speed up the restora-
tion process, when the device that stores one of the files -- Full Partial Backup
BACKUP DATABASE AdventureWorks
fails but the rest of the devices are working correctly. In Read_Write_Filegroups
this case, there is no need to restore the full database; TO DISK = N’C:\Backup\AdventureWorks.bak’
only the affected file. The following SQL code performs
Full and Differential File backups: -- Differential Partial Backup
BACKUP DATABASE AdventureWorks
Read_Write_Filegroups
-- Full File Backup
TO DISK = N’C:\Backup\AdventureWorks.bak’
BACKUP DATABASE AdventureWorks
WITH DIFFERENTIAL
FILE=’AdventureWorks_Data’
TO DISK = N’C:\Backup\AdventureWorks.bak’
courtesy of DELL BEST PRACTICES FOR BACKUP AND RESTORE IN SQL SERVER 2005 5
A diagram of Full-Backup and Simple Mode strategy:2 A diagram of a Log & Full backup strategy:
Schedule the Log backup before the full backup. This When using Full, Differential, and Log Backups the DBA
approach will speed up the recovery process. If you schedules a Full Backup occasionally, differential back-
schedule the Log backup before the full backup, you will ups often, and Logs very often. In this case, occasionally
only need to restore the last full backup and one transac- means as frequently as needed, to minimize the impact of
tion log to recover the database. Scheduling after the full a full backup, but enough to avoid differential backups to
backup will demand the restoration of the last full backup grow too much; often means as frequently as needed, to
and two transaction logs backups. minimize the impact of restoring too many log backups;
and very often means as frequently as needed to mini-
Use the Log & Full backup strategy in 8x5 databases mize data loss exposure. Some implementations of this
where you can afford to lose up to one day of work. strategy may be:
1) Full backup every month, differential backups every
night, and log backups every two hours.
2 The scale on the Backup strategies diagrams is deliberately abstracted. Some databases will require weekly full database backups; more often business
should choose daily backups; and finally others will required multiple daily full backups. Take into account the business requirements: value, volatility, size
and usage, when defining your backup plan.
6 BEST PRACTICES FOR BACKUP AND RESTORE IN SQL SERVER 2005 courtesy of Dell
2) Full backup every week, differential backups every 6
hours, and logs every fifteen minutes.
courtesy of DELL BEST PRACTICES FOR BACKUP AND RESTORE IN SQL SERVER 2005 7
° In case of a server breakdown due to software will be removed in future releases of SQL Server.
or hardware issues, the database service will be • When using the file system for backups, grant file and
available within 12 hours of the first report of the folder access to the SQL Service account and DBA
incident and no more than 15 minutes of data will only. The SQL Service account will require Read and
be lost. Write access to the folder or shared folder and the DBA
° In case of data center breakdown due to fire, flood, will required Full Control and should be the owner
earthquake or other environment or civil disasters, of the folder. Restring access to the backup files will
the database service will be available within 24 four prevent users from copying the files and restore them in
hours and no more than 4 hours of data will be lost. a server where they have administrative rights.
• Consider establishing an Auditing Policy on the Backup
To set unambiguous expectations of users, write an SLA Folder. Enabling file access auditing to the Backup
that describes the response time the IT Department will Folder will monitor user access to the backups.
provide in case of each type of event. • After the database is back up in a file or files, compress
and encrypt the files before moving the contents to
tape backups or other forms of long term storage.
Operational Best Practices Encrypting the backup files will help you protect the
Beside the backup and restore strategy, the DBA should confidentiality of the information if somebody gains
also consider different options and practices to handle physical access to the media.
backup and restore activities. The following guidelines • Physically secure backup media. Do not rely only on
may help you take some decisions about backup and encryption to secure the media, make sure that the
media is always physically secure to avoid someone to
restore procedures:
copy the media, and use brute force attacks to decrypt
the file and eventually restore the database in another
Performance server.
• Schedule Backup Operations When Database Activity Is • Sanitize the backup media before disposal. Do not
Low: Database operation may have an important impact throw tape media in the trash, unless you are absolutely
on the server; avoid backup operation when the activity certain that the data has been erased. Deleting the files
of the server is high, especially full database backups. or overwriting the files may not be enough. Consider
• Back up first to disk, whenever possible. Consider using the use of specialized software or magnetic methods to
a File Server to store the backups. Backing up to disk erase the information.
will greatly increase the performance of the backup
process and free the resources of SQL Server. Using file Integrity
backups also simplifies the restoration process. When • Use the CHECKSUM option of the Backup command.
backing up to a remote share, use UNC paths and do This option, new in SQL Server 2005, essentially
not use mapped drives. performs two types of verifications on database
• When using a File server to store backups, consider backups. The first one is that it stores a CHECKSUM
using a private LAN trunk to avoid general network of the backup that can help you verify if the backup
congestion. A private trunk requires that both the File is corrupt. The second verification only works if the
and the Database Servers have an additional network PAGE_VERIFY option of the database is in CHECKSUM
card connected to a private LAN. This practice also or TORN_PAGE_DETECTION mode. If one these modes
increases the security of the backup process. are enabled, the BACKUP statement will verify that
• Back up to tape or other devices for long run storage. the pages are reliable before they are backed up. The
Tapes and other media types offer an economical option default of the PAGE_VERIFY option is CHECKSUM;
to store the information for the long run. however the CHEKSUM option of the Backup command
• Do not use the same physical disk that hold the is not enabled by default. The following exemplifies the
database files or Log files for backup purposes. Using CHECKSUM option of the backup command:
the same physical disk not only affects the performance,
but also may reduce the recoverability of the plan. BACKUP DATABASE AdventureWorks
TO DISK = N’C:\Backup\AdventureWorks.bak’
Security WITH CHECKSUM
The following measures will help you reduce or handle
security threats: • Test Backup files periodically using the RESTORE
VERIFYONLY command. The following code is
an example of how to use RESTORE VERIFYONLY
Confidentially
command:
• Do not use the Password option of the Backup RESTORE VERIFYONLY
statement: The password option of the backup statement FROM DISK = N’C:\Backup\AdventureWorks.bak’
provides weak protection and does not prevent reading WITH CHECKSUM
of the data. The password option is also deprecated and
8 BEST PRACTICES FOR BACKUP AND RESTORE IN SQL SERVER 2005 courtesy of Dell
Availability the recovery team will test their ability to configure an
• Use the Mirror option of the Backup command to alternate environment ready for production.
backup to a local hard drive and a backup server
simultaneously. Having a local copy of the backup Manageability
increases the response time of the restoration process • Use SQL Server Agent to automate the Backup process.
and having a remote copy makes the copy available in Automating the backup process will facilitate the
case of a server collapse. The following command is backup process and reduce the likelihood of human
an example on how to use the MIRROR clause of the errors. The SQL Server agent will help DBAs automate
BACKUP command: administrative tasks, which include the backup process.
BACKUP DATABASE AdventureWorks To create a Job to backup a database use the following
TO DISK=’C:\Backup\AdventureWorks.bak’
MIRROR TO DISK=’\\ILUVATAR\Backup\AdventureWorks.bak’
procedure:
WITH FORMAT 1) Open SSMS
2) Connect to the SQL Server instance that holds the
• Perform trial restorations frequently. Frequently you database.
should verify that the backup system is properly copying 3) Navigate to the Server\Databases folder.
the database and that the backup hardware and process 4) Right click the database you want to backup.
is working correctly. Trial restorations should use test 5) From the shortcut menu, select Tasks, Backup.
server and should never be performed in a production 6) In the Database Backup dialog box, select the type of
environment. backup you want the server to perform, the backup
• Perform Fire Drills periodically. A fire drill is a
destination path and the backup options.
procedure that tests if the backup and restore plan will
help the IT department to be compliant with the SLA. In 7) Press Crl+Shift+M or select Script to Job option in the
a fire drill not only the backup is restored to verify the Backup Database window to create a Job.
integrity, the whole recovery plan is executed to verify 8) Select Schedules in the Select a Page listbox.
readiness in case of an event. A fire drill will assume 9) Press new to create a schedule for the backup.
that the production server or data center is down and 10) Configure the backup schedule (Figure 2) and select
Ok to confirm your selection.
10 BEST PRACTICES FOR BACKUP AND RESTORE IN SQL SERVER 2005 courtesy of Dell
To restore a database up to the mark, use the BACKUP DATABASE AdventureWorks
STOPATMARK option of the RESTORE LOG command. TO DISK = ‘C:\Backup\AdventureWorks.bak’
WITH COPY_ONLY
The following code will restore the database up using the
previously created log mark.
Summary of Backup Best Practices
RESTORE DATABASE AdventureWorks The following guidelines are a summary of the best prac-
FROM DISK = N’C:\Backup\AdventureWorks.bak’
WITH FILE = 1 tices included in this whitepaper:
, NORECOVERY • Have a Data Recovery Plan
° Begin designing your plan with a threat analysis
GO ° Make sure your plan covers threats from all sources:
RESTORE LOG AdventureWorks Hardware, Software, People and Environment.
FROM DISK = N’C:\Backup\AdventureWorks.bak’
WITH FILE = 2
° ou should consider the value, volatility, size and
Y
, STOPATMARK=’UpgradeVersion’
usage of your database when designing your data
GO recovery plan.
° The most relevant operational requirements of data
recovery plans are: security, performance, scalability
You can also use database snapshots when you want to
and manageability. Make sure your plan considers
have the ability to recover in case of a high risk operation. these requirements.
Database snapshots contrary to Log Marks can be used to • Backup Strategies
recover databases that use the simple recovery model. On ° Combine different backup types to create a backup
the other hand, database snapshots are only available in strategy.
the Enterprise Edition of SQL Server 2005. The following ° Do not backup tempdb.
code creates a database snapshot of the AdventureWorks ° Unless you plan to change its contents, do not back
database: up the model database.
° Use the Full Backup simple mode strategy only in
CREATE DATABASE AdventureWorks_PreviousVersion development, read only, stage or system databases.
ON ( NAME = AdventureWorks_Data ° Schedule the Log backup before the Full backup
, FILENAME = ‘C:\Backup\AdventureWorks_ ° Use the Log & Full backup strategy in 8x5 databases
PreviousVersion.ss’ ) where you can afford to lose up to one day of work.
AS SNAPSHOT OF AdventureWorks;
GO ° Consider the use of the Full & Log backup strategy
in 8x5 databases, and non-volatile 24x7 databases.
The following code will restore the database from a previ- ° Use the Full, Differential and Log strategy in volatile
24x7 databases, when the value or your data
ously created snapshot: requires very frequent log backups, and in non-
partitioned Data marts.
RESTORE DATABASE AdventureWorks
FROM DATABASE_SNAPSHOT = ‘AdventureWorks_
° Consider the use of a Full - File/Filegroup – Log
PreviousVersion’
Backup strategy in Very Large Databases (VLDB) or
partitioned Data marts/Data warehouses.
• Only DBAS should be able to restore and back • Write a Service Level Agreement that describes the
up databases: Limit access to Backup and Restore response time the IT Department will provide in case of
operations, because either one can hinder your backup each type of event.
plan. • Performance
• Use the COPY_ONLY option for unscheduled backups. ° Schedule Backup Operations When Database
The COPY_ONLY option of the BACKUP command Activity Is Low.
identifies backups that should not affect the normal ° Back up first to disk, whenever possible. Consider
sequence of backups. This command is useful when you using a File Server to store the backups.
want to backup databases outside your DRP. If you do ° When using a File server to store backups, consider
not use this option you may disrupt the plan, because using a private LAN trunk to avoid general network
the backup may not be available in case of a disaster. congestion.
When you run a full backup with the COPY_ONLY, the ° Back up to tape or other devices for long run
command will not affect following differential backups storage.
that will not use this backup as the starting point. When ° Do not use the same physical disk that hold the
you run a Log backup with the COPY_ONLY option, the database files or Log files for backup purposes.
Log is not truncated and will be available for the next • Security
Log backup. An example of how to use the COPY_ONLY ° Do not use the Password option of the Backup
option: statement.
° When using the file system for backups, grant file
courtesy of DELL BEST PRACTICES FOR BACKUP AND RESTORE IN SQL SERVER 2005 11
and folder access to the SQL Service account and Javier Loria is a Solid Quality Mentors Mentor based in
DBA only Costa Rica. He began his professional career in 1992,
° Consider establishing an Auditing Policy on the as a software developer and system engineer. His career
Backup Folder evolved rapidly to the training side, especially in the XML
° After the database is back up in a file or files, and OLAP world, training customers at different loca-
compress and encrypt the files before moving the
contents to tape backups or other long term storage. tions in Latin America. He is a software architect, and
Business Intelligence Architect assisting our customers in
° Physically secure backup media.
° Sanitize the backup media before disposal. Latin America. Javier became SQL Server MVP in 2001.
° Use the CHECKSUM option of the Backup Javier is an MCT, MCSE, MCSD, MCDBA, and MCAD.
command Javier was an active member responsible for writing the
° est Backup files periodically using the RESTORE
T
Microsoft SQL Server 2005 courseware. He is the coau-
VERIFYONLY command. thor of several books including MOC2782: Designing
° Use the Mirror option of the Backup command to Microsoft® SQL Server™ 2005 Databases, MCTS Training
backup to a local hard drive and a backup server Kit 70-431: Implementing and Maintaining SQL Server
simultaneously. 2005 and Microsoft SQL Server 2005 Database Essentials
° Perform trial restorations frequently. Step by Step.
° Perform Fire Drills periodically.
•M anageability
° Use SQL Server Agent to automate the Backup
process.
° Document the backup strategy.
° Define a backup media/tape rotation policy.
° Create a backup media log.
•O ther Best Practices
° Before restoring any database, consider the need of
backing up the existing log file
° Before performing any High Risk operation add a
Log Mark or create a Database snapshot
° Only DBAS should be able to restore and backup
databases.
° Use the COPY_ONLY option for unscheduled
backups.
12 BEST PRACTICES FOR BACKUP AND RESTORE IN SQL SERVER 2005 courtesy of Dell