Sharpen Your Basic SQL Server Skills - 25 Jan 2008

Database backup demystified

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

Discuss this Article 5

MarcosGalvani
on Mar 5, 2008
Megan, since you work in the magazine (at least, looks like) you shouldn't rate the article. Don't you think so?
AnneG_editor
on Mar 5, 2008
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.
meganbearly (not verified)
on Jan 29, 2008
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
MarcosGalvani
on Mar 5, 2008
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.
stefbauer
on Jan 28, 2008
Link to "tail-log backups" in the Learning Path section is broken.

Please or Register to post comments.

IT/Dev Connections

Las Vegas
September 30th - October 4th

Paul ThurottOur Experts will show you:
• Common SQL Server
Problems
• Best Practices for T-SQL
• SQL Server Integration
Services
• Database Development

Come See Michael Otey & Tim Ford in Person!

Early Registration Now Open

From the Blogs
May 21, 2013
blog

A Common Misconception about MAXDOP

Out of the box, SQL Server is (and has been) able to take advantage of multiple processors/cores without any effort on behalf of administrators....More
May 9, 2013
blog

My ISO 8601-Compliant Signature 2

My family recently just "officially" announced that we're in the process of adopting a child from South Africa. We're quite excited, of course, but there's a ton of paperwork to do—along with the need for gobs of signatures....More
May 8, 2013
blog

Use SSIS for ETL from Hadoop

In this blog post, Mark Kromer walks you through using SSIS as a way to use ETL techniques using Microsoft's Hadoop on Windows (HDInsight) as a source using Hive connectors...More
SQL Server Pro Forums

Get answers to questions, share tips, and engage with the SQL Server community in our Forums.