When you back up a SQL Server database to a file that already holds a backup, SQL Server adds the new backup to the end of the file instead of overwriting it. Each backup in the file gets a position number, and WITH FILE = X on the restore picks which one you get back.
Here’s what happens:
– WITH FILE = 1: Restores the first backup stored in the file.
– WITH FILE = 2: Restores the second backup, and so on.
– No FILE option: Restores position 1, the oldest backup in the file, which is easy to get wrong when you wanted the latest.
This can be useful when you run consecutive backups without changing the file path and want to restore a specific version later.
In the demo below, I’ll walk you through how the WITH FILE = X option works when backing up and restoring a SQL Server database. I’ll also inspect the backup file using RESTORE HEADERONLY to show how that works too.
Demo: Using WITH FILE to Restore Specific Backups
1. Create a Test Database and Insert Data
CREATE DATABASE [backupOverwrite];
GO
USE [backupOverwrite];
GO
DROP TABLE IF EXISTS TestTable;
CREATE TABLE TestTable (aaa VARCHAR(100), bbb VARCHAR(100), ccc VARCHAR(100));
INSERT INTO TestTable VALUES ('1', '1', '1');
SELECT * FROM TestTable;
2. Perform the First Backup
-- backup database to file BACKUP DATABASE backupOverwrite TO DISK = 'C:\sql_Backups\backup_test.bak' WITH COMPRESSION, STATS;

The on file 1 at the end of each Processed line is the backup’s position in the file. Here’s the file on disk:

3. Insert More Data and Backup Again
INSERT INTO TestTable VALUES ('2', '2', '2');
BACKUP DATABASE backupOverwrite TO DISK = 'C:\sql_Backups\backup_test.bak' WITH COMPRESSION, STATS;Same file name, and no INIT or FORMAT option, so the default NOINIT applies and the second backup is appended to the file. The messages now say on file 2, and the .bak has grown:


4. Inspect the Backup File
To see the backup versions inside the file, use RESTORE HEADERONLY:
RESTORE HEADERONLY FROM DISK = N'C:\sql_Backups\backup_test.bak';
Look for the Position column. This shows 1 for the first backup and 2 for the second. BackupFinishDate tells you which is which:

To see the data and log files inside one of the backups, add the position to RESTORE FILELISTONLY. You need the logical names it returns for a restore with MOVE, further down:
RESTORE FILELISTONLY FROM DISK = N'C:\sql_Backups\backup_test.bak' WITH FILE = 2;
5. Restore the First Backup (WITH FILE = 1)
I’m doing this restore using an additional option, REPLACE, which means I don’t need to drop this test database before restoring. Without it, a database in the FULL recovery model stops the restore with error 3159, because the tail of its log hasn’t been backed up.
-- restore test database, first file in backup RESTORE DATABASE backupOverwrite FROM DISK = 'C:\sql_Backups\backup_test.bak' WITH FILE = 1, REPLACE, STATS;

Verify the data. Only the first row is there:
SELECT * FROM backupOverwrite.dbo.TestTable;

6. Restore the Second Backup (WITH FILE = 2)
Again with REPLACE, to overwrite the database restored in step 5:
-- restore test database, second file in backup RESTORE DATABASE backupOverwrite FROM DISK = N'C:\sql_Backups\backup_test.bak' WITH FILE = 2, REPLACE, STATS;

Verify the data again. Both rows are back:

Restore One of the Backups as a New Database
To bring back the second backup alongside the original database, rather than over it, give it a new name and move its files. Without MOVE, SQL Server tries to write to the original database’s files and refuses with error 1834, The file … cannot be overwritten. It is being used by database ‘backupOverwrite’. Use the logical names from RESTORE FILELISTONLY:
RESTORE DATABASE backupOverwrite_copy
FROM DISK = N'C:\sql_Backups\backup_test.bak'
WITH FILE = 2,
MOVE N'backupOverwrite' TO N'C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\DATA\backupOverwrite_copy.mdf',
MOVE N'backupOverwrite_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\DATA\backupOverwrite_copy_log.ldf',
STATS;
SELECT * FROM backupOverwrite_copy.dbo.TestTable;Tested on SQL Server 2025, that restores both rows into backupOverwrite_copy, and the messages end with on file 2. The folder in the MOVE paths is the 2025 default (MSSQL17), so change it to suit your instance. DBA Scripts: Generate Restore WITH MOVE Script on sqldba.blog writes the MOVE lines for you.
Appending to a Backup File or Overwriting It
Appending is the default, so a job that backs up to the same file name every night keeps adding to that file until something clears it. WITH INIT overwrites the backups in the file instead. After a backup WITH INIT, RESTORE HEADERONLY shows a single backup at position 1.
Ask for a position that isn’t there and the restore fails. On the two-backup file, WITH FILE = 3 returns:
Msg 3287, Level 16, State 1, Line 2 The file ID 3 on device 'C:\sql_Backups\backup_test.bak' is incorrectly formed and can not be read. Msg 3013, Level 16, State 1, Line 2 RESTORE DATABASE is terminating abnormally.
Keeping several backups in one file also means one file to lose. If that .bak is deleted or damaged, every backup in it goes with it, so for real backups a new file per backup, with the date in the name, is the safer habit. Appending is handy for a quick before-and-after on a test database, like the demo above.
Conclusion
WITH FILE = X targets one backup in a file that holds several, and RESTORE HEADERONLY shows you the positions to choose from. Leave FILE out and you get position 1. Use WITH REPLACE when restoring over an existing database without dropping it first, and MOVE when restoring it under a new name. For the restore process from start to finish, see How to Restore a Database in SQL Server on sqldba.blog.
Leave a Reply