zoukankan      html  css  js  c++  java
  • 批量恢复数据库

    USE Master;
    GO
    SET NOCOUNT ON
    -- 1 - Variable declaratioN
    DECLARE @dbName sysname
    DECLARE @backupPath NVARCHAR(500)
    DECLARE @cmd NVARCHAR(500)
    DECLARE @fileList TABLE (backupFile NVARCHAR(255))
    DECLARE @lastFullBackup NVARCHAR(500)
    DECLARE @lastDiffBackup NVARCHAR(500)
    DECLARE @backupFile NVARCHAR(500)
    -- 2 - Initialize variables
    SET @dbName = 'dddd'
    SET @backupPath = 'c:\'
    -- 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'
    SET @cmd = 'RESTORE DATABASE ' + @dbName + ' FROM DISK = '''
    + @backupPath + @lastFullBackup + ''' WITH NORECOVERY, REPLACE'
    PRINT @cmd
    -- 4 - Find latest diff backup
    SELECT @lastDiffBackup = MAX(backupFile)
    FROM @fileList
    WHERE backupFile LIKE '%.DIF'
    AND backupFile > @lastFullBackup
    -- check to make sure there is a diff backup
    IF @lastDiffBackup IS NOT NULL
    BEGIN
    SET @cmd = 'RESTORE DATABASE ' + @dbName + ' FROM DISK = '''
    + @backupPath + @lastDiffBackup + ''' WITH NORECOVERY'
    PRINT @cmd
    SET @lastFullBackup = @lastDiffBackup
    END
    -- 5 - check for log backups
    DECLARE backupFiles CURSOR FOR
    SELECT backupFile
    FROM @fileList
    WHERE backupFile LIKE '%.TRN'
    AND backupFile > @lastFullBackup
    OPEN backupFiles
    -- Loop through all the files for the database
    FETCH NEXT FROM backupFiles INTO @backupFile
    WHILE @@FETCH_STATUS = 0
    BEGIN
    SET @cmd = 'RESTORE LOG ' + @dbName + ' FROM DISK = '''
    + @backupPath + @backupFile + ''' WITH NORECOVERY'
    PRINT @cmd
    FETCH NEXT FROM backupFiles INTO @backupFile
    END
    CLOSE backupFiles
    DEALLOCATE backupFiles
    -- 6 - put database in a useable state
    SET @cmd = 'RESTORE DATABASE ' + @dbName + ' WITH RECOVERY'
    PRINT @cmd

  • 相关阅读:
    在C#中对枚举进行位运算--枚举组合
    详解C#泛型(一)
    详解C#委托和事件(二)
    详解C#特性和反射(四)
    详解C#特性和反射(三)
    详解C#特性和反射(二)
    openssl 证书cert与key合并pfx
    GRPC Oauth IdentityServer4
    GRPC Oauth Identity
    PG SQL funcation
  • 原文地址:https://www.cnblogs.com/qanholas/p/2128029.html
Copyright © 2011-2022 走看看