Restoring a Database

Occasional database corruption, hardware failure, and natural disasters do happen, and as a database administrator, you need to be able to restore the database if any of these events occur. Even if you are a pro at backup and restore, keep in mind that restoring a database is different from restoring an operating system or recovering other types of applications. The mix of full, differential, and transaction log backups ensures that you can get up-to-the-minute recovery of a database, but it complicates the recovery process.

In the following subsection, you will find tips and advice on troubleshooting database corruption. After that you will find step-by-step procedures for restoring a database in various situations, including:

  • Restoring a database using backups created in SQL Server Management Studio.

  • Restoring a file or filegroup.

  • Restoring a database to a different location.

  • Restoring a database using Transact-SQL.

Database Corruption and Problem Resolution

All the knowledge you have accumulated as a database administration is most important in one defining moment. That is the moment when you attempt to restore a database. The techniques you use to restore a database depend on the backup options you have used and the state of the database. As you know, the backup techniques available are full, differential, transaction log, and file/filegroups. What you may not know is how to restore a database by combining these techniques.

Table 14-2 lists some suggested recovery strategies for corrupted databases. These strategies show how to recover a database with various combinations of the available backup operations. If you use SQL Server Management Studio for backup and restore, these procedures are done for you automatically in most cases. The actual step-by-step process is covered later in this chapter.

Table 14-2. Recovery Strategies for Databases

Backup Type

Restore Process

Full backups only

Restore the database using the last full backup.

Full and differential backups

Restore the last full backup with NORECOVERY. Then restore the last differential backup with RECOVERY.

Full and transaction log backups

Back up the current transaction log with NO_TRUNCATE. Restore the last full backup with NORECOVERY. Apply log backups from that time forward in sequence, using NORECOVERY. Apply the last differential backup with the RECOVERY option.

Full, differential, and transaction log backups

Back up the current transaction log with NO_TRUNCATE. Restore the last full backup with NORECOVERY and transaction log backups and then the last differential backup with NORECOVERY. Apply log backups from that time forward in sequence, using NORECOVERY. Apply the last backup using the RECOVERY option.

Now you know how to restore a database in theory. But before you begin, you should make sure the database is really corrupt and cannot be recovered by other means. To troubleshoot database problems and potential corruption, complete the following steps:

  1. Start with the SQL Server logs. See what types of error messages are in the logs, paying particular attention to errors that occur during database startup. Also take a look at user-related errors. If you find errors, you can look up the error numbers in the SQL Server Books Online or the Microsoft Online Support Web site (http://search.support.microsoft.com). You access the server logs through the Management folder in SQL Server Management Studio as discussed in Chapter 13.

  2. Check the state of the database. Every time you start SQL Server, it goes through a recovery process on each database. If the recovery process has problems, the mode or state of the database may be abnormal. To check mode or state, use these properties of the databaseproperty function:

    • IsShutDown. If set to 1, the database is shut down because of problems during startup.

    • IsEmergencyMode. If set to 1, the database is in emergency mode, which allows a suspect database to be used.

    • IsSingleUser, IsDboOnly, IsReadOnly, or IsOffline. If set to 1, the database is in a limited or no access mode and needs to be made operational so it can be accessed.

    • IsSuspect. If set to 1, the database is suspect, which means it is possibly corrupted.

    • IsInLoad. If set to 1, the database is going through the loading process.

    • IsInRecovery. If set to 1, the database is going through the recovery process.

    • IsNotRecovered. If set to 1, the database failed to recover and is in an unstable state.

  3. If possible, try to use the DBCC command to further troubleshoot or repair the database. DBCC is covered in Chapter 15.

  4. If these procedures indicate that you have a corrupt database that cannot be repaired, restore the database from backup.

You can use the databaseproperty function as shown in Example 14-5.

Example 14-5. The databaseproperty Function Syntax and Usage

Syntax

databaseproperty('database','property')

Usage

select databaseproperty('Customer','IsEmergencyMode')

Restoring a Database from a Normal Backup

SQL Server Management Studio tracks all the backups you create for each database; when you need to restore a database, SQL Server Management Studio automatically configures the restore. You can restore a database using these default settings or fine-tune the restore operation as necessary.

To restore a database, complete the following steps:

  1. If you are using transaction logs and the database is still running, you should back up the current transaction log with NO_TRUNCATE. When you are using the SQL Server Backup dialog box, this means you should select Back Up The Tail Of The Log on the Options page of the Back Up Database dialog box when performing the transaction log backup.

  2. In SQL Server Management Studio, connect to the appropriate server in Object Explorer view.

  3. Expand the Databases folder. Right-click the database you want to restore. On the shortcut menu, point to Tasks, select Restore, and then select Database. This opens the Restore Database dialog box shown in Figure 14-4.

    The Restore Database dialog box

    Figure 14-4. The Restore Database dialog box

  4. The database currently selected is listed as the To Database in the Destination For Restore area at the top of the dialog box. If you are restoring the database to its original location, leave the database in the To Database box as it is. If you want to restore the database to an alternate location, select a different database to use as the destination or type the name of a new database for the restore operation.

    Note

    Note

    This option is provided to allow you to restore a database to a different location, as described in the subsection titled "Restoring a Database to a Different Location" later in this chapter. All databases on the server are included in the drop-down list as possible values except master and tempdb.

  5. By default, the database is restored to the most recent possible point in time. If multiple backups are available, you may be able to select a point in time for the restore. For example, if you know that GOTEAM accidentally deleted the Accounts table at 12:16 P.M., you could restore the database to a point just prior to this transaction, such as 12:15 P.M. To use the point in time option, click the Properties (...) button to the right of the To A Point In Time text box. This opens the Point In Time Restore dialog box. Choose A Specific Date And Time, select a date and time using the text boxes provided, and then click OK.

    Note

    Note

    Restoring a database from a tape device or other backup device differs from a normal backup. This is primarily because you have to work with backup media (tapes) that may contain multiple backups, as well as multiple backup media sets (tape sets). If you are restoring from a device, select From Device, and then click the related Properties (...) button. You can then use the Specify Backup dialog box to specify the backup media and its location for the restore operation. You can add multiple locations and view the contents of added backup sets as well.

  6. The database currently selected is listed as the From Database under Source For Restore. If you are restoring a different database, choose this database instead. Only databases that have backup history in the msdb are listed.

  7. Use the Select The Backup Sets To Restore option to select the backup set to restore. By default, the last full set (including the last full backup, differential backups since the last full backup, and transaction log backups since the last full backup) should be selected. The selected backups can also represent the most current backup set (according to a recovery plan) that meets the point in time recovery requirements.

    Real World

    Real World

    Normally, you will want to start with the last complete backup set. However, if you know that the last backup set is bad or contains transactions that you do not want to apply, such as a massive table deletion, go back to a previous backup set by selecting a different full backup and its related differential and transaction log backups as the starting point.

  8. The lower portion of the Restore Database dialog box provides a backup history for the selected database. You can use the information in the history as follows:

    • Restore. Allows you to select which backup sets to restore. Default selections are based on the first backup to restore and go forward in time through differential and transaction log backups. You should rarely change the default selections.

    • Name. Indicates the name of the backup set.

    • Component. Shows the backed-up component as Database, File, or a blank entry. A blank entry indicates a transaction log backup.

    • Type. Indicates the type of backup performed as Full, Differential, or Transaction Log.

    • Server. Shows the database engine instance that performed the backup.

    • Database. Displays the name of the database backed up.

    • Position. Shows the position of the backup set in the volume.

    • First LSN. For log backups, this is the log sequence number of the first transaction in the backup set, which helps with ordering transaction logs for the restore operation.

    • Last LSN. For log backups, this is the log sequence number of the last transaction in the backup set, which helps with ordering transaction logs for the restore operation.

    • Checkpoint LSN. For log backups, this is the log sequence number of the most recent checkpoint at the time the backup was created, which helps with ordering transaction logs for the restore operation.

    • Start Date. Displays a date and time stamp that indicates when the backup operation started.

    • Finish Date. Displays a date and time stamp that indicates when the backup operation finished.

    • Size. Shows the size of the backup.

    • User Name. Displays the name of the user who performed the backup operation.

    • Expiration. Indicates the date and time the backup set expires.

  9. Select the Options page to configure options for the restore operation. The Options page is shown in Figure 14-5. You use the available options as follows:

    • Overwrite The Existing Database. Allows the restore operation to overwrite any existing databases and their related files. (This is the same as using RESTORE with the REPLACE option.)

    • Preserve The Replication Settings. Ensures that any replication settings are preserved when restoring a published database to a server other than the server where the database was originally created. You must select Leave The Database Ready For Use By Rolling Back The Uncommitted Transactions option. (This is the same as using RESTORE with the PRESERVE_REPLICATION option.)

    • Prompt Before Restoring Each Backup. Automatically prompts after completing a successful restore and before starting the next restore. The prompt includes a Cancel button, which is useful to cancel the restore operation after a particular backup is restored. This is a good option to use when you need to swap tapes for different media sets.

    • Restrict Access To The Restored Database. Sets the database in restricted-user mode so only the dbo, dbcreator, and sysadmin can access it. (This is the same as using RESTORE with the RESTRICTED_USER option.)

    • Restore Database Files As. Allows you to change the restore location for database files.

    The Options page of the Restore Database dialog box

    Figure 14-5. The Options page of the Restore Database dialog box

  10. Set the recovery state using one of the following options:

    • Leave The Database Ready For Use. Completes the entire restore process and applies all the selected backups, which can include a full backup, a differential backup, and multiple transaction log backups. All completed transactions are applied, and any uncompleted transactions are rolled back. When the restore process is completed, the database is returned to ready status and you can use it for normal operations. (This is the same as using RESTORE WITH RECOVERY.)

    • Leave Database Non-Operational. This is essentially a manual restore that allows you to go step-by-step through the backups. SQL Server completes the entire restore process and applies all the selected backups, which can include a full backup, a differential backup, and multiple transaction log backups. When the restore is completed, the database is not returned to ready status, and you cannot use it for normal operations. All transactions have not been processed, and the database is waiting for you to apply additional transaction logs. Apply these transaction logs using this mode, and then for the last transaction log, set the mode to Leave Database Operational. All completed transactions are then applied, and any uncompleted transactions are rolled back. (This is the same as using RESTORE WITH NORECOVERY.)

    • Leave Database In Read-Only Mode. This is similar to the Leave Database Non-Operational option, with some exceptions. When the restore process ends, the database is in Read-Only mode, and it is ready for additional transaction logs to be applied. In Read-Only mode, you can check the data and test the database. If necessary, apply additional transaction logs. Then for the last transaction log, set the mode to Leave Database Operational. All completed transactions are then applied, and any uncompleted transactions are rolled back. (This is the same as using RESTORE WITH STANDBY.)

    Real World

    Real World

    When you use the option Leave Database In Read-Only Mode, SQL Server also creates an Undo file, which you can use to undo the restore operation. To commit the restore operations and the final transactions without restoring another transaction log, you could use:

    RESTORE DATABASE Customer
    
    WITH RECOVERY

    This commits final transactions (if possible), deletes the Undo file, and puts the database back in operations mode. Although you may want to use WITH RECOVERY at this stage, you probably do not want to use WITH NORECOVERY because you will undo all the changes from the restore and may end up with an empty database.

  11. When you are ready to start the restore operation, click OK. Stop the restore at any time by clicking Stop Action Now. If an error occurs, you will see a prompt with an error message.

Restoring Files and Filegroups

You can restore files and filegroups from database backups or file backups either individually, in combination with each other, or all together. If any changes were made to the files or filegroups, you must also restore all transaction log backups that were created after the files or filegroups were backed up.

Although you can usually recover individual files or filegroups, there are exceptions. If tables and indexes are created that span multiple filegroups, all the related filegroups must be restored together. Do not worry, for SQL Server generates an error prior to starting the restore if a needed filegroup is missing. Further, if the entire database is corrupted, you must restore all files and filegroups in the database. In both cases, you must also apply transaction log backups created after the file or filegroup backups you are restoring.

To restore files or filegroups, complete the following steps:

  1. If you are using transaction logs and the database is still running, you should back up the current transaction log with NO_TRUNCATE. When you are using the SQL Server Backup dialog box, this means you should select Back Up The Tail Of The Log on the Options page of the Back Up Database dialog box when performing the transaction log backup.

  2. In SQL Server Management Studio, connect to the appropriate server in Object Explorer view.

  3. Expand the Databases folder. Right-click the database you want to restore. On the shortcut menu, point to Tasks, select Restore, and then select Files And Filegroup. This opens the Restore Files And Filegroups dialog box, shown in Figure 14-6.

    The Restore Files And Filegroups dialog box

    Figure 14-6. The Restore Files And Filegroups dialog box

  4. The database currently selected is listed as the To Database under Destination To Restore. If you are restoring a file or filegroup to its original database, this is what you want to use. If you want to restore the file or filegroup to a different database, select the different database to use as the destination or type the name of a new database for the restore operation.

    Note

    Note

    This option is provided to allow you to restore a database to a different location, as described in the subsection titled "Restoring a Database to a Different Location" later in this chapter. All databases on the server are listed as possible values except master and tempdb.

  5. The database currently selected is listed as the From Database under Source For Restore. If you are restoring files and filegroups for a different database, choose this database instead. Only databases that have backup history in the msdb are listed.

  6. The lower portion of the Restore Files And Filegroups dialog box provides a backup history for the files and filegroups in the selected database. You can use the information in the history as follows:

    • Restore. The backup files to restore

      Note

      Note

      No default selections are made in the Restore text box; you must choose the files manually.

    • Name. The name of the backup set

    • Type. The type of backup performed as Full, Differential, or Transaction Log

    • Server. The database engine instance that performed the backup

    • File Logical Name. The logical name of the file

    • Database. The name of the file that was backed up

    • Start Date. A date and time stamp indicating when the backup operation started

    • Finish Date. A date and time stamp indicating when the backup operation finished

    • Size. The size of the backup

    • User Name. The name of the user who performed the backup operation

  7. Select the backup files you want to restore.

  8. Select the Options page to configure options for the restore operation. The available options are the same as those discussed in the subsection titled "Restoring a Database from a Normal Backup" earlier in this chapter.

  9. When you are ready to start the restore operation, click OK. You can stop the restore at any time by clicking Stop.

Restoring a Database to a Different Location

When you restore a database to a different location, you are essentially copying the database from backups. If you use this procedure to copy a database to a new location on the same computer, you create a copy of the database that can have separate files and a different database name. Restoring a database to a different location is similar to the process of restoring files and filegroups discussed previously. The main differences are as follows:

  1. On the General page under Destination For Restore, type a new name for the database in the To Database box. For example, if you are restoring the Customer database to a new location, name the copy Customer 2 or CustomerCopy.

  2. When you access the Options page, you must override the default destination paths and enter new destination paths for all of the files you are restoring. Simply click in the Restore As box, and then enter a new file path. Or you can click the related Properties (...) button to select a new Restore As location.

If you use this procedure to copy a database to a different computer, you can create a working copy of the database on another server. You do not need to create a new database or perform any preliminary work, with one exception—if you want to use backup devices on the destination server, you should set them up beforehand. Also, before you begin the restore, you should ensure that the destination computer is using the same code page, sort order, Unicode collation, and Unicode locale as the source server. If these configuration settings are not identical, you will not be able to run the database on the destination server.

Recovering Missing Data

If you suspect part of the database is missing or corrupted, you can perform a partial restore to a new location so that you can recover the missing or corrupted data. To do this, use the PARTIAL option with the RESTORE DATABASE statement as discussed in the subsection titled "Using Transact-SQL Restore Commands" later in this chapter. You can restore partial databases only at the filegroup level. The primary file and filegroup are always restored along with the files that you specify and their corresponding filegroups. Files and filegroups that are not restored are marked as offline and you cannot access them.

To carry out the restore and recovery process, complete the following steps:

  1. Perform a partial database restore. Give the database a new name and location in the RESTORE DATABASE statement and use MOVE/TO to move the original database source files to new locations, such as:

    RESTORE DATABASE new_custdb_partial
    
         FILEGROUP = 'Customers2'
    
         FROM DISK='g:cust.dmp'
    
         WITH FILE=1,NORECOVERY,PARTIAL,
    
         MOVE 'cust' TO 'g:cu2.pri',
    
         MOVE 'cust_log' TO 'g:cu2.log',
    
         MOVE 'cust_data_2' TO 'g:cu2.dat2'
    
    GO
  2. Extract any needed data from the partial restore and insert it into the database from which it was deleted.

Creating Standby Servers

The notion of restoring a backup to a different computer can be extended to create a standby backup server that you can bring online if the primary server fails. When you create a standby server, you have two options:

  • You can create a cold standby that you synchronize manually.

  • Or you can create a warm standby that SQL Server synchronizes automatically.

Creating a Cold Standby

To create a standby that you synchronize manually, complete the following steps:

  1. Install SQL Server on a new server system using an identical configuration. This means that the destination server should use the same code page, sort order, Unicode collation, and Unicode locale as the source server.

  2. Copy all of the databases on the primary server to this new system by specifying a different restore location in the Restore Database dialog box.

  3. Maintain the copies of the databases by periodically applying the transaction log backups from the primary to the standby server.

  4. You may want to leave the standby server in Standby mode so that the database is read-only. This allows users to access the database but not make changes.

If one or more databases on the primary server fail for any reason, you can make the corresponding databases on the standby server available to users. However, before you do this, you should synchronize the primary and the standby servers by completing the following steps:

  1. On the standby server, apply any transaction log backups created on the primary server that have not been applied yet. You must apply these backups in the proper time sequence.

  2. Create a backup of the active transaction log on the primary server and apply this backup to the database on the standby server. This ensures up-to-the-minute synchronization. Be sure to recover the database or specify that the database should be put in operational mode after this backup is applied.

    Tip

    Tip

    If you need to make the standby server appear to be the primary server, you may need to take the primary off the network and rename it. Then rename the standby so that it appears to be the primary.

  3. After you restore the primary server to working condition, any changes to the standby’s databases need to be restored to the primary server. Otherwise, those changes are lost when you start using the primary server again.

Note

Note

Standby servers are not the same as a SQL Server failover cluster, which is created using the SQL Server Failover Cluster Wizard and Microsoft Cluster Service. Standby servers store a second copy of databases on their hard disk drives. Virtual servers use a single copy of databases that is accessed from a shared storage device.

Creating a Warm Standby

SQL Server 2005 Enterprise Edition includes a feature called log shipping. You can use log shipping to create a standby server that is automatically synchronized with the primary server. To do this, follow these steps:

  1. Install SQL Server on a new server system using an identical configuration. This means that the destination server should use the same code page, sort order, Unicode collation, and Unicode locale as the source server.

  2. Copy all of the databases on the primary server to this new system by specifying a different restore location in the Restore Database dialog box.

  3. On the primary server, configure log shipping as described in the section titled "Configuring Log Shipping" in Chapter 15.

The primary server is referred to as the source server. The servers receiving the logs are referred to as destination servers. After configuring log shipping, you should check the status of log shipping on the source and destination servers periodically.

If one or more databases on the primary server fail for any reason, you can make the corresponding databases on the standby available to users. To do that, follow these steps:

  1. Make sure that the most recent logs have been applied by checking the status of log shipping on the destination server.

  2. Take the primary server off the network and rename it.

  3. Rename the standby server so that it appears to be the primary server.

  4. Check connections to the new primary server.

After you restore the primary server to working condition, any changes to the standby’s databases need to be restored to the primary server. Otherwise, those changes are lost when you start using the primary server again.

Using Transact-SQL Restore Commands

You can also restore databases using Transact-SQL. The commands you will use are RESTORE DATABASE and RESTORE LOG. You can use RESTORE DATABASE to restore an entire database, specific files and filegroups, or part of a corrupted database. Example 14-6 shows the syntax and usage for a complete restore. The option WITH RECOVERY is the default mode.

Example 14-6. RESTORE DATABASE Syntax and Usage for a Complete Restore

Syntax

RESTORE DATABASE { database_name | @database_name_var } 
[ FROM <backup_device> [ ,...n ] ]
[ WITH 
    [ { CHECKSUM | NO_CHECKSUM } ]
    [ [ , ] { CONTINUE_AFTER_ERROR | STOP_ON_ERROR } ]
    [ [ , ] FILE = { file_number | @file_number } ] 
    [ [ , ] KEEP_REPLICATION ] 
    [ [ , ] MEDIANAME = { media_name | @media_name_variable } ] 
    [ [ , ] MEDIAPASSWORD = { mediapassword |
                    @mediapassword_variable } ] 
    [ [ , ] MOVE 'logical_file_name' TO 'operating_system_file_name' ] 
                 [ ,...n ] 
    [ [ , ] PASSWORD = { password | @password_variable } ] 

    [ [ , ] { RECOVERY | NORECOVERY | STANDBY = 
          {standby_file_name | @standby_file_name_var } }
    ] 
    [ [ , ] REPLACE ] 
    [ [ , ] RESTART ] 
    [ [ , ] RESTRICTED_USER ] 
    [ [ , ] { REWIND | NOREWIND } ] 
    [ [ , ] STATS [ =percentage ] ] 
       [ [ , ] STOPAT = { date_time | @date_time_var } |
     [ , ] STOPATMARK = { 'mark_name' | 'lsn:lsn_number' }
             [ AFTER datetime ] |
     [ , ] STOPBEFOREMARK = { 'mark_name' | 'lsn:lsn_number' }
             [ AFTER datetime ] 
    ] 
  [ [ , ] { UNLOAD | NOUNLOAD } ] 
]
[;]
<backup_device> ::=
{ { 'logical_backup_device_name' |
            @logical_backup_device_name_var }
   | { DISK | TAPE } = { 'physical_backup_device_name' |
              @physical_backup_device_name_var } }

Usage

RESTORE DATABASE Customer
   FROM TAPE = '\.	ape0'

Usage

RESTORE DATABASE Customer
   FROM Customer_1
   WITH NORECOVERY,
         MOVE 'CustomerData1' TO 'F:mssql7dataNewCust.mdf',
         MOVE 'CustomerLog1' TO 'F:mssql7dataNewCust.ldf'
RESTORE LOG Customer
   FROM CustomerLog1
   WITH RECOVERY

Using RESTORE DATABASE, you also can restore files and filegroups. Example 14-7 shows the related syntax and usage.

Example 14-7. RESTORE DATABASE Syntax and Usage for File and Filegroup Restore

Syntax

RESTORE DATABASE { database_name | @database_name_var } 
     <file_or_filegroup_or_pages> [ ,...f ]
[ FROM <backup_device> [ ,...n ] ]
[ WITH 
   [ { CHECKSUM | NO_CHECKSUM } ]
   [ [ , ] { CONTINUE_AFTER_ERROR | STOP_ON_ERROR } ]
   [ [ , ] FILE = { file_number | @file_number } ] 

   [ [ , ] MEDIANAME = { media_name | @media_name_variable } ] 
   [ [ , ] MEDIAPASSWORD = { mediapassword |
                      @mediapassword_variable } ]
   [ [ , ] MOVE 'logical_file_name' TO 'operating_system_file_name' ] 
                [ ,...n ] 
   [ [ , ] PASSWORD = { password | @password_variable } ] 
   [ [ , ] NORECOVERY ] 
   [ [ , ] REPLACE ] 
   [ [ , ] RESTART ] 
   [ [ , ] RESTRICTED_USER ]
   [ [ , ] { REWIND | NOREWIND } ] 
   [ [ , ] STATS [ =percentage ] ] 
   [ [ , ] { UNLOAD | NOUNLOAD } ] 
]
[;]
<backup_device> ::=
{  { logical_backup_device_name|
            @logical_backup_device_name_var }
   | { DISK | TAPE } = { 'physical_backup_device_name' |
              @physical_backup_device_name_var } } 
<file_or_filegroup_or_pages> ::=
{  FILE = { logical_file_name | @logical_file_name_var }
   | FILEGROUP = { logical_filegroup_name | @logical_filegroup_name_var 
}}
   | PAGE = 'file:page [ ,...p ]'  }

Usage

RESTORE DATABASE Customer
   FILE = 'Customerdata_1',
   FILE = 'Customerdata_2',
   FILEGROUP = 'Primary'
   FROM Customer_1
   WITH NORECOVERY
RESTORE LOG Customer
   FROM CustomerLog1

Example 14-8 shows the syntax for performing a partial restore. This command creates a new database that is based on a partial copy of the backup data. When you use this procedure, the database_name represents the new name for the database, and the MOVE/TO command is used to move the original database source files to new locations.

Example 14-8. RESTORE DATABASE Syntax and Usage for Partial Restore

Syntax

RESTORE DATABASE { database_name | @database_name_var } 
     <files_or_filegroups> 
[ FROM <backup_device> [ ,...n ] ] 
[ WITH 
     { PARTIAL } 

     [ [ , ] { CHECKSUM | NO_CHECKSUM } ]
     [ [ , ] { CONTINUE_AFTER_ERROR | STOP_ON_ERROR } ]
     [ [ , ] FILE = { file_number | @file_number } ] 
     [ [ , ] MEDIANAME = { media_name | @media_name_variable } ]g 
     [ [ , ] MEDIAPASSWORD = { mediapassword |
                        @mediapassword_variable } ] 
     [ [ , ] MOVE 'logical_file_name' TO 'operating_system_file_name' ] 
                  [ ,...n ] 
     [ [ , ] PASSWORD = { password | @password_variable } ] 
     [ [ , ] NORECOVERY ] 
     [ [ , ] REPLACE ] 
     [ [ , ] RESTART ] 
     [ [ , ] RESTRICTED_USER ]
     [ [ , ] { REWIND | NOREWIND } ] 
     [ [ , ] STATS [=percentage ] ] 
     [ [ , ] STOPAT = { date_time | @date_time_var } |
       [ , ] STOPATMARK = { 'mark_name' | 'lsn:lsn_number' }
               [ AFTER datetime ] |
               [ , ] STOPBEFOREMARK = { 'mark_name' | 'lsn:lsn_number' }
               [ AFTER datetime ] 
     ] 
     [ [ , ] { UNLOAD | NOUNLOAD } ] 
]
[;]
<backup_device> ::=
{ { logical_backup_device_name |
            @logical_backup_device_name_var }
  | { DISK | TAPE } = { 'physical_backup_device_name' |
             @physical_backup_device_name_var } } 

<files_or_filegroups> ::=
        { FILE = { logical_file_name | @logical_file_name_var } 
       |
      FILEGROUP = { logical_filegroup_name | @logical_filegroup_name_var }} 
    [ ,...f ]

Usage

RESTORE DATABASE cust_part
   FILEGROUP = 'Customers2'
   FROM DISK='g:cust.dmp'
   WITH FILE=1,NORECOVERY,PARTIAL,
   MOVE 'cust' TO 'g:cu2.pri',
   MOVE 'cust_log' TO 'g:cu2.log',
   MOVE 'cust_data_2' TO 'g:cu2.dat2'
GO
RESTORE LOG cust_part
   FROM DISK = 'g:cust.dmp'
   WITH FILE = 2,RECOVERY
GO

Example 14-9 shows how you can use RESTORE LOG.

Example 14-9. RESTORE LOG Syntax and Usage

Syntax

RESTORE LOG { database_name | @database_name_var } 
     <file_or_filegroup_or_pages> [ ,...f ]
[ FROM <backup_device> [ ,...n ] ] 
[ WITH 
   [ { CHECKSUM | NO_CHECKSUM } ]
   [ [ , ] { CONTINUE_AFTER_ERROR | STOP_ON_ERROR } ]
   [ [ , ] FILE = { file_number | @file_number } ] 
   [ [ , ] KEEP_REPLICATION ] 
   [ [ , ] MEDIANAME = { media_name | @media_name_variable } ] 
   [ [ , ] MEDIAPASSWORD = { mediapassword | @mediapassword_variable }      
]
   [ [ , ] MOVE 'logical_file_name' TO 'operating_system_file_name' ] 
                [ ,...n ] 
   [ [ , ] PASSWORD = { password | @password_variable } ] 
   [ [ , ] { RECOVERY | NORECOVERY | STANDBY = 
          {standby_file_name | @standby_file_name_var } }
   ] 
   [ [ , ] REPLACE ] 
   [ [ , ] RESTART ]
   [ [ , ] RESTRICTED_USER ]
   [ [ , ] { REWIND | NOREWIND } ] 
   [ [ , ] STATS [=percentage ] ] 
   [ [ , ] STOPAT = { date_time | @date_time_var } |
     [ , ] STOPATMARK = { 'mark_name' | 'lsn:lsn_number' }
             [ AFTER datetime ] |
     [ , ] STOPBEFOREMARK = { 'mark_name' | 'lsn:lsn_number' }
             [ AFTER datetime ] 
     ] 
   [ [ , ] { UNLOAD | NOUNLOAD } ] 
]
[;]
<backup_device> ::=
{ { logical_backup_device_name |
            @logical_backup_device_name_var }
   | { DISK | TAPE } = { 'physical_backup_device_name' |
              @physical_backup_device_name_var } } 
<file_or_filegroup_or_pages> ::=
{  FILE = { logical_file_name | @logical_file_name_var }
  | FILEGROUP = { logical_filegroup_name | @logical_filegroup_name_var } 
}
   | PAGE = 'file:page [ ,...p ]'  }

Usage

RESTORE DATABASE Customer
   FROM Customer_1, Customer_2
   WITH NORECOVERY

RESTORE LOG Customer
   FROM CustomerLog1
   WITH NORECOVERY
RESTORE LOG Customer
   FROM CustomerLog2
   WITH RECOVERY, STOPAT = 'Dec 11, 2006 3:30 PM'

Restoring Full-Text Catalogs

In SQL Server 2005, full-text catalogs are treated as files and are included in the database file set for the purposes of backup and restore. When you restore a database, the full-text catalogs are restored automatically. When you restore the filegroup associated with one or more full-text catalogs, the catalogs are restored as well. Changes to full-text catalogs are backed up with standard differential backups of a database or the associated filegroup. Although full-text catalogs are backed up and restored in association with database files, there are also ways to back up and restore only your full-text catalogs.

You can restore a full-text catalog by itself using T-SQL. To do this, you specify the logical file name of the full-text catalog using the FILE clause of the RESTORE statement. Consider the following example:

RESTORE DATABASE Customer

FILE = 'customerdb_cat'

FROM Customer_Catalog

WITH NORECOVERY

RESTORE DATABASE Customer

FILE = 'customerdb_cat'

FROM Customer_Catalog2

In this example, you specify the backup device to use for the restore as Customer_Catalog. You restore the last full backup of the full-text catalog named customerdb_cat. Next, you restore the differential backup of the full-text catalog. It is important to note that the first restore uses the NORECOVERY option to ensure that the database is offline, and the Full-Text Search service is stopped during the restore. After the second restore, however, the database is brought back online and the Full-Text Search service is started.

You can restore multiple catalogs as part of a filegroup as well. To do this, use the FILEGROUP option to specify the logical name of the filegroup to restore, such as:

RESTORE DATABASE Customer

FILEGROUP = 'Catalogs_Primary'

FROM Customer_Catalog

You can restore catalogs to an alternate location using the WITH MOVE option. This option lets you set the root folder location for catalogs being restored. Consider the following example:

RESTORE DATABASE Customer

FROM Customer_Catalog

WITH MOVE 'customerdb_cat' TO 'C:DataCatalogs'

Here, you specify the backup device to use for the restore as Customer_Catalog. You restore the last full backup of the full-text catalog named customerdb_cat to the C:DataCatalogs folders.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset