July 28, 2015 at 5:41 am
I need a foolproof way to run a job that restores a database from server A to server B
Every once in a while, my restore fails, because my attempt to kill connections before the restore doesn't work.
Will this make sure there are no connections, so I can restore :
ALTER DATABASE MyDatabase SET OFFLINE WITH ROLLBACK IMMEDIATE
Or is DROP DATABASE MyDatabase more foolproof ?? What can prevent the DROP DATABASE from working ?
July 28, 2015 at 5:43 am
Drop database will fail if there are connections. Plus if you drop the DB, the restore has to go and recreate the files, which may take some time if the log file is large.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
July 28, 2015 at 5:45 am
I am using RedGate Backup/restore, and have included DISCONNECT_EXISTING, but it does not always work.
My primary concern is reliability of the restore process, with speed secondary.
July 28, 2015 at 5:59 am
homebrew i do something very similar as far as restoring an existing database.
i have a SQL job with the steps to handle the takeover and restore as separate steps; this might help:
--step 1 drop the database: performed in db context of UserAnalysis so i keep the connection
IF EXISTS(SELECT * FROM master.sys.databases WHERE name='UserAnalysis')
BEGIN
ALTER DATABASE [UserAnalysis] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
EXEC sp_renamedb 'UserAnalysis','UserAnalysisDropping';
DROP DATABASE [UserAnalysisDropping]
END
--step 2
RESTORE DATABASE [UserAnalysis]
FROM DISK = 'W:\SQLBackup\CFSDWHD_MonthEnd.bak'
WITH RECOVERY,
MOVE 'CFSDWH' TO 'W:\SQLData\UserAnalysis.mdf',
MOVE 'CFSDWH_log' TO 'W:\SQLLogs\UserAnalysis_1.ldf'
--step 3 housekeeping for logical file names
ALTER DATABASE [UserAnalysis] SET RECOVERY SIMPLE WITH NO_WAIT
ALTER DATABASE [UserAnalysis] MODIFY FILE ( NAME = CFSDWH, NEWNAME = UserAnalysis_data ) ;
ALTER DATABASE [UserAnalysis] MODIFY FILE ( NAME = CFSDWH_log, NEWNAME = UserAnalysis_log ) ;
--step 4
DBCC SHRINKFILE (N'UserAnalysis_Data' , 0, TRUNCATEONLY)
DBCC SHRINKFILE (N'UserAnalysis_log' , 0, TRUNCATEONLY)
Lowell
July 28, 2015 at 6:01 am
Lowell (7/28/2015)
DBCC SHRINKFILE (N'UserAnalysis_Data' , 0, TRUNCATEONLY)DBCC SHRINKFILE (N'UserAnalysis_log' , 0, TRUNCATEONLY)
Wha????
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
July 28, 2015 at 6:05 am
yeah, i know....
it has a purpose, really!
it comes from a big ol data warehouse db that gets copied once a month.; a previous job step deleted a ton of data, since this db, in my case, is supposed to be a static analysis db.
Lowell
July 28, 2015 at 6:09 am
Just bear in mind that TRUNCATEONLY is ignored when shrinking a log file (log records can never be moved within the log), so that may shrink the log to 0.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
July 28, 2015 at 6:13 am
I noticed this in my log file:
"Msg 6107, Sev 14, State 1, Line 1 : Only user processes can be killed."
So there's some system process every once in a while that I can't kill ??
Will this work if there's some system process connected ?
ALTER DATABASE [UserAnalysis] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
EXEC sp_renamedb 'UserAnalysis','UserAnalysisDropping';
DROP DATABASE [UserAnalysisDropping]
July 28, 2015 at 6:16 am
Probably the checkpoint process.
The ALTER might wait for the system process to finish, but it should work.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
July 28, 2015 at 11:35 am
Any opinion on this method ?
ALTER DATABASE MyDatabase SET OFFLINE WITH ROLLBACK IMMEDIATE
October 2, 2015 at 5:14 pm
homebrew01 (7/28/2015)
Any opinion on this method ?
ALTER DATABASE MyDatabase SET OFFLINE WITH ROLLBACK IMMEDIATE
Thanks! That worked for me and I was able to kill unkillable the SPIDs <50 and put it into single user mode in my own process.
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply