还原文件和文件组 (SQL Server)Restore Files and Filegroups (SQL Server)

适用对象:是SQL Server 否Azure SQL 数据库 否Azure Synapse Analytics (SQL DW) 否并行数据仓库 APPLIES TO: yesSQL Server noAzure SQL Database noAzure Synapse Analytics (SQL DW) noParallel Data Warehouse

本主题说明如何使用 SQL Server 2019 (15.x)SQL Server 2019 (15.x)SQL Server Management StudioSQL Server Management StudioTransact-SQLTransact-SQL中还原文件和文件组。This topic describes how to restore files and filegroups in SQL Server 2019 (15.x)SQL Server 2019 (15.x) by using SQL Server Management StudioSQL Server Management Studio or Transact-SQLTransact-SQL.

本主题内容In This Topic

开始之前Before You Begin

限制和局限Limitations and Restrictions

  • 还原文件和文件组的系统管理员必须是唯一一位当前使用要还原的数据库的人。The system administrator restoring the files and filegroups must be the only person currently using the database to be restored.

  • 不允许在显式或隐式事务中使用 RESTORE。RESTORE is not allowed in an explicit or implicit transaction.

  • 在简单恢复模式下,文件必须属于只读文件组。Under the simple recovery model, the file must belong to a read-only filegroup.

  • 在完整恢复模式或大容量日志恢复模式下,必须先备份活动事务日志(称为日志尾部),然后才能还原文件。Under the full or bulk-logged recovery model, before you can restore files, you must back up the active transaction log (known as the tail of the log). 有关详细信息,请参阅 备份事务日志 (SQL Server)数据库还原到一个新位置并且可以选择重命名该数据库。For more information, see Back Up a Transaction Log (SQL Server).

  • 若要还原已加密的数据库,您必须有权访问用于对数据库进行加密的证书或非对称密钥。To restore a database that is encrypted, you must have access to the certificate or asymmetric key that was used to encrypt the database. 如果没有证书或非对称密钥,数据库将无法还原。Without the certificate or asymmetric key, the database cannot be restored. 因此,只要需要该备份,就必须保留用于对数据库加密密钥进行加密的证书。As a result, the certificate that is used to encrypt the database encryption key must be retained as long as the backup is needed. 有关详细信息,请参阅 SQL Server Certificates and Asymmetric KeysFor more information, see SQL Server Certificates and Asymmetric Keys.

SecuritySecurity

权限Permissions

如果不存在要还原的数据库,则用户必须有 CREATE DATABASE 权限才能执行 RESTORE。If the database being restored does not exist, the user must have CREATE DATABASE permissions to be able to execute RESTORE. 如果数据库存在,则 RESTORE 权限默认授予 sysadmindbcreator 固定服务器角色成员以及数据库的所有者 (dbo)(对于 FROM DATABASE_SNAPSHOT 选项,数据库始终存在)。If the database exists, RESTORE permissions default to members of the sysadmin and dbcreator fixed server roles and the owner (dbo) of the database (for the FROM DATABASE_SNAPSHOT option, the database always exists).

RESTORE 权限被授予那些成员身份信息始终可由服务器使用的角色。RESTORE permissions are given to roles in which membership information is always readily available to the server. 因为只有在固定数据库可以访问且没有损坏时(在执行 RESTORE 时并不会总是这样)才能检查固定数据库角色成员身份,所以 db_owner 固定数据库角色成员没有 RESTORE 权限。Because fixed database role membership can be checked only when the database is accessible and undamaged, which is not always the case when RESTORE is executed, members of the db_owner fixed database role do not have RESTORE permissions.

使用 SQL Server Management StudioUsing SQL Server Management Studio

还原文件和文件组To restore files and filegroups

  1. 连接到相应的 SQL Server 数据库引擎SQL Server Database Engine实例之后,在对象资源管理器中,单击服务器名称以展开服务器树。After you connect to the appropriate instance of the SQL Server 数据库引擎SQL Server Database Engine, in Object Explorer, click the server name to expand the server tree.

  2. 展开 “数据库”Expand Databases. 根据具体的数据库,选择一个用户数据库,或展开“系统数据库”并选择一个系统数据库。Depending on the database, either select a user database or expand System Databases, and then select a system database.

  3. 右键单击数据库,指向“任务” ,再单击“还原” 。Right-click the database, point to Tasks, and then click Restore.

  4. 单击 “文件和文件组” ,将打开 “还原文件和文件组” 对话框。Click Files and Filegroups, which opens the Restore Files and Filegroups dialog box.

  5. “常规” 页上的 目标数据库 列表框中,输入要还原的数据库。On the General page, in the To database list box, enter the database to restore. 您可以输入新的数据库,也可以从下拉列表中选择现有的数据库。You can enter a new database or choose an existing database from the drop-down list. 该列表包含了服务器上除系统数据库 mastertempdb之外的所有数据库。The list includes all databases on the server, excluding the system databases master and tempdb.

  6. 若要指定要还原的备份集的源和位置,请单击以下选项之一:To specify the source and location of the backup sets to restore, click one of the following options:

    • 源数据库From database

      在列表框中输入数据库名称。Enter a database name in the list box. 此列表仅包含根据 msdb 备份历史记录已进行过备份的数据库。This list contains only databases that have been backed up according to the msdb backup history.

    • 源设备From device

      单击浏览按钮。Click the browse button. “指定备份设备” 对话框的 “备份介质类型” 列表框中,选择列出的设备类型之一。In the Specify backup devices dialog box, select one of the listed device types in the Backup media type list box. 若要为 “备份介质” 列表框选择一个或多个设备,请单击 “添加”To select one or more devices for the Backup media list box, click Add.

      将所需设备添加到 “备份介质” 列表框后,单击 “确定” 返回到 “常规” 页。After you add the devices you want to the Backup media list box, click OK to return to the General page.

  7. “选择用于还原的备份集” 网格中,选择用于还原的备份。In the Select the backup sets to restore grid, select the backups to restore. 此网格将显示对于指定位置可用的备份。This grid displays the backups available for the specified location. 默认情况下,系统会推荐一个恢复计划。By default, a recovery plan is suggested. 若要覆盖建议的恢复计划,可以更改网格中的选择。To override the suggested recovery plan, you can change the selections in the grid. 任何依赖于已取消选择备份的备份,也将被自动取消选择。Any backups that depend on a deselected backup are deselected automatically.

    列标题Column head Values
    还原Restore 选中的复选框指示要还原的备份集。The selected check boxes indicate the backup sets to be restored.
    名称Name 备份集的名称。The name of the backup set.
    文件类型File Type 指定备份中数据的类型:数据、日志,或 Filestream 数据 。Specifies the type of data in the backup: Data, Log, or Filestream Data. 包含在表中的数据备份在 “数据” 文件中。Data that is contained in tables is in Data files. 事务日志数据备份在 “日志” 文件中。Transaction log data is in Log files. 存储在文件系统上的二进制大型对象 (BLOB) 数据备份在 Filestream 数据 文件中。Binary large object (BLOB) data that is stored on the file system is in Filestream Data files.
    类型 Type 执行的备份类型:“完整”、“差异”或“事务日志” 。The type of backup performed: Full, Differential, or Transaction Log.
    ServerServer 执行备份操作的数据库引擎实例的名称。The name of the Database-Engine instance that performed the backup operation.
    文件逻辑名称File Logical Name 文件的逻辑名称。The logical name of the file.
    DatabaseDatabase 备份操作中涉及的数据库的名称。The name of the database involved in the backup operation.
    开始日期Start Date 备份操作开始的日期和时间,按客户端的区域设置显示。The date and time when the backup operation began, presented in the regional setting of the client.
    完成日期Finish Date 备份操作完成的日期和时间,按客户端的区域设置显示。The date and time when the backup operation finished, presented in the regional setting of the client.
    大小Size 备份集的大小(字节)。The size of the backup set in bytes.
    用户名User Name 执行备份操作的用户的名称。The name of the user who performed the backup operation.
  8. 若要查看或选择高级选项,请在 “选择页” 窗格中单击 “选项”To view or select the advanced options, click Options in the Select a page pane.

  9. “还原选项” 面板中,可以根据您的实际情况选择下列任意选项。In the Restore options panel, you can choose any of the following options, if appropriate for your situation.

    还原为文件组Restore as filegroup
    指示要还原整个文件组。Indicates that an entire filegroup is being restored.

    覆盖现有数据库Overwrite the existing database
    指定还原操作应覆盖所有现有数据库及其相关文件,即使已存在同名的其他数据库或文件。Specifies that the restore operation should overwrite any existing databases and their related files, even if another database or file already exists with the same name.

    选择此选项等效于在 Transact-SQLTransact-SQL RESTORE 语句中使用 REPLACE 选项。Selecting this option is equivalent to using the REPLACE option in a Transact-SQLTransact-SQL RESTORE statement.

    还原每个备份之前进行提示Prompt before restoring each backup
    在还原每个备份设置前要求您进行确认。Asks you for confirmation before restoring each backup set.

    如果对于不同介质集必须更换磁带,例如在服务器具有一个磁带设备时,此选项非常有用。This option is particularly useful where you must swap tapes for different media sets, such as when the server has one tape device.

    限制访问还原的数据库Restrict access to the restored database
    使还原的数据库仅供 db_ownerdbcreatorsysadmin的成员使用。Makes the restored database available only to the members of db_owner, dbcreator, or sysadmin.

    选择此选项等效于在 Transact-SQLTransact-SQL RESTORE 语句中使用 RESTRICTED_USER 选项。Selecting this option is synonymous to using the RESTRICTED_USER option in a Transact-SQLTransact-SQL RESTORE statement.

  10. 还可以通过在 “将数据库文件还原为” 网格中指定每个文件的新还原目标,从而将数据库还原到新的位置。Optionally, you can restore the database to a new location by specifying a new restore destination for each file in the Restore database files as grid.

    列标题Column head Values
    原始文件名Original File Name 源备份文件的完整路径。The full path of a source backup file.
    文件类型File Type 指定备份中数据的类型:数据、日志,或 Filestream 数据 。Specifies the type of data in the backup: Data, Log, or Filestream Data. 包含在表中的数据备份在 “数据” 文件中。Data that is contained in tables is in Data files. 事务日志数据备份在 “日志” 文件中。Transaction log data is in Log files. 存储在文件系统上的二进制大型对象 (BLOB) 数据备份在 Filestream 数据 文件中。Binary large object (BLOB) data that is stored on the file system is in Filestream Data files.
    还原为Restore As 要还原的数据库文件的完整路径。The full path of the database file to be restored. 若要指定新的还原文件,请单击文本框,再编辑建议的路径和文件名。To specify a new restore file, click the text box and edit the suggested path and file name. 更改 “还原为” 列中的路径或文件名等效于在 Transact-SQLTransact-SQL RESTORE 语句中使用 MOVE 选项。Changing the path or file name in the Restore As column is equivalent to using the MOVE option in a Transact-SQLTransact-SQL RESTORE statement.
  11. “恢复状态” 面板确定还原操作之后的数据库状态。The Recovery state panel determines the state of the database after the restore operation.

回退未提交的事务,使数据库处于可以使用的状态。无法还原其他事务日志。(RESTORE WITH RECOVERY)Leave the database ready for use by rolling back the uncommitted transactions. Additional transaction logs cannot be restored. (RESTORE WITH RECOVERY)
恢复数据库。Recovers the database. 此选项为默认行为。This is the default behavior. 请仅在要还原所有必要的备份时选择此选项。Choose this option only if you are restoring all of the necessary backups now. 此选项等效于在 Transact-SQLTransact-SQL RESTORE 语句中指定 WITH RECOVERY。This option is equivalent to specifying WITH RECOVERY in a Transact-SQLTransact-SQL RESTORE statement.

不对数据库执行任何操作,不回退未提交的事务。可以还原其他事务日志。(RESTORE WITH NORECOVERY)Leave the database non-operational, and don't roll back the uncommitted transactions. Additional transaction logs can be restored. (RESTORE WITH NORECOVERY)
使数据库处于还原状态。Leaves the database in the restoring state. 若要恢复数据库,则将需要使用前面的 RESTORE WITH RECOVERY 选项(请参阅上面的内容)来执行另一个还原操作。To recover the database, you will need to perform another restore using the preceding RESTORE WITH RECOVERY option (see above). 此选项等效于在 Transact-SQLTransact-SQL RESTORE 语句中指定 WITH NORECOVERY。This option is equivalent to specifying WITH NORECOVERY in a Transact-SQLTransact-SQL RESTORE statement.

如果选择此选项, “保留复制设置” 选项将不可用。If you select this option, the Preserve replication settings option is unavailable.

使数据库处于只读模式。回滚未提交的事务,但将回滚操作保存在一个文件中,以便可使恢复效果逆转。(RESTORE WITH STANDBY)Leave the database in read-only mode. Roll back the uncommitted transactions, but save the rollback operation in a file so the recovery effects can be undone. (RESTORE WITH STANDBY)
使数据库处于备用状态。Leaves the database in a standby state. 此选项等效于在 Transact-SQLTransact-SQL RESTORE 语句中指定 WITH STANDBY。This option is equivalent to specifying WITH STANDBY in a Transact-SQLTransact-SQL RESTORE statement.

选择此选项需要您指定一个备用文件。Choosing this option requires that you specify a standby file.

回滚撤消文件Rollback undo file
在“回滚撤消文件” 文本框中指定备用文件名称。Specify a standby file name in the Rollback undo file text box. 如果使数据库处于只读模式 (RESTORE WITH STANDBY),则必须选中此选项。This option is required if you leave the database in read-only mode (RESTORE WITH STANDBY).

使用 Transact-SQLUsing Transact-SQL

还原文件和文件组To restore files and filegroups

  1. 执行 RESTORE DATABASE 语句以还原文件和文件组备份,同时指定下列内容:Execute the RESTORE DATABASE statement to restore the file and filegroup backup, specifying:

    • 要还原的数据库的名称。The name of the database to restore.

    • 从中还原完整数据库备份的备份设备。The backup device from where the full database backup will be restored.

    • 每个要还原文件的 FILE 子句。The FILE clause for each file to restore.

    • 每个要还原文件组的 FILEGROUP 子句。The FILEGROUP clause for each filegroup to restore.

    • NORECOVERY 子句。The NORECOVERY clause. 如果在创建备份之后没有对文件进行修改,则指定 RECOVERY 子句。If the files have not been modified after the backup was created, specify the RECOVERY clause.

  2. 如果在创建文件备份之后对文件进行了修改,则执行 RESTORE LOG 语句以应用事务日志备份,同时指定下列内容:If the files have been modified after the file backup was created, execute the RESTORE LOG statement to apply the transaction log backup, specifying:

    • 事务日志将应用到的数据库的名称。The name of the database to which the transaction log will be applied.

    • 要还原的事务日志备份的备份设备。The backup device from where the transaction log backup will be restored.

    • 如果在应用当前事务日志备份之后还要应用其他事务日志备份,则指定 NORECOVERY 子句;否则指定 RECOVERY 子句。The NORECOVERY clause if you have another transaction log backup to apply after the current one; otherwise, specify the RECOVERY clause.

      事务日志备份(如果可用)必须包括截止到日志结尾处的文件和文件组备份(除非已还原所有数据库文件)。The transaction log backups, if applied, must cover the time when the files and filegroups were backed up until the end of log (unless ALL database files are restored).

示例 (Transact-SQL)Example (Transact-SQL)

以下示例将还原 MyDatabase 数据库的文件和文件组。This example restores the files and filegroups for the MyDatabase database. 为了将数据库还原到当前时间,将应用两个事务日志。To restore the database to the current time, two transaction logs are applied.

USE master;  
GO  
-- Restore the files and filesgroups for MyDatabase.  
RESTORE DATABASE MyDatabase  
   FILE = 'MyDatabase_data_1',  
   FILEGROUP = 'new_customers',  
   FILE = 'MyDatabase_data_2',  
   FILEGROUP = 'first_qtr_sales'  
   FROM MyDatabase_1  
   WITH NORECOVERY;  
GO  
-- Apply the first transaction log backup.  
RESTORE LOG MyDatabase  
   FROM MyDatabase_log1  
   WITH NORECOVERY;  
GO  
-- Apply the last transaction log backup.  
RESTORE LOG MyDatabase  
   FROM MyDatabase_log2  
   WITH RECOVERY;  
GO  

另请参阅See Also

Restore a Database Backup Using SSMS Restore a Database Backup Using SSMS
备份文件和文件组 (SQL Server) Back Up Files and Filegroups (SQL Server)
创建完整数据库备份 (SQL Server) Create a Full Database Backup (SQL Server)
备份事务日志 (SQL Server) Back Up a Transaction Log (SQL Server)
还原事务日志备份 (SQL Server) Restore a Transaction Log Backup (SQL Server)
RESTORE (Transact-SQL)RESTORE (Transact-SQL)