SQL Server Backup Failed or the Restore Is Stuck

⚡Work out which of three incidents you have, because they share nothing. A BACKUP that failed, a RESTORE that failed, and a restore that is still running and might be stuck are three separate problems with three separate first moves. If the last line you have is 3013, you do not have the error yet, you have its companion. The real one is the line above it. And if a restore is sitting there, read percent_complete before you kill anything, because a restore at 100 percent is normally still working.

Which Message Do You Have?

Find the line you are holding, then take the jump on its band. Backup messages and restore messages sit in the same numeric range and look alike at a glance, which is why so many of these tickets start in the wrong place.

MessageWhat it means
All you have is 3013 Step 1: the line above 3013
3013, is terminating abnormallyA companion line, no cause in it
The backup failed Step 2: backup messages
3201, operating system error 5Denied to the service account, not to you
3201, operating system error 2 or 3The path does not exist to the server
3202, operating system error 112The target filled up mid-write
18204 and 3041, error log onlyThe job kept the summary, not the cause
The restore failed Step 3: restore messages
3154, holds a backup of a database other thanThe wrong database is being overwritten
3169, backed up on a server running database versionThe backup is from a newer build
4305, 4326, 3159The chain, not the file
5133 and 3156, use WITH MOVEThose paths do not exist here
No error, it is still running Step 4: stuck or slow
The restore is just sitting thereOne column says whether it is moving
It finished, the database is not usable Step 5: after it finished
927, in the middle of a restoreWaiting for you, not broken
In Recovery, Recovery Pending, RestoringFour states, three different answers

This page routes. Each step is one check and one hand-off to the post that owns the answer, so you are never reading background while a restore is running. Every error number on this site is decoded in SQL Server Errors: The Complete Guide.


STEP1

Step 1: The Real Error Is the Line Above 3013

Error 3013 is the most reported and least useful message in the whole backup and restore stack. Its text is literally a template, %hs is terminating abnormally, filled in with BACKUP DATABASE or RESTORE DATABASE. It tells you the statement stopped. It never tells you why, because that is always the message immediately before it.

Here is a backup failing on this instance. Two messages, and only one of them is worth reading.

A failed backup, both messagesexample output, not something to copy
Msg 3201, Level 16, State 1, Server HPAI01, Line 2 Cannot open backup device ‘D:\MSSQL\Lab1001_bkp\no_such_folder\master.bak’. Operating system error 3(The system cannot find the path specified.). Msg 3013, Level 16, State 1, Server HPAI01, Line 2 BACKUP DATABASE is terminating abnormally.

3201 names the device and the operating system error. 3013 adds nothing. If 3013 is all anyone has sent you, go and find the line above it, and the three places it will be are the Messages pane of whoever ran it, the output of the Agent job step, or the SQL Server error log. The full entry, including the 3241 variant that means the backup file itself is not what RESTORE expected, is RESTORE Is Terminating Abnormally (Errors 3013 and 3241).

Once you have the real message, go to Step 2 if it was a backup, Step 3 if it was a restore, and Step 4 if there is no error because nothing has finished yet.


STEP2

Step 2: Backup Failures, by Message

3201 with operating system error 5, access is denied. This is the one that wastes the most time, because the person testing it can reach the path perfectly well from Explorer. The backup is written by the SQL Server service account, not by you, so the only account whose permissions matter is the one the engine is running as. Cannot Open Backup Device: Operating System Error 5 is the full entry and covers the UNC share case, where the share permissions and the NTFS permissions both have to allow the service account.

3201 with operating system error 2 or 3. Not a permission problem. The path does not exist from where the engine is standing, which usually means a mapped drive letter that only exists in somebody’s desktop session, or a folder that was renamed. The capture in Step 1 is this case.

3202, write failed, operating system error 112. The target filled up during the backup. That is a space incident rather than a backup incident, and SQL Server Disk Is Full or the Log Will Not Truncate is the page for it.

Nothing useful in the job history. An Agent job step often reports only the summary, and the summary is 3041, which is as empty as 3013. The error log has both halves. Here is the same failure as Step 1, read back out of the error log.

The same failure in the SQL Server error log, same secondexample output, not something to copy
09:46:58 spid69 Error: 18204, Severity: 16, State: 1. 09:46:58 spid69 BackupDiskFile::CreateMedia: Backup device ‘D:\MSSQL\Lab1001_bkp\no_such_folder\master.bak’ failed to create. Operating system error 3(The system cannot find the path specified.). 09:46:58 Backup Error: 3041, Severity: 16, State: 1. 09:46:58 Backup BACKUP failed to complete the command BACKUP DATABASE master. Check the backup application log for detailed messages.

Read it bottom up. 3041 is the summary the job reported. 18204 directly above it is the cause, and it names the device and the operating system error exactly as 3201 did in the session. That is the pattern for every backup failure logged by the engine, including the ones raised by third party tools through VDI and VSS, where the engine records that the device failed and the vendor log records why.

Once it is fixed, prove it rather than assume it. Get Database Backup History reads what actually completed from msdb, Get Last Database Backup Times gives one row per database with how old each backup is, and Get Backup Coverage is the version that answers the question a manager asks, which is whether anything is uncovered.


STEP3

Step 3: Restore Failures, by Message

A restore fails for one of four reasons, and the message separates them cleanly. None of them are fixed by running the restore again.

3154, the backup set holds a backup of a database other than the existing one. The engine is refusing to overwrite a database with a backup that did not come from it, which is a guard rather than a fault. Either you have the wrong file, or you meant to restore into a copy and typed the production name, or you genuinely do want to overwrite and have to say so. The Backup Set Holds a Backup of a Database Other Than the Existing Database covers all three and why WITH REPLACE is the one you should be slowest to reach for.

3169, the database was backed up on a server running database version. Backups move forward only. A 2022 backup restores on 2025, and nothing restores downward, no matter how the compatibility level is set. The number in the message is an internal database version, not a build number, so the first job is working out which release wrote the file, and Restore From a Newer SQL Server Version (Error 3169) decodes that from the two numbers in the message. Every way out of this is some form of moving the data rather than the file.

4305, 4326 or 3159. The files are fine, the chain is not. 4305 means this log backup starts after the point the database has reached, 4326 means it ends before it, and 3159 means the database you are overwriting has a tail that has not been backed up. The LSNs quoted in the message tell you which end of the gap you are on, and The Log in This Backup Set Is Too Recent or Too Early to Apply (Errors 4305, 4326 and 3159) finds the break for you, because a backup job that is green every night still only proves the files were written, not that the log backups between them form an unbroken chain.

5133 and 3156. The restore is trying to put the files back exactly where they were on the source server, and that path does not exist here.

Restoring to paths that do not exist on this serverexample output, not something to copy
Msg 5133, Level 16, State 1, Server HPAI01, Line 2 Directory lookup for the file “D:\MSSQL\no_such_folder\Lab1001_paths.mdf” failed with the operating system error 2(The system cannot find the file specified.). Msg 3156, Level 16, State 3, Server HPAI01, Line 2 File ‘Lab1001_bkp’ cannot be restored to ‘D:\MSSQL\no_such_folder\Lab1001_paths.mdf’. Use WITH MOVE to identify a valid location for the file. Msg 3119, Level 16, State 1, Server HPAI01, Line 2 Problems were identified while planning for the RESTORE statement. Previous messages provide details. Msg 3013, Level 16, State 1, Server HPAI01, Line 2 RESTORE DATABASE is terminating abnormally.

The same 5133 and 3156 pair repeats once for every file in the backup set, so a database with eight files produces sixteen messages and one 3119 at the end. Nothing has been written at this point, which is what “planning for the RESTORE statement” means. Generate Restore With Move Script reads the file list out of the backup and writes the WITH MOVE clauses for you, which is the part people get wrong by hand on a database with more than two files. Generate Backup and Restore Scripts is the wider version for moving a set of databases, and How to Restore a Database in SQL Server is the walkthrough if this is the first one you have done. The options are documented on MS Docs under RESTORE statements.


STEP4

Step 4: Is It Stuck, or Just Slow?

If there is no error and the session is still running, you are not debugging anything yet. You are deciding whether to wait. One query answers it, and it answers it for backups as well as restores.

percent_complete moving at all means the restore is working. estimated_completion_time is milliseconds remaining and it is a rough extrapolation, so treat it as an order of magnitude rather than a promise. The number that matters most is whether the percentage changed since you last looked.

SELECT  r.session_id,
        r.command,
        CAST(r.percent_complete AS DECIMAL(5,1))        AS pct_done,
        r.total_elapsed_time / 1000                     AS elapsed_s,
        r.estimated_completion_time / 1000              AS est_remaining_s,
        r.wait_type,
        LEFT(t.text, 46)                                AS statement_text
FROM    sys.dm_exec_requests AS r
CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) AS t
WHERE   r.command IN ('BACKUP DATABASE','RESTORE DATABASE','BACKUP LOG','RESTORE LOG');

Here is that query sampled once a second through a real restore of a 1.4 GB database on this instance, with a few rows dropped to keep it readable.

One restore, sampled once a secondexample output, not something to copy
sample_at session_id command pct_done elapsed_s est_remaining_s wait_type ——— ———- —————- ——– ——— ————— ———— 09:50:04 68 RESTORE DATABASE 10.0 1 7 BACKUPTHREAD 09:50:06 68 RESTORE DATABASE 20.2 3 11 BACKUPTHREAD 09:50:09 68 RESTORE DATABASE 32.0 5 10 BACKUPTHREAD 09:50:12 68 RESTORE DATABASE 46.2 8 9 BACKUPTHREAD 09:50:15 68 RESTORE DATABASE 65.0 11 6 BACKUPTHREAD 09:50:18 68 RESTORE DATABASE 80.5 14 3 BACKUPTHREAD 09:50:21 68 RESTORE DATABASE 95.7 17 0 BACKUPTHREAD 09:50:23 68 RESTORE DATABASE 100.0 20 0 BACKUPTHREAD 09:50:26 68 RESTORE DATABASE 100.0 22 0 BACKUPTHREAD 09:50:29 68 RESTORE DATABASE 100.0 25 0 BACKUPTHREAD 09:50:32 68 RESTORE DATABASE 100.0 28 0 BACKUPTHREAD

Read the last four rows, because they are the whole reason people kill restores that were about to finish. The statement reported RESTORE DATABASE successfully processed 176354 pages in 28.802 seconds, and it sat at 100.0 percent for the last 9 of those 28.8 seconds, nearly a third of the total. The percentage tracks the data copy. Everything after the copy, which is the undo and redo work that makes the database consistent, has no percentage at all and reports 100 the whole way through. On a database with a long open transaction at backup time that phase can run for considerably longer than the copy did.

estimated_completion_time behaves the way estimates behave. It went up before it came down, because at 10 percent the engine had one second of history to extrapolate from. It is useful in the middle of a long restore and misleading at both ends.

For the versions of this you want on a regular basis rather than at three in the morning, Get Backup Restore Progress is this check with the statement text and the elapsed and remaining times formatted, and Get Backup Restore Duration Estimate answers the question before you start, by reading how long the same database has taken historically. The column itself is documented on MS Docs under sys.dm_exec_requests.

If the percentage has genuinely not moved for several minutes, look at wait_type in the same row before killing anything. BACKUPTHREAD is the restore working normally. A lock wait means something else is holding the database, which Step 5 covers, and killing a restore part way through leaves you with the state Step 5 opens on.


STEP5

Step 5: It Finished and the Database Still Is Not Usable

A restore that completed and a database that is online are two different events, and the gap between them is where most of the panic in this incident lives. One column settles it, and it is worth running before you conclude anything at all.

SELECT name, state_desc, recovery_model_desc, is_in_standby
FROM   sys.databases
WHERE  name = N'YourDatabase';
Restored WITH NORECOVERY, then recoveredexample output, not something to copy
name state_desc recovery_model_desc ———– ———- ——————- Lab1001_bkp ONLINE FULL Lab1001_rst RESTORING FULL Msg 927, Level 14, State 2, Server HPAI01, Line 1 Database ‘Lab1001_rst’ cannot be opened. It is in the middle of a restore. RESTORE DATABASE successfully processed 0 pages in 0.354 seconds (0.000 MB/sec). name state_desc recovery_model_desc ———– ———- ——————- Lab1001_rst ONLINE FULL

That is the whole sequence. The restore succeeded, the database sat in RESTORING, every query against it raised 927, and a single RESTORE DATABASE Lab1001_rst WITH RECOVERY processing zero pages in 0.354 seconds brought it online. Nothing was wrong at any point.

state_descWhat it meansWhat to do
RESTORINGThe last restore ran WITH NORECOVERY and the database is waiting for more files, or for you to say there are no moreApply the remaining log backups, then WITH RECOVERY on the last one. Full entry: in the middle of a restore (927)
RECOVERINGThe engine is working on it right now, usually rolling transactions forward or back after a restart or a restoreWait, and find out how long it has left. Why Is the Database in In Recovery Mode reads the progress out of the error log
RECOVERY_PENDINGRecovery could not start, almost always because a file is missing or unreadable. Documented, not reproduced hereDatabase in Recovery Pending (Error 945) has the error log lines that name the file
OFFLINESomebody took it offline deliberately, which is not a restore state at allQueries raise 942 rather than 927, which is how you tell these apart. ALTER DATABASE ... SET ONLINE
ONLINEDoneCheck the row counts, not the message

Two things worth knowing before you reach for the state table. A database in RESTORING is not damaged and cannot be fixed by restarting the instance, and restarting is the single most common wrong move at this point. And the difference between RECOVERING and RECOVERY_PENDING is the difference between waiting and acting, so read it rather than guessing. MS Docs on restore and recovery covers the phases behind these states.

When it is online, Get Last Restore History records what was restored, from which file and by whom, which is what the incident write-up needs and what nobody can remember a week later.


STEP6

Step 6: Prove the Chain Before the Next One

The incident ends when the database is online. The job ends when you can show this will not happen the same way again, and there are four checks that do it.

Get Backup Chain Integrity walks the backup history and finds the breaks, which is the only way to learn about a broken chain before you need it rather than during a restore. Get Backup Coverage answers the blunter question of which databases have no recent backup at all, and Get Recovery Model Audit finds the databases sitting in FULL recovery with nobody taking log backups, which is both a log that will fill and a restore that cannot roll forward.

A backup that has never been restored is a file, not a backup. A job finishing green says the file was written, not that it restores, not that the process works end to end, and not that anyone on the team can run it under pressure, and Get Last Restore History is the proof either way, because it shows whether anything has actually been restored on this instance and when. Rehearse it on a quiet afternoon and time it, which is also where the Step 4 estimate comes from instead of a guess made during an outage. If the plan itself is the thing in doubt, Differential vs Log Backup covers what each one actually restores and how the chain breaks, and Recovery Models Explained is the decision underneath all of it.


Frequently Asked Questions

The restore is stuck at 100 percent. Is it hung?
Almost certainly not. percent_complete tracks the copy of data out of the backup file, and the recovery phase that follows it has no percentage, so the column reports 100 for the whole of it. In the capture in Step 4 the restore sat at exactly 100.0 for 9 seconds of a 28.8 second run, and the database was fine. Check wait_type in the same row instead: BACKUPTHREAD is work in progress. Killing a restore at this point is how a quick incident becomes a long one.
The backup failed with 3013. What is the real error?
The line directly above it. 3013 is a template, %hs is terminating abnormally, with BACKUP DATABASE or RESTORE DATABASE substituted in, and it contains no cause by design. If the job history only shows 3013 or its summary cousin 3041, read the SQL Server error log for the same timestamp, where the cause is logged immediately above as 3201, 18204 or similar. Step 1 and Step 2 show both halves of the same failure.
Cannot restore, the database is in use. How do I get everyone off it?
Set it to single user with an immediate rollback, run the restore, and set it back to multi user afterwards. Do not restart the instance for this, and do not kill sessions one at a time while the application reconnects faster than you can. If this is the production database you are overwriting, 3159 may be waiting for you on the other side of it, because the tail of its log has not been backed up yet, which is covered in Step 3.
How long will the restore take?
The honest answer before it starts is however long it took last time for the same database on the same hardware, which is why Get Backup Restore Duration Estimate reads the history rather than guessing. Once it is running, estimated_completion_time in Step 4 gives you an order of magnitude, and it is at its least reliable in the first few seconds and once the copy finishes. Add time for the recovery phase, which no estimate covers.
The restore said it succeeded but the database still says Restoring.
That is WITH NORECOVERY doing exactly what it was told. The database is holding the door open for more log backups, and until you run RESTORE DATABASE <name> WITH RECOVERY it will refuse every query with error 927. Nothing is damaged and restarting the instance will not change it. Step 5 has the sequence, and the full entry for 927 covers the case where the files you were expecting do not exist.
Can I restore a backup from a newer version of SQL Server?
No, and no setting changes that. Error 3169 is the engine telling you the backup was written by a newer database version than it supports, and compatibility level has nothing to do with it, because it is set inside the database you cannot open yet. The ways round it all involve moving the data rather than the file: script the schema and copy the rows across, or rebuild the target on a release at least as new as the source. Restore From a Newer SQL Server Version (Error 3169) walks through both routes in full.
I can reach the backup folder in Explorer, so why is it access denied?
Because you are not the one writing the file. The SQL Server service account is, and it is usually a managed service account or a domain account with no interactive rights anywhere. On a UNC path both the share permissions and the NTFS permissions have to allow that account. Cannot Open Backup Device: Operating System Error 5 is the full entry, including how to find out which account the service is actually running as.

Related

Comments

Leave a Reply

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