Conclusion
Using RESTORE FILELISTONLY + MOVE allows you to restore a backup file to a new database name, preventing overwriting the original DB, and also lets you re-specify the MDF / LDF paths.
Where It’s Useful
- Restoring production backups to a test environment
- Duplicating a database for query analysis
- Creating a test copy before a release
- When the original DB cannot be touched, but data validation is needed
Steps
1. Verify Backup File Content and Version
First, check if the .bak file is usable and if the backup type is correct. If there are multiple backups, you can also identify which one to restore.
RESTORE HEADERONLYFROM DISK = N'C:\SampleBackup\DemoDb_backup_2026_04_17_163005.bak';If you see BackupType = 1, it usually indicates a full backup, which can be restored directly.
2. Query Data File Logical Names
When restoring, MOVE uses the logical names within the backup, not the physical file names. This step is crucial; otherwise, errors often occur.
RESTORE FILELISTONLYFROM DISK = N'C:\SampleBackup\DemoDb_backup_2026_04_17_163005.bak';GOYou will see something similar to:
| LogicalName | Type |
|---|---|
| DemoDb | D |
| DemoDb_log | L |
These two names will then be used with MOVE.
3. Restore to a New Database Name
Create a new database DemoDb_Copy260417 and place the data files in a new path to avoid conflicts with the original DB.
RESTORE DATABASE [DemoDb_Copy260417]FROM DISK = N'C:\SampleBackup\DemoDb_backup_2026_04_17_163005.bak'WITH FILE = 1, MOVE N'DemoDb' TO N'D:\MSSQL\Data\DemoDb_Copy260417.mdf', MOVE N'DemoDb_log' TO N'D:\MSSQL\Log\DemoDb_Copy260417.ldf', RECOVERY, STATS = 10;GOSTATS = 10 means progress will be displayed every 10%. Just as people appreciate updates, so does SQL.
Additional Notes
FILE = 1typically refers to the first backup set; if a single .bak file contains multiple backups, you need to useHEADERONLYfirst to confirm the Position.- If the MDF / LDF paths do not exist, the restore will fail. Create the folders first.
- If the target DB already exists, you can delete it first or add
REPLACE(use with caution in production environments).
Command / Example Summary
Click to expand
RESTORE HEADERONLYFROM DISK = N'C:\SampleBackup\DemoDb_backup_2026_04_17_163005.bak';
RESTORE FILELISTONLYFROM DISK = N'C:\SampleBackup\DemoDb_backup_2026_04_17_163005.bak';GO
RESTORE DATABASE [DemoDb_Copy260417]FROM DISK = N'C:\SampleBackup\DemoDb_backup_2026_04_17_163005.bak'WITH FILE = 1, MOVE N'DemoDb' TO N'D:\MSSQL\Data\DemoDb_Copy260417.mdf', MOVE N'DemoDb_log' TO N'D:\MSSQL\Log\DemoDb_Copy260417.ldf', RECOVERY, STATS = 10;GOWrap-up
Restoring a database isn’t difficult; the challenge lies in getting the logical file names and paths wrong. Clarify them before proceeding to avoid many pitfalls.