Although backups are important for maintaining SQL Server, they’re avoided sometimes by DBAs who assume backups are too complicated. You can actually implement backups easily with a few simple commands. In this article I’ll answer your questions about basic but crucial elements of database backup and include scripts for saving a copy of the database which you can use for a successful restore. For more SQL Server tips, check out my blog at sqlmag.com/go/SQLskills.

Q: What are the types of backup methods in SQL Server?
A: SQL Server has four backup methods: full, transaction log or incremental, differential, and file or file group. You should always perform a full backup, in which you make a complete copy of the database. The transaction log or incremental backup method copies all the modifications made to a database. A differential backup makes a copy of all the changes in a database since the last full backup. The file or file group backup method copies the actual database files on disk.

A full backup quickly restores a database to its original state and is the baseline for the other types of backups. A full backup contains all of a database’s data, structures, and security objects, plus the transaction log, which you can use to restore any changes made to the database while it’s being backed up. A database restored by the full backup method doesn’t contain any uncommitted transactions (transactions which have begun but to which the administrator hasn’t explicitly committed in the database either by turning on auto commit or by using the COMMIT statement). The following code sample shows a full backup:

BACKUP DATABASE AdventureWorks
TO DISK = ‘C:\full_bk_AdventureWorks.bak’

The transaction log or incremental backup helps recover a database to its latest working condition. To use this method, you need to set the database to full recovery or bulk recovery mode. To restore a database to a point in time, you must have an unbroken chain of transaction log backups. You need to first restore the database from a full backup before you can restore from a transaction log backup. Finally, you must restore each transaction log backup in the order in which it was taken. The following code sample shows a transaction log backup:

BACKUP LOG AdventureWorks
TO DISK = ‘C:\log_bk_AdventureWorks.trn’

Performing a differential backup saves time and media space compared with performing a full backup. If the database is set to full-recovery mode, restoring the latest differential backup restores the database state to the time the last differential backup was completed. A differential backup can’t be restored independently; it can be restored only after a full backup is restored. The following code sample shows a differential backup:

BACKUP DATABASE AdventureWorks
TO DISK = ‘C:\diff_bk_AdventureWorks.dif’
WITH DIFFERENTIAL

The file or file group backup method is an alternative way to perform a full backup. This method lets you quickly restore a database to working condition. However, you can perform a file or file group backup only under certain conditions. To use this method, the database must have been created with multiple files or file groups. The following code sample shows a file backup:

BACKUP DATABASE AdventureWorks FILE =
‘AdventureWorks_data’
TO DISK = ‘C:\file_bk_AdventureWorks.
data’

Q: What are the recovery models in SQL Server?
A: SQL Server has three recovery models: full, bulk-logged, and simple. In the full recovery model, all changes to the database are logged, so the database can be restored to the point of failure with full backup and log files. Use the full recovery model for essential databases that are updated frequently. The following code sample shows a full recovery:

ALTER DATABASE AdventureWorks
SET RECOVERY FULL

In the bulk-logged recovery model, all changes to the database except high-speed bulk insert operations are logged. (Don’t let the name confuse you—this method doesn’t actually log bulk processes.) Database performance isn’t compromised when bulk operations are in process. The following code sample shows a bulk-logged recovery:

ALTER DATABASE AdventureWorks
SET RECOVERY BULK_LOGGED

The simple recovery model lets you restore a database to the point of its last full backup. A database that isn’t updated frequently is a candidate for the simple recovery model. The following code sample shows a simple recovery:

ALTER DATABASE AdventureWorks
SET RECOVERY SIMPLE

Q: Does the RESTORE HEADERONLY command change or restore the header of a BACKUP file?
A: This command doesn’t change anything in the header. The clause simply returns all the backup header information for all the backup sets on a particular backup device. The following code sample illustrates using RESTORE HEADERONLY:

RESTORE HEADERONLY
FROM DISK = ‘C:\file_bk_
AdventureWorks.data’
WITH NOUNLOAD;

Q: What’s a tail-log backup, and when do you use it?
A: The tail-log backup requirement first occurs in SQL Server 2005 and applies to all subsequent versions. When you recover a database using the full or bulk-logged recovery model, before you restore it, you might find that some transaction log entries don’t yet have backups. Backing up this set of transaction log entries is known as a tail-log backup. SQL Server 2005 requires a tail-log backup operation on a database before it’s restored, to ensure that no data is accidentally lost. The following code sample shows a tail-log backup:

BACKUP LOG AdventureWorks
TO DISK = ‘C:\taillog_bk_
AdventureWorks.trn’
WITH NORECOVERY




You must log on before posting a comment.

If you don't have a username & password, please register now.

Reader Comments

Link to "tail-log backups" in the Learning Path section is broken.

stefbauer

Article Rating 3 out of 5

Thanks for pointing out this problem stefbauer! I have replaced the link so you should be able to access that article now.

Megan Bearly assistant editor mbearly@sqlmag.com

meganbearly

Article Rating 5 out of 5

Megan, since you work in the magazine (at least, looks like) you shouldn't rate the article. Don't you think so?

MarcosGalvani

Article Rating 2 out of 5

I felt a little lost with the article. I mean, if you are an advanced DBA it is pointless and if you are a novice, it doesn't explain too much, not even direct you to a more detailed material. Shame on you.

MarcosGalvani

Article Rating 1 out of 5

Marcos, thanks for your feedback. As the name of Pinal's column implies, the article is geared toward SQL Server novices. What type of material (skill level) would you have found more helpful? P.S. You have a point about editors rating articles; I admit, I feel a little silly rating an article on a publication I work for, but that's the limitation of our current commenting mechanism.

AnneG_editor

Article Rating 5 out of 5

 

  Related Articles

Backup and Restore with SMO Database Restore Differential Backups Protect and Simplify

  Related Whitepapers

Buyer’s Guide to Log Management: Comparing On-Premise and On-Demand Solutions Get Started with Oracle on Windows DVD StoreVault SnapManagers for Microsoft Exchange and SQL Server

  Related Events

Power Up! With Virtualization Online Conference Microsoft TechEd Developers Microsoft Belgium Developer & IT Pro Days 2006

  Related eBooks

Making SQL Server Perform Backup and Recovery Survival Guide HA Solutions for Windows, SQL, and Exchange Servers

  Related Essential Guides

The Essential Guide to Business Intelligence Reporting: Choosing the Right Tool for the Right Job Virtualization of SQL Server 2008 The Essential Guide to Reporting Services Tips & Tricks

  Related Resources

Buy One Get One Order SQL Server Magazine and get Windows IT Pro Magazine FREE!! Instant Gratification - Only $5.95!! Instant online access to thousands of SQL Server Magazine articles! Get It All - Order Windows It Pro VIP Today! Online access to 26,000+ articles. A $500+ value for only $279!!   Email Newsletters

  vLabs Links

SQL Server 2000 SQL Server 2005 Upgrade