Sdílet prostřednictvím


Restoring Renamed Files and Filegroups

The name of a file or filegroup can be changed and even reused elsewhere. The SQL Server Database Engine tracks files and filegroups internally by globally unique identifiers (GUIDs). In most situations, a restore operation can recognize that a name has changed, and the operation handles the change correctly.

How restore handles a changed name is affected by whether a file is being restored offline or online.

  • During an offline restore, the database catalog is not accessible, including the current list of filegroups. Restore matches the name given against the file or filegroup names in the backup. Therefore, if the name has been reused since the backup was created, the wrong file might be restored. However, restore later drops any files that do not appear in the database at the recovery point. This causes additional restore and roll forward activity, but the database is correct. This extra work is unavoidable, because restore does not know the recovery point in advance and cannot predict which files are present in the database at an unknown point.

  • During an online restore, the database catalog is accessible. If the new file or filegroup name is given at restore time, the Database Engine gets the corresponding GUID from the database catalog and restores the file or filegroup with that GUID, if it exists in the backup.

    If the old name is given, it is not in the catalog. If the GUID matches the GUID of a current file or filegroup in the backup, the Database Engine restores that file or filegroup from the backup.

    If the GUID from the backup does not match any of the current files or filegroups, an error occurs.

How Restore Handles a Reused File or Filegroup Name

The name of a dropped file or filegroup can be later reused; for example, the name of a former filegroup might be reused for a new file. This is similar to the renaming of a file or filegroup. However, when you reuse a name, the same name refers to different objects at different times.

Restore automatically handles changes in the number and names of files and filegroups. To simplify restore procedures, a best practice is to take a full database backup of new or changed files and filegroups soon after they change.

If a file has been renamed and its name reused since its last backup, the Database Engine handles it based on whether the database catalog is accessible and whether the name is in it:

  • If the database catalog is accessible and the name is in the catalog, the Database Engine determines the GUID of the corresponding file or filegroup and restores the file or filegroup with that GUID from the backup. If the name does not match one of the current files or filegroups, an error occurs.

  • If the catalog is not accessible, because the database is being restored offline, the Database Engine determines the object to restore from the information in the backup, as described earlier in this topic.

The Database Engine might prevent you from reusing a name until you take a log backup. In this case, an error is displayed.