SQL Server Disaster Recovery

You are here: Home » Exercises » SQL Server 2000 » Full Database Recovery

SQL Server 2000 Full Database Recovery

Perform SQL Server Restore and Recovery Using Database Backup, Differential Backups, and Transaction Logs

Description:

We will end up destroying all data and transaction log files 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 files will simulate a loss of the disks in which the files were 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

(back to top)

Setup:

None.

(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

Screen shot in query analyzer of adding a row to the shippers table in the Northwind database using the INSERT command

 

 

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".

Screen shot in Enterprise Manager, right clicking on the Northwind database, then choosing all tasks, then 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). Then choose the Append to media radio button. Finally, click on the "OK" button to perform the backup.

Screen shot of the backup database screen - provide a database name, a backup name, backup type, destination, and click OK

 

4) 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.

Screen Shot from query analyzer - Inserting another row into the shippers table using the INSERT statement

 

5) 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").

 

6) 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". Click on the "OK" button to perform the backup. (You could click on the options tab if you wished to verify the backup as shown below, but we're just going to move along and click the "OK" button)

 

Screen shot of the backup database screen - provide a database name, a backup name, backup type(differential), destination, and click OK

 

 

7) 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.

Screen Shot from query analyzer - Inserting another row into the shippers table using the INSERT statement

 

8) Prepare to 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").

 

 

9) 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 "OK" button to perform the backup.

Screen shot of the backup database screen - provide a database name, a backup name, backup type (Transaction Log), destination, and click OK

 

 

10) We are going to simulate the loss of a data file and transaction log file by actually deleting both files from the database. In Enterprise Manager, right click on the Northwind database and choose "Properties" (from the first pop-up menu which is pictured in step 2. Check under the Data Files and Transaction Log tabs to find the files we need to delete. (Only the Data Files tab is pictured below).

Screen shot in Enterprise manager - Viewing the Northwind Database Data Files tab

 

 

11) 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 the database by right clicking on the SQL Server icon in the systray

.

 

12) Delete the Northwind data files and transaction log files that we found the file locations for earlier in this exercise. (or files). All database files are now completely gone. The backup that we took earlier in this exercise must be restored in order to get our data back.

 

13) Start SQL Server by doing what was shown in step 11 only select 'MSSQLServer-Start'

14) In a typical restore and recovery situation, we would try to determine exactly what the issue is. If you're using the full recovery model, you'd also want to take a transaction log backup right now. In our case, we know we are missing a data file, so we are going to start our restore process. In Enterprise manager, right click on "Databases" then choose "All Tasks" and "Restore Database"

Screen Shot in Enterprise Manager - Right Clicking on Databases, then All Tasks, Then choosing restore database

 

 

15) 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. Three 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 transaction log backup. 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 - various backups have been presented to us

 

 

16) Uncheck the transaction log option - we're going to restore the transaction log later. Keep the Full and differential backups checked. Then click on the options tab.

Screen shot of Restore database screen - unchecking the transaction log backup restore

 

17) 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.

Screen Shot of Restore database options tab - Choosing to put database into a read only state so we can apply the transaction log backup later - clicking OK

 

 

18) In query analyzer, check our status. Note that the full and differential backups have been successfully restored. Make sure you aren't connected to Northwind in Query Analyzer so that we may complete our restore.

Query Analzyer screen shot showing querying of shippers table indicating that all but one of the rows have been restored

 

 

19) Restore the remaining transaction log. Repeat step 14 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 screen, selecting last transaction log backup and clicking the OK button

 

 

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

Screen shot of Query Analyzer - querying the shippers table - all of the rows have been successfully restored

 

 

The restore is complete.

(back to top)

T-SQL 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 - showing a row being inserted into the shippers table and the table being queried

 

 

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

Query Analyzer screen shot - backing up the database using the backup database statement

 

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

Query Analyzer screen shot - showing a row being inserted into the shippers table and the table being queried

 

 

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

Query Analyzer screen shot - backing up the database using the backup database statement and the DIFFERENTIAL option

 

 

5) Add a final row to the table.

Query Analyzer screen shot - showing a row being inserted into the shippers table and the table being queried

 

 

6) Perform a transaction log backup as shown below

Query Analyzer Screen shot - back up northwind transaction log

 

7) While connected to Northwind in Query Analyzer, determine which file is the data file that we will delete in the next step by using exec sp_helpfile. In our example below, the top file is the data file. We know it is a data file, not only because of its name, but also because transaction logs can NEVER be part of a filegroup. In addition, the files by default end in .mdf or .ndf and log files end in .ldf (this can be altered by the DBA however, so be careful). You can just expand the filename column to see the full path. We're going to delete both files.

Query Analyzer screen shot - using the exec sp_helpfile command to find the file locations for Northwind data and transaction log files

 

 

8) In Query Analyzer, shut down the database.

Query Analyzer screen shot - using the SHUTDOWN WITH NOWAIT command to shutdown the database

 

 

9) Delete both files in the OS.

10) You can restart the server by right clicking on the SQL Server icon in the systray and clicking "MSSQL Server - Start".

11)Normally at this point we would go through the steps to determine what has happened to Northwind. In our case we know we have to restore the data file that we just deleted. The tricky part is finding the value for the FILE parameter shown in the next step. Use the RESTORE HEADERONLY on our backup device TSQLBACKUP to find what file numbers we need (from the position column)

Query Analyzer screen shot, using the RESTORE HEADERONLY command to find the right data file to restore

 

We now know the file numbers for each of the backups that we performed. This will allow us to perform our incremental restore

 

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

Screen Shot in Query Analyzer - using the RESTORE DATABASE command to restore the full and differential backups

 

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.  Querying the results at this point will show that the first two rows that we added (IDs 22 and 23) have been restored, but shipperID 24 has yet to be restored.

 

13) Finish the restore operation

Screen shot of Query Analyzer, applying the transaction log backup to complete the restore operation

 

 

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

Screen shot in query analyzer - Querying the shippers table in the Northwind database showing that all of our rows have been restored

 

Our restore has completed successfully.

(back to top)