SQL Server 2000 Lost Secondary Data File - Full Recovery Model

Restore a SQL Server Secondary Data File By Using a Database Backup, Differential Backup, and Transaction Log Backup

Description:

We will end up destroying a secondary data file in a production database, then using a database backup, a differential backup, and a transaction log backup to perform a full restore. The destruction of the secondary data file will simulate a loss of the disk in which the secondary data file was residing on.

(back to top)

Requirements:

  • As with all of the exercises on this site, do not perform this exercise on a production server.  Perform this exercise in a closed development environment
  • The database needs to be using the Full Recovery Model - for the exercise on changing the recovery model, click here
  • You will need to have a backup device already created - for the exercise on creating a backup device, click here
  • You need to have a secondary data file in the database - the Setup section will help you if one doesn't already exist.
(back to top)

Setup:

It is doubtful that you have more than just the primary data file for Northwind if you are using the original Northwind install.  We need to add a data file for this exercise. So, in Enterprise Manager, right click on the Northwind database and choose properties as shown below.

Enterprise Manager screen shot, right clicking on the Northwind database and choosing properties

 

From the "Data Files" tab, you'll see that the primary data file already is present.  Type in Northwind_Secondary under the primary data file to add a secondary data file to the Northwind database.  Then we're going to accept all of the defaults for this data file - click the "OK" button. We now have a secondary data file at our disposal to destroy. Expand the "Location column" and commit it to memory (or write it down). We will be deleting this data file later in the exercise.

Northwind Properties screen shot showing the addition of a data file to the database so that there is now a primary and a secondary data file

 

(back to top)

GUI Steps:

1) In Query Analyzer, insert a row into the shippers table and then select from the table to make sure that our insert was accepted

Query Analyzer screen shot of inserting a row into the shippers table in the Northwind database and querying from the table

 

2) Start the steps to perform a full database backup by going into Enterprise Manager, right clicking on the Northwind database, then choosing "All Tasks" and "Backup Database".

Enterprise Manager - right clicking on Northwind Database, choosing All Tasks, and selecting Backup Database

 

3) The Backup screen comes up. Choose the database name (Northwind). Name this backup Northwind Full. Make sure that the "Database - Complete" radio button is selected. Also make sure that you choose a destination (if no destination is chosen click the "Add" button to select a destination). Then choose the Append to media radio button. Finally, click on the "Options" tab to go to the next step.

Screen Shot of SQL Server Backup Screen - provide a database name, backup name, database backup type (complete), backup destination, and click the OK button

 

4) We are going to verify our backup. On the "Options" tab check the "Verify backup upon completion" check box. Then click the "OK" button to start the backup.

Screen Shot of Backup database screen - verifying the backup upon completion and clicking the OK button

 

5) The full backup is complete. We're now going to add another row. In Query Analyzer, insert a row into the shippers table and then select from the table to make sure our insert was accepted.

Query Analyzer screen shot of inserting a row into the shippers table in the Northwind database and querying from the table

6) Prepare to perform a differential database backup, by doing exactly what we did in step 2 to bring up the backup screen. (In Enterprise Manager, right click on the Northwind Database, then choose "All Tasks" and "Backup Database").

 

7) The backup screen comes up. Perform a differential backup. Choose the Northwind Database and name the backup "Northwind Differential". For a backup, choose "Database - Differential". Choose your backup device and append to the device, then click on the "Options" tab so we can verify our backup.

Screen Shot of SQL Server Backup Screen - provide a database name, backup name, database backup type (differential), backup destination, and click the OK button

 

8) Verify the backup as performed in step 4. Check the "Verify Backup Upon Completion" box, then click on the "OK" button to start the backup.

 

9) The full and differential backups are now complete. We're going to insert a row to store in our transaction log backup. Do the following within Query Analyzer.

Query Analyzer screen shot of inserting a row into the shippers table in the Northwind database and querying from the table

 

10) Perform a transaction log backup, by doing exactly what we did in step 2 to bring up the backup screen. (In Enterprise Manager, right click on the Northwind Database, then choose "All Tasks" and "Backup Database").

 

11) Now perform the Transaction Log backup. Choose Northwind as the database, and name the backup "Northwind TRANSLOG". Select "Transaction Log" for the backup type. Then choose your backup device and append to the media. Click on the "Options" tab to verify the backup.

Screen Shot of SQL Server Backup Screen - provide a database name, backup name, database backup type (trans log), backup destination, and click the OK button

 

12) Verify the backup as performed in step 4. Check the "Verify Backup Upon Completion" box, then click on the "OK" button to start the backup.

 

13) Add one more row

Query Analyzer screen shot of inserting a row into the shippers table in the Northwind database and querying from the table

 

14) Stop SQL Server by right clicking on the SQL Server icon in the systray. Choose "MSSQLServer-Stop". Then click 'YES' when you are asked if you are sure that you want to do this.

Screen shot of shutting down database by right clicking on SQL Server icon in the systray.

 

15) Delete the Northwind secondary data file (or files). That we set up in the setup portion of this exercise.  We deleted the file located at C:\Program Files\Microsoft SQL Server\MSSQL\Data\Northwind_Secondary1_Data.ndf. The data file is now completely gone.

 

16) Start SQL Server by doing what was shown in step 5 only select 'MSSQLServer-Start'.

 

17) In Enterprise Manager, we can see that the database is suspect as shown below.  In a typical restore and recovery situation, we would try to determine exactly what the issue is.  We know that we are missing a secondary data file.  Since we are missing a secondary data file, and not a primary data file, we will first backup the transaction log.  Right click on the Northwind database then choose "All Tasks" and "Backup Database" as shown below.

Screen shot of right clicking on the suspect Northwind database in Enterprise Manager, choosing All Tasks then selecting backup database

 

18) From the "General" tab, name your backup, take all of the defaults and click the "OK" button.

Screen Shot of SQL Server Backup Screen - provide a database name, backup name, database backup type (trans log), backup destination, and click the OK button

 

19) Now we can start the restoration process.  In Enterprise Manager, right click on the "Databases" folder, then choose "All Tasks" "Restore Database".

 

Right clicking on suspect Northwind database in Enterprise Manager, selecting All Tasks and Restore Database

 

20) The "Restore Database" screen pops up. Choose "Restore as database" Northwind. Choose the "Database" radio button. In the "Parameters" section, choose Northwind for the "Show backups of database" item. Four backups are shown as indicated in the image below. By default, SQL Server wants to restore our full backup, followed by the differential backup, followed by the two transaction log backups. These are hierarchical, note that the size of a Full database backup is much larger than a differential, which is significantly larger than a transaction log backup. Normally, we would just click on the "OK" button at this point to do a full restore and recovery. But we're going to do an incremental recovery.

Screen shot of restore database screen - a full restore path has been shown to us

 

21) Scroll down to the TRANLOG2 backup and uncheck it. Then click on the "Options" tab.

Screen shot of Restore database screen - unchecking the last transaction log restore option and clicking on the options tab

 

22) On the options tab, check "Leave database read-only and able to restore additional transaction logs. This will allow us to see how the incremental restore is progressing. Click ok and the Full and differential logs are restored.

Restore Database screen shot - on options tab, clicking the leave database read-only and able to restore additional transaction logs option - then clicking ok

 

23) In query analyzer, check our status. Note that the full and differential backups have been successfully restored.

Query analzyer screen shot querying the shippers table showing that our restore is part way through

 

24) Restore the remaining transaction log. Repeat step 19 to bring up the "Restore Database" screen. We're going to take all of the defaults. SQL Server recommends that we now restore the transaction log, which we will do. Also, if you go to the Options tab, you will see that the radio button "Leave database operational. No additional transaction logs can be restored" has been checked. This is what we want. Hit the "OK" button on either screen and our restore is ready for access.

Screen shot of restore database menu - selecting last tranasction log and clicking the OK button to bring us to a full restore

 

25) Validate that the restore is complete by selecting from the shippers table

Query analzyer screen shot querying the shippers table showing that our restore is now finished

The restore is complete.

 

(back to top)

T-SQL Steps:

1) In Query Analyzer, insert a row into the shippers table and then query the table to make sure that our insert was accepted

Query Analyzer screen shot of inserting a row into the shippers table in the Northwind database and querying from the table

 

2) Still in Query Analyzer, back up the database using the following command.

Query Analyzer screen shot showing the backing up of the Northwind database using the BACKUP DATABASE statement

 

3) Insert another row into the shippers table as shown below.

Query Analyzer screen shot of inserting a row into the shippers table in the Northwind database and querying from the table

 

4) Create a differential backup to record the change that was made in the previous step.

Query Analyzer screen shot showing the backing up of the Northwind database using the BACKUP DATABASE statement with the DIFFERENTIAL option

 

5) Add a final row to the table.

Query Analyzer screen shot of inserting a row into the shippers table in the Northwind database and querying from the table

 

6) Perform a transaction log backup as shown below

Query Analyzer screen shot showing the backing up of the Northwind database transaction logs using the BACKUP LOG statement

 

7) Add one more row

Query Analyzer screen shot of inserting a row into the shippers table in the Northwind database and querying from the table

 

8) Shut down the database

Query Analyzer screen shot of shutting down the database using SHUTDOWN WITH NOWAIT

 

9) Delete the file that we added during the setup portion of this exercise, then restart the database by right clicking on the SQL Server icon in the systray and choosing "MSSQLServer - Start"

 

10) In Enterprise Manager, you will notice that the database is labeled as suspect.  Normally we would go through troubleshooting steps to determine what the problem is, but in our case we know that we have lost a secondary data file.

Enterprise Manager screen shot showing that the Northwind database is suspect

 

11) Since we still have a primary data file and a transaction log, we will be able to back the transaction log up so that we can preserve any transactions that were made since the last database backup.  Enter the following command in SQL Server so that we will be able to restore our fourth insert.

Query Analyzer screen shot showing the backing up of the Northwind database transaction logs using the BACKUP LOG statement with the NO_TRUNCATE option since the database may be damaged

 

12) Now we need to do the restore.  We need to find out what files to restore from our tsqlbackup device.  The position column is the key column because it will tell us where within the device each of our backups lie.  Our full has position 1, followed by our differential at position 2, and our two transaction log backups at positions three and four.

Screen shot of using the RESTORE HEADERONLY statement in query analyzer to get the file numbers needed for the RESTORE statement

 

Key columns to be concerned about with the RESTORE HEADERONLY command (for further information see Books Online):

  • BackupName - Name that we provided our backup.  Note if you are backing up files with the same name to the same device, you may have repeats in this column, the position column will tell you which is the most recent (the highest number in the position column provides the most recent backup)
  • BackupType - 1=Database, 2=Transaction Log, 4=File, 5=Differential Database, 6=Differential File
  • Position - Position of the backup set in the device
  • BackupStartDate (not shown in image) - When backup started
  • BackupFinishDate (not shown in image) - When backup finished

13) While connected to the master database in query analyzer, perform an incremental restore by issuing the following statements.

Screen shot of using the RESTORE DATABASE statement with the STANDBY option to do a partial recovery and leave the database in a read only state

The use of STANDBY allows the database to be opened in read only mode, but still allows us to apply more transaction logs. We are using the file values provided in the query results from step 11. We have restored the full and differential backups and are able to query the results since we are in read only mode.

14) Query the database just to check our progress - you wouldn't normally do this, but we're just restoring incrementally in this exercise.  You'll notice that the first three inserts have been successfully restored.

Query analzyer screen shot querying the shippers table showing that our restore is part way through

 

15) Finally, we restore the last transaction log.

Query Analyzer screen shot of using the RESTORE LOG statement to restore the final log and complete the recovery

16) Query the database to ensure everything has been restored to the way it was.

Query analzyer screen shot querying the shippers table showing that our restore is now complete

Our restore has completed successfully.

(back to top)