RESTORE Is Terminating Abnormally (Errors 3013 and 3241)

🚨Part of the SQL Server Errors series, the exact messages and what actually causes them.

The message you were givenMsg 3013  ·  Level 16
RESTORE DATABASE is terminating abnormally.
The error that actually mattersMsg 3241  ·  Level 16  ·  one of several
The media family on device ‘D:\Backups\sales.bak’ is incorrectly formed. SQL Server cannot process this media family.
3013 is never the cause. It is the last line of a failed restore, and the real error is immediately above it. If your tool only showed you 3013, go and find the full message output, because the answer is in the line you did not see.

3013 is one of the most searched SQL Server errors and one of the least informative. It says the restore stopped. It never says why. Everything useful is in the message printed just before it, and plenty of tools, job histories and application logs show only the last line.

To pin down what actually appears above it, I broke a restore five different ways in a throwaway container and captured the real output each time. Five different causes, five different errors, and 3013 was identical in every one.

How this was tested A disposable SQL Server 2022 CU26 (16.0.4265.3) container. A real database, a real backup, then five deliberate ways of failing to restore it, plus the same backup restoring cleanly to prove the file was good all along. Everything below is captured output, not an example.

Five Causes, One Closing Line

What was wrongThe real errorWhat it tells you
The file is not a backup at all3241Wrong file, a download that failed, or a backup written by something else. The file is unreadable as a backup.
The backup file is truncated3287The copy did not finish. Different error to 3241, and it names the file ID it could not read.
The target path is not usable3634 then 3156Nothing wrong with the backup. The folder does not exist or the service account cannot write there.
The database is in use3102Nothing wrong with anything. You are connected to the database you are restoring over.
Backup is of a different database3154Right file, wrong target, or a target that already exists under that name.

Only two of those five are a problem with the backup file. The other three are a problem with where you are restoring it to, or what you are connected to, and those are free to fix. Treating every 3013 as “the backup is corrupt” sends you looking for a new backup when the one you have is fine.

Case 13241, the file is not a backup
Msg 3241, Level 16, State 1, Line 1
The media family on device '/var/opt/mssql/data/garbage.bak' is incorrectly formed.
SQL Server cannot process this media family.
Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.

This is the pairing people mean when they say “3013”. In practice it is a file that never finished copying, a backup taken by a third-party tool that writes its own format, or simply the wrong file. It is also what you get restoring a backup from a newer version of SQL Server to an older one, which is worth ruling out early because it looks identical to corruption.

Case 23287, the file is a backup but not all of it
Msg 3287, Level 16, State 1, Line 1
The file ID 1 on device '/var/opt/mssql/data/truncated.bak' is incorrectly formed
and can not be read.
Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.

A genuinely different error from 3241, produced here by cutting a good backup short. SQL Server recognised the header and then ran out of file. If you see 3287, compare the file size against the source before concluding anything about corruption. This is usually a copy that was interrupted.

Case 33634 and 3156, and SQL Server telling you outright to read up
Msg 3634, Level 16, State 1, Line 1
The operating system returned the error '5(Access is denied.)' while attempting
'RestoreContainer::ValidateTargetForCreation' on '/no/such/dir/x.mdf'.
Msg 3156, Level 16, State 5, Line 1
File 'RestoreDemo' cannot be restored to '/no/such/dir/x.mdf'.
Use WITH MOVE to identify a valid location for the file.
...
Problems were identified while planning for the RESTORE statement.
Previous messages provide details.
Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.

Read the second to last line. “Previous messages provide details” is SQL Server saying, in its own words, that 3013 is not the answer and the answer is further up. Note also that this case produced four errors before the closing line, one pair per file. The backup here was perfectly good.

Case 43102, you are standing in it
Msg 3102, Level 16, State 1, Line 1
RESTORE cannot process database 'RestoreDemo' because it is in use by this session.
It is recommended that the master database be used when performing this operation.
Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.

The most embarrassing and probably the most common. Your query window is connected to the database you are restoring over. The fix is in the message: connect to master first. Other sessions holding the database produce a similar refusal, which is what SINGLE_USER WITH ROLLBACK IMMEDIATE is for.

Case 53154, right backup, wrong target
Msg 3154, Level 16, State 4, Line 1
The backup set holds a backup of a database other than the existing 'Other' database.
Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.

Restoring one database’s backup over a different existing database. This one has a page of its own, because the safe fix and the dangerous fix look very similar.


Ask the Backup File Directly

Before restoring anything, two read-only statements tell you whether the file is the problem. Neither writes anything and neither needs the target database.

-- What is actually in this file? Database name, size, LSNs, version it came from.
RESTORE HEADERONLY FROM DISK = 'D:\Backups\sales.bak';

-- The logical file names, which is what WITH MOVE needs.
RESTORE FILELISTONLY FROM DISK = 'D:\Backups\sales.bak';

-- Read the whole backup and check it, without restoring it.
RESTORE VERIFYONLY FROM DISK = 'D:\Backups\sales.bak';

This splits the diagnosis in one step. Run HEADERONLY against the file that failed:

  • It fails too, and the problem is the file. Confirmed in testing: HEADERONLY against the unreadable file returned the same 3241, followed by RESTORE HEADERONLY is terminating abnormally.
  • It succeeds, and the file is fine. Your problem is the target, the paths, or the session, which is three of the five cases above.

Note that 3013’s text follows the statement you ran. It says RESTORE HEADERONLY is terminating abnormally there rather than RESTORE DATABASE, which is a small confirmation that it is a closing line rather than a diagnosis.

And to close the loop: the same backup that produced four of those five failures restored cleanly the moment it was pointed at a valid target from a session on master.

Processed 344 pages for database 'Restored', file 'RestoreDemo' on file 1.
Processed 2 pages for database 'Restored', file 'RestoreDemo_log' on file 1.
RESTORE DATABASE successfully processed 346 pages in 0.066 seconds (40.897 MB/sec).

Getting the Full Message Text

If all you have is 3013, the useful line was thrown away before it reached you. Where to find it:

  • SQL Agent job history truncates. Open the step’s output file if it has one, or set one, rather than reading the summary.
  • The SQL Server error log has the real error even when the client only showed the last line. Restore failures are written there with their causes.
  • Applications and ORMs often surface only the final message. Rerun the restore by hand in SSMS or sqlcmd and read the whole output.
  • In PowerShell, capture the verbose stream, because Invoke-Sqlcmd puts informational and error detail there rather than in the return value.

What Not To Do

  • Do not conclude the backup is corrupt from a 3013. Three of the five reproduced causes had nothing wrong with the backup file at all.
  • Do not go looking for an older backup first. Run RESTORE VERIFYONLY on the one you have. It takes seconds and answers the question.
  • Do not add WITH REPLACE to make an error go away. It suppresses the safety check behind 3154, and its whole job is to stop you overwriting the wrong database.
  • Do not retry the identical statement. Every case here failed the same way every time. Read the line above the 3013 instead.
  • Do not restore from the database you are replacing. Connect to master. That alone is case 4.

Common Questions

How do I fix error 3013?
You do not, directly. 3013 reports that the restore stopped, and it is the same message whatever went wrong. Find the error printed immediately before it: across five reproduced failures that was 3241, 3287, 3634 with 3156, 3102 or 3154, each with a completely different fix.
My tool only shows 3013. Now what?
Rerun the restore by hand in SSMS or sqlcmd and read the whole output, or open the SQL Server error log, which records the real error even when the client showed only the closing line. Agent job history in particular truncates the useful part.
Does 3241 always mean the backup is corrupt?
Not necessarily. It means SQL Server cannot read the file as a backup, which also covers the wrong file entirely, a copy that never finished, a third-party format, and a backup from a newer version of SQL Server being restored to an older one. That last case is a common surprise and there is nothing wrong with the file.
What is the difference between 3241 and 3287?
3241 is “this is not a backup media family I can process”. 3287 is “this is a backup, and the file ID inside it cannot be read”, which in testing came from a backup that had been truncated. 3287 points more strongly at an interrupted copy, so compare the file size against the source first.
Can I check a backup without restoring it?
Yes, and you should. RESTORE VERIFYONLY reads and checks the whole backup, RESTORE HEADERONLY tells you what is in it, and RESTORE FILELISTONLY gives the logical file names for WITH MOVE. All three are read-only and none need the target database to exist.
Why did I get four errors before the 3013?
Because a restore validates every file. In the reproduced bad-path case, each of the data file and the log file produced a 3634 and a 3156, then a line reading “Problems were identified while planning for the RESTORE statement. Previous messages provide details”, then 3013. SQL Server is explicitly pointing you upwards.

Related Scripts

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *