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.
Five Causes, One Closing Line
| What was wrong | The real error | What it tells you |
|---|---|---|
| The file is not a backup at all | 3241 | Wrong file, a download that failed, or a backup written by something else. The file is unreadable as a backup. |
| The backup file is truncated | 3287 | The copy did not finish. Different error to 3241, and it names the file ID it could not read. |
| The target path is not usable | 3634 then 3156 | Nothing wrong with the backup. The folder does not exist or the service account cannot write there. |
| The database is in use | 3102 | Nothing wrong with anything. You are connected to the database you are restoring over. |
| Backup is of a different database | 3154 | Right 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.
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.
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.
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.
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.
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:
HEADERONLYagainst the unreadable file returned the same 3241, followed byRESTORE 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
sqlcmdand read the whole output. - In PowerShell, capture the verbose stream, because
Invoke-Sqlcmdputs 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 VERIFYONLYon the one you have. It takes seconds and answers the question. - Do not add
WITH REPLACEto 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?
My tool only shows 3013. Now what?
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?
What is the difference between 3241 and 3287?
Can I check a backup without restoring it?
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?
Related Scripts
- The Backup Set Holds a Backup of a Database Other Than the Existing One (3154), case 5 in full
- Cannot Open Backup Device, when the restore cannot even reach the file
- Backup & Recovery scripts, coverage and restore readiness before you need them
- SQL Server Errors: The Complete Guide, the index for the whole error series
Leave a Reply