I am used to getting requests from users to restore Test databases with that of PROD. This could be ad-hoc or on schedule. The below script takes inputs as DB Name, Prod Backup location and backupfile name and restores the Test db. I have made modifications to suit my requirement.
------------------------------------------------------------------------------------------ Restore database from a backup file-- NOTE: If the database gets locked in Single user mode, execute-- the below command on it and it will restore the Multiuser mode:-- ALTER DATABASE yourdatabasename SET MULTI_USER-- Modify the @databaseName, @restoreDirectory & @backupFileName as per your requirement.---------------------------------------------------------------------------------------USE masterGO-- Declare the variablesDECLARE @backupFileName varchar(100),@restoreDirectory varchar(100),@databaseDataFilename varchar(100),@databaseLogFilename varchar(100),@databaseDataFile varchar(100),@databaseLogFile varchar(100),@databaseName varchar(100),@execSql nvarchar(1000)-- Set the name of the database to restoreSET @databaseName = 'YOUR_DATABASE_NAME'-- Set the path to the directory containing the database backup file.-- IMPORTANT:: Make sure there is a \ character at the end of your directory path as below;SET @restoreDirectory ='LOCATION OF BACKUPFILE' -- such as '\\NETWORK\BKUP\DB\'-- Create the backup file name based on the restore directory, the database name and today's date & time.-- The formatting has been done as per my backup file requirement.SET @backupFileName = @restoreDirectory + @databaseName +'_backup_'+convert(varchar,getdate(), 112)+'.bak'PRINT @backupFileName-- Get the data file and its pathSELECT @databaseDataFile =RTRIM([Name]),@databaseDataFilename =RTRIM([Filename])FROM master.dbo.sysaltfiles AS filesINNER JOIN master.dbo.sysfilegroups AS groupsON files.groupID = groups.groupIDWHERE dbid = ( SELECT dbid FROM master.dbo.sysdatabases WHERE [Name] = @databaseName)-- Get the log file and its pathSELECT @databaseLogFile = rtrim([Name]),@databaseLogFilename = rtrim([Filename])FROM master.dbo.sysaltfiles AS filesWHERE dbid =( SELECT dbid FROM master.dbo.sysdatabases WHERE [Name] = @databaseName)ANDgroupID = 0PRINT 'Killing active connections to the "'+ @databaseName + '" database'-- Create the sql to kill the active database connectionsSET @execSql = ' 'SELECT @execSql = @execSql +'kill '+convert(char(10), spid)+ ' 'FROM master.dbo.sysprocessesWHERE db_name(dbid)= @databaseNameAND dbid<> 0 AND spid <> @@spid-- EXEC (@execSql)PRINT 'Restoring "'+ @databaseName +'" database from "'+ @backupFileName + '" with 'PRINT ' DATA file "'+ @databaseDataFile +'" located at "'+ @databaseDataFilename +'"'PRINT ' LOG file "'+ @databaseLogFile +'" located at "'+ @databaseLogFilename + '"'SET @execSql = 'restore database [' + @databaseName + ']from disk = ''' + @backupFileName + '''withfile = 1, move ''' + @databaseDataFile +''' to '+''''+ @databaseDataFilename + ''',move ''' + @databaseLogFile +''' to '+''''+ @databaseLogFilename + ''',norewind, nounload, replace'PRINT @execsqlEXEC sp_executesql @execSql---------------End of Restore script---------------------------------------MvM
No comments:
Post a Comment