357 字
2 分鐘
瀏覽次數
MSSQL RESTORE DATABASE Hands-on: Restore to a New DB Using FILE + MOVE
2026-04-17

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 HEADERONLY
FROM 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 FILELISTONLY
FROM DISK = N'C:\SampleBackup\DemoDb_backup_2026_04_17_163005.bak';
GO

You will see something similar to:

LogicalNameType
DemoDbD
DemoDb_logL

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;
GO

STATS = 10 means progress will be displayed every 10%. Just as people appreciate updates, so does SQL.

Additional Notes#

  • FILE = 1 typically refers to the first backup set; if a single .bak file contains multiple backups, you need to use HEADERONLY first 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 HEADERONLY
FROM DISK = N'C:\SampleBackup\DemoDb_backup_2026_04_17_163005.bak';
RESTORE FILELISTONLY
FROM 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;
GO

Wrap-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.

MSSQL RESTORE DATABASE Hands-on: Restore to a New DB Using FILE + MOVE
https://joyceowo.github.io/posts/en/345a78ea09fa808a8d27cf9bb32b8b92/
作者
JoyceOwO
發佈於
2026-04-17
許可協議
CC BY-NC-SA 4.0