Australian cloud for business software — built and run in Sydney since 2002

NetLeverage
NetLeverage

SQL Server restore error 3156: file cannot be restored to a path. Use WITH MOVE to identify a valid location

Technical article
Troubleshooting

About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.

A restore that should be routine stops with a message of the form File '...' cannot be restored to '...'. Use WITH MOVE to identify a valid location for the file., where the dots stand for the file and path names. It can happen when a database is restored onto a different server, or beside the original, and the folder layout in the backup does not match what the target has. Error 3156 does not say which of several things is wrong with the location. It points at the destination and leaves the diagnosis to you.

What the error means

Microsoft gives the message text for error 3156 as "File '%ls' cannot be restored to '%ls'. Use WITH MOVE to identify a valid location for the file." The symbolic name is LDDB_CANT_WRITE. Its explanation: "This general message identifies the logical or physical file names of files that could not be restored because of a problem with the specified location."

The first name in the message is the file as the backup knows it. The second is the path SQL Server tried to write to. A restore writes each file back to the location recorded in the backup unless you tell it otherwise. The MOVE option is how you give each file a different home.

Microsoft calls it a "general message", and its user action begins "Look at the error logs for other messages that provide more details." The detail therefore lives in the error log and in any message reported with 3156.

The remedy follows from the wording. A restore puts each file back where the backup says it was. If that place does not exist on this server, is closed to the service account, or is already in use, the restore cannot proceed, and the message tells you to supply a valid location with WITH MOVE. Microsoft's page ties the error to "a problem with the specified location", not to the contents of the backup.

Likely causes

Microsoft lists three possible causes:

Cause What to check
"You might need access to the specified Windows directory." The SQL Server service account has no permission to write to that folder
"You might have mistyped the path or specified a path that does not exist." The folder, drive letter or path does not exist on the target
"The file name might be being used by a file that cannot be overwritten." Another database or process already has a file of that name open

A second Microsoft page describes a case that produces the same message for a database with a memory-optimized filegroup. "During the process of a database restoration, SQL Server Database Engine will create a folder for a memory-optimized filegroup. This issue occurs if there's already a folder with the same name in the same folder path, and the folder is used by SQL Server or other processes."

Prove the cause before changing anything

All of these only read.

  1. Copy the full text of the error, including the two names. Check the SQL Server error log and the messages above 3156 for a more specific reason, as Microsoft's page says to do.
  2. List the files in the backup and where they would be restored. Microsoft's RESTORE FILELISTONLY returns one row for each file, with the LogicalName and the PhysicalName ("Physical or operating-system name of the file"):
RESTORE FILELISTONLY FROM DISK = N'<backup file path>' WITH FILE = <backup set file number>;

Microsoft says you can find the position of a backup set with RESTORE HEADERONLY.

  1. On the target, compare each PhysicalName with the real folders. Does the drive exist? Does the folder exist? Does the SQL Server service account have rights to it? Is there already a database file or folder with the same name?
  2. Check whether the backup includes a memory-optimized filegroup. If the error names a folder rather than a file, and the same folder name is already used by a database on this instance, you have the case in Microsoft's second page.
  3. If the restore is to the same server beside an existing database, check whether that database's files use the same names.

Example (illustrative): at contoso.com.au an engineer restores FinanceDB.bak onto a new server SQL09 that has only a D: drive. RESTORE FILELISTONLY shows PhysicalName values on E:\SQLData\ and F:\SQLLog\. Neither drive exists on SQL09, which matches the "path that does not exist" cause, so the files must be given new locations with MOVE.

Fix

Microsoft's action is: "Correct the problem with the specified location, for example, by granting access, or use the WITH MOVE option in your RESTORE statement to relocate the file."

Restore with MOVE to a valid location

Save first. Write down the output of this statement for the backup and the folders you plan to use:

RESTORE FILELISTONLY FROM DISK = N'<backup file path>' WITH FILE = <backup set file number>;

Also confirm the target database does not already exist, or, if you are restoring to a new name, that the name is free. Microsoft's restore-to-a-new-location page says that "By default, data and log files are restored to their original locations" and that MOVE relocates each file "to avoid collisions with existing files". It adds that before relocating, you should verify that enough space is available and identify any collisions with existing files, using the RESTORE ... VERIFYONLY statement with the same MOVE parameters.

Risk: The next statement restores a database from a backup and writes new files at the folders you name, using disk space there. Microsoft's pages used here do not describe what happens when the name you restore to is already a database on the instance, so restore under a name that is not in use, as Microsoft's new-location syntax does.

Microsoft's basic syntax for a restore to a new location with a new name is:

RESTORE DATABASE <new_database_name>
FROM DISK = N'<backup file path>'
WITH FILE = <backup set file number>,
 MOVE N'<logical file name in backup>' TO N'<new operating system file name>',
 MOVE N'<logical log file name in backup>' TO N'<new log file name>';

Give one MOVE for every file in the RESTORE FILELISTONLY output, using its LogicalName and a full path on the target. Microsoft's note says that if you restore to a different server instance you can use the original database name.

To undo it: a restore under a new name creates a new database and new files and does not change the database the backup came from. If you do not want the copy, remove the database by the new name you gave it, and the files you named in MOVE; check that name against your note before you remove anything.

The memory-optimized filegroup case

Microsoft's workaround for the folder-collision case: "Use a different folder name or a different folder path when restoring a database." Its example restore script uses MOVE for the data and log files, and a further MOVE for the memory-optimized file with a different path and folder name. The script also includes the Replace keyword, and Microsoft's note says that if the database folder is not in use, the Replace keyword "will ensure that the process of restoration is completed without error." The page does not say what Replace overwrites, so do not add it until you know that no database of that name holds data you need.

Granting access

If the cause is permission, Microsoft names granting access as the remedy. The page names no account and gives no steps. Access is a change to the folder's permissions, so note the folder's existing permissions before anyone changes them.

Confirm the fix

  • The restore completes with no 3156 message.
  • The database comes online, and RESTORE FILELISTONLY paths now match the files on disk.
  • Run your normal application checks against the restored database.

What to watch afterwards

  • The user action on Microsoft's page begins with the error log. Keep the log text from the failed attempt with the ticket, because it holds the operating-system reason that error 3156 does not.
  • Any restore script that relies on the backup's recorded paths. Rewrite it to give a MOVE for each file.
  • Backups of databases with a memory-optimized filegroup. A restore beside the original can collide on the folder name.
  • Free space at the new folders.

Risks and limits

These statements have not been run in a lab. Microsoft's error 3156 page lists three causes and two actions, and gives no steps beyond them. Its memory-optimized page applies to restores that involve a memory-optimized filegroup. Neither page gives a version range. The restore statement shown is Microsoft's syntax with placeholders, not a tested script, so check it against your backup's file list.

Sources