I backed up a database:
BACKUP DATABASE MyDatabase
TO DISK = \'MyDatabase.bak\'
WITH INIT --overwrite existing
And then tried to restore it
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
Here's how you do it:
RESTORE DATABASE {DatabaseName}
FROM DISK = '{databasename}.bak'
WITH REPLACE, RECOVERY
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:
NORECOVERY
option, when you have a DIFF backup. No LOG backup are allowed in SIMPLE recovery model database. NORECOVERY
option, 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.
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;
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.