0
Posted on
Sunday, January 22, 2017
by
醉·醉·鱼
and labeled under
sql
总是不喜欢UI,点得慢死了~
USE Master;
GO
SET NOCOUNT ON
-- 1 - Variable declaration
DECLARE @dbName sysname
DECLARE @backupPath NVARCHAR(500)
DECLARE @dbDataPath NVARCHAR(500)
DECLARE @cmd NVARCHAR(500)
DECLARE @fileList TABLE (backupFile NVARCHAR(255))
DECLARE @lastFullBackup NVARCHAR(500)
DECLARE @backupFile NVARCHAR(500)
DECLARE @NoExec bit
DECLARE @debug bit
-- 2 - Initialize variables
SET @dbName = 'KEY_WORD_IN_YOUR_BACKUP_FILE'
SET @backupPath = 'D:\'
SET @dbDataPath = 'D:\Data\'
SET @NoExec = 1
SET @debug = 1
-- 3 - get list of files
SET @cmd = 'DIR /b ' + @backupPath
INSERT INTO @fileList(backupFile)
EXEC master.sys.xp_cmdshell @cmd
-- 4 - Find latest full backup
SELECT @lastFullBackup = MAX(backupFile)
FROM @fileList
WHERE backupFile LIKE '%.BAK'
AND backupFile LIKE 'Servlet.' + @dbName + '%'
SET @cmd = 'RESTORE DATABASE ' + @dbName + ' FROM DISK = '''
+ @backupPath + @lastFullBackup + '''' + CHAR(10) + 'WITH RECOVERY, REPLACE,' + CHAR(10)
+ 'MOVE N''RECNETSTARTUP_dat'' TO N''' + @dbDataPath + @dbName + '.mdf'',' + CHAR(10)
+ 'MOVE N''RECNETSTARTUP_log'' TO N''' + @dbDataPath + @dbName + '_log.LDF'', NOUNLOAD, STATS = 5'
IF @debug = 1
PRINT @cmd
IF @NoExec <> 1
EXEC (@cmd)
Post a Comment