Search This Blog

Monday, 5 March 2012

SQL Server database scripted restore from backup

SQL Server scripted restore - this is a bit aggressive as it will boot out users and lock the database, so it's best used in an out of ours scheduled basis.


USE [Master];
GO


ALTER DATABASE [MyDatabase] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO


RESTORE DATABASE [MyDatabase] FROM  DISK = N'C:\BAK\MyDatabase.BAK' WITH FILE = 1, MOVE N'MyDatabase_Data' TO N'C:\DB\MyDatabase_2012_01_01.MDF',  MOVE N'MyDatabase_Log' TO N'C:\DB\MyDatabase_2012_01_01.LDF',  NOUNLOAD,  REPLACE,  STATS = 10
GO


ALTER DATABASE [MyDatabase] SET MULTI_USER;
GO


ALTER DATABASE [MyDatabase] SET RECOVERY SIMPLE;
GO


USE [MyDatabase];
GO


DBCC SHRINKFILE (MyDatabase_Log, 64);
GO


ALTER DATABASE [MyDatabase] SET RECOVERY FULL;
GO

Note: STATS indicates the percentage completion that a progress event is raised.  I've not tried it, but I gather these can be captured if you're running this script as an SMO object within .net

No comments:

Post a Comment