SQL Server: Database stuck in “Restoring” state

前端 未结 26 1863
后悔当初
后悔当初 2020-11-28 17:09

I backed up a database:

BACKUP DATABASE MyDatabase
TO DISK = \'MyDatabase.bak\'
WITH INIT --overwrite existing

And then tried to restore it

相关标签:
26条回答
  • 2020-11-28 17:29

    Ran into a similar issue while restoring the database using SQL server management studio and it got stuck into restoring mode. After several hours of issue tracking, the following query worked for me. The following query restores the database from an existing backup to a previous state. I believe, the catch is the to have the .mdf and .log file in the same directory.

    RESTORE DATABASE aqua_lc_availability
    FROM DISK = 'path to .bak file'
    WITH RECOVERY
    
    0 讨论(0)
  • 2020-11-28 17:31

    Here's how you do it:

    1. Stop the service (MSSQLSERVER);
    2. Rename or delete the Database and Log files (C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data...) or wherever you have the files;
    3. Start the service (MSSQLSERVER);
    4. Delete the database with problem;
    5. Restore the database again.
    0 讨论(0)
  • 2020-11-28 17:31
    RESTORE DATABASE {DatabaseName}
       FROM DISK = '{databasename}.bak'
       WITH REPLACE, RECOVERY
    
    0 讨论(0)
  • 2020-11-28 17:36

    By default, every RESTORE DATABASE comes with RECOVERY set up. The 'NORECOVERY' options, basically tells the SQL Server that the database is waiting for more restore files (could be a DIFF file and LOG file and, could include tail-log backup file, if possible). The 'RECOVERY' options, finish all transactions and let the database ready to perform transactions.

    So:

    1. if your database is set up with SIMPLE recovery model, you can only perform a FULL restore with NORECOVERY option, when you have a DIFF backup. No LOG backup are allowed in SIMPLE recovery model database.
    2. Otherwise, if your database is set up with FULL or BULK-LOGGED recovery model, you can perform a FULL restore followed by NORECOVERYoption, then perform a DIFF followed by NORECOVERY, and, at last, perform LOG restore with RECOVERY option.

    Remember, THE LAST RESTORE QUERY MUST HAVE RECOVERY OPTION. It could be an explicit way or not. In therms of T-SQL, the situation:

    1.

     USE [master]
        GO
        RESTORE DATABASE Database_name 
        FROM DISK = N'\\path_of_backup_file.bak WITH FILE = 1, [REPLACE],NOUNLOAD, 
        RECOVERY -- This option could be omitted.
        GO
    

    WITH REPLACE option must be used with caution as it can lead to data loss

    Or, if you perform a FULL and DIFF backup, you can use this

       USE [master]
        GO
        RESTORE DATABASE Database_name
          FROM DISK = N'\\path_of_backup_file.bak' WITH FILE = 1, 
           NOUNLOAD,NORECOVERY
        GO
        RESTORE DATABASE Database_name
          FROM DISK =N'\\path_of_**diff**backup_file.bak' WITH FILE = 1, 
         NOUNLOAD, RECOVERY
        GO
    
     2. USE [master]
        GO
       -- Perform a Tail-Log backup, if possible. 
       BACKUP LOG Database_name
       GO
       -- Restoring a FULL backup
       RESTORE DATABASE Database_name
        FROM DISK = N'\\path_of_backup_file.bak' WITH FILE = 1, 
         NOUNLOAD,NORECOVERY
      GO 
      -- Restore the last DIFF backup
      RESTORE DATABASE Database_name
        FROM DISK = N'\\path_of_DIFF_backup_file.bak' WITH FILE = 1,
         NORECOVERY,NOUNLOAD
      GO
      -- Restore a Log backup
      RESTORE LOG Database_name
        FROM DISK = N'path_of_LOG_backup_file.trn' WITH FILE = 2,
        RECOVERY, NOUNLOAD
      GO
    

    Of course, you can perform a restore with the option STATS = 10 that tells the SQL Server to report every 10% completed.

    If you prefer, you can observe the process or restore in real-time based query. As follow:

    USE[master]
    GO
    SELECT session_id AS SPID, command, a.text AS Query, start_time, percent_complete, dateadd(second,estimated_completion_time/1000, getdate()) as estimated_completion_time 
        FROM sys.dm_exec_requests r CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) a 
            WHERE r.command in ('BACKUP DATABASE','RESTORE DATABASE')
    GO
    

    Hope this help.

    0 讨论(0)
  • 2020-11-28 17:36

    If you want to restore an SQL Server database from a backup file, you can use the following script:

    RESTORE DATABASE [MyDatabase] -- which database to restore
    FROM DISK = N'X:\MyDatabase.bak' -- location of the database backup
    WITH 
        FILE = 1, -- restore from a backup file
        -- declare where the file groups should be located (can be more than two)
        MOVE N'MyDatabase_Data' TO N'D:\SSDPATH\MyDatabase.mdf',
        MOVE N'MyDatabase_Log' TO N'E:\HDDPATH\MyDatabase.ldf',
        -- Tape option; only relevant if you backup from magnetic tape
        NOUNLOAD,
        -- brings the database online after the database got restored
        -- use this option when you don't want to restore incremental backups
        -- use NORECOVERY when you want to restore differential and incremental backup files
        RECOVERY,
        -- replace existing database with the backup 
        -- deletes the existing database
        REPLACE, 
        -- print log message for every 1 percent of restore
        STATS = 1;
    
    0 讨论(0)
  • 2020-11-28 17:37

    I figured out why.

    If the client who issued the RESTORE DATABASE command disconnects during the restore, the restore will be stuck.

    It's odd that the server, when told to restore a database by a client connection, will not finish the restore unless the client stays connected the entire time.

    0 讨论(0)
提交回复
热议问题