15
2023
11
15:54:32

数据库异地备份还原

  1. ---备份

  2. USE MASTER

  3. GO  

  4. EXEC sp_configure 'show advanced options', 1;  

  5. RECONFIGURE WITH OVERRIDE;  

  6. EXEC sp_configure 'xp_cmdshell', 1;  

  7. RECONFIGURE WITH OVERRIDE;  

  8. GO  

  9. EXEC MASTER ..xp_cmdshell 'net use \\sharef.cn\Share\DataBackup\SQLBackupData01 "password" /user:sharef\username'  

  10. GO  

  11. ---\\sharef.cn\Share\DataBackup\SQLBackupData01  为中间存储备份文件的 存储服务器共享路径  

  12. CREATE LOGIN [backup_link_user] WITH PASSWORD=N'admindba@0001221', DEFAULT_DATABASE=[master], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF  

  13. GO  

  14. USE [DBA_Maintenance]  

  15. GO  

  16. CREATE USER [backup_link_user] FOR LOGIN [backup_link_user]  

  17. GO  

  18. USE [DBA_Maintenance]  

  19. GO  

  20. ALTER ROLE [db_datareader] ADD MEMBER [backup_link_user]  

  21. GO  

  22. USE [DBA_Maintenance]  

  23. GO  

  24. ALTER ROLE [db_datawriter] ADD MEMBER [backup_link_user]  

  25. GO  

  26. USE [msdb]  

  27. GO  

  28. CREATE USER [backup_link_user] FOR LOGIN [backup_link_user]  

  29. GO  

  30. USE [msdb]  

  31. GO  

  32. ALTER ROLE [db_datareader] ADD MEMBER [backup_link_user]  

  33. GO  

  34. USE DBA_Maintenance  

  35. GO  

  36. CREATE PROCEDURE PRO_Archive_BackupData  

  37. AS  

  38. BEGIN  

  39.    SET NOCOUNT ON  

  40.        DECLARE @NOW DATETIME=GETDATE()  

  41.        DECLARE @condition DATETIME  

  42.        SET @condition=dateadd(day,-3,@NOW)  

  43.        DELETE db_backup_record  

  44.            OUTPUT DELETED.*,@NOW  

  45.            INTO db_backup_archive  

  46.        WHERE backup_end<=@condition  and is_restore=1  

  47.    SET NOCOUNT OFF  

  48. END  

  49. GO  

  50. CREATE PROCEDURE [dbo].[PRO_BACKUP_DB]  

  51. (  

  52. @Instance nvarchar(128)=NULL,  

  53. @DB_NAME SYSNAME,  

  54. @TYPE  VARCHAR(12), --FULL DIFF LOG  

  55. @BACK_PATH VARCHAR(512)  

  56. )  

  57. WITH ENCRYPTION  

  58. AS  

  59. --version1.1  

  60. BEGIN  

  61.    SET NOCOUNT ON  

  62.    DECLARE @BACKUP_FULL_SQL NVARCHAR(1280)  

  63.    DECLARE @BACKUP_DIFF_SQL NVARCHAR(1280)  

  64.    DECLARE @BACKUP_LOG_SQL NVARCHAR(1280)  

  65.    DECLARE @DATE_SERIAL_NUMBER NVARCHAR(16)  

  66.    IF @Instance IS NULL  

  67.    BEGIN  

  68.        SET @Instance=@@SERVICENAME  

  69.    END  

  70.    SET @DATE_SERIAL_NUMBER='_'+''+CONVERT(varchar(8), GETDATE(), 112)+ replace(convert(char(5), getdate(), 108), ':', '')+''  

  71.    IF @DB_NAME IS NULL  

  72.    BEGIN  

  73.        PRINT 'Database Does not Exist'  

  74.        RETURN  1;  

  75.    END  

  76.    IF NOT EXISTS(SELECT NAME FROM MASTER.SYS.DATABASES WHERE NAME=@DB_NAME)  

  77.    BEGIN  

  78.        PRINT '[' + @DB_NAME + '] is not a valid database name!'  

  79.        RETURN 1;  

  80.    END  

  81.    IF @TYPE NOT IN ('FULL','DIFF','LOG')  

  82.    BEGIN  

  83.        PRINT 'Database backup type must be [full], [log] or [diff]!'  

  84.        RETURN 1;  

  85.    END  

  86.    DECLARE @EXISTS_INSTANCE_SQL NVARCHAR(512)  

  87.    DECLARE @EXISTS_INSTANCE_FOLDER TABLE(COL NVARCHAR(512))  

  88.    DECLARE @EXISTS_FOLDER TABLE(COL NVARCHAR(512))  

  89.    DECLARE @EXISTS_SQL NVARCHAR(512)  

  90.    DECLARE @EXISTS_SHAREFLODER_SQL NVARCHAR(512)  

  91.    DECLARE @MKDIR_SQL NVARCHAR(512)  

  92.    DECLARE @MKDIR_INSTANCE_SQL NVARCHAR(512)  

  93.    SET @EXISTS_SHAREFLODER_SQL=N'EXEC MASTER..XP_CMDSHELL ''DIR'+CHAR(9)+''+@BACK_PATH+''''  

  94.    SET @EXISTS_INSTANCE_SQL=N'EXEC MASTER..XP_CMDSHELL ''DIR'+CHAR(9)+''+@BACK_PATH+'\'+@Instance+''''  

  95.    SET @EXISTS_SQL=N'EXEC MASTER..XP_CMDSHELL ''DIR'+CHAR(9)+''+@BACK_PATH+'\'+@Instance+'\'+@DB_NAME+''''  

  96.    SET @MKDIR_INSTANCE_SQL=N'EXEC MASTER..XP_CMDSHELL ''MKDIR'+CHAR(9)+''+@BACK_PATH+'\'+@Instance+''', NO_OUTPUT'  

  97.    SET @MKDIR_SQL=N'EXEC MASTER..XP_CMDSHELL ''MKDIR'+CHAR(9)+''+@BACK_PATH+'\'+@Instance+'\'+@DB_NAME+''', NO_OUTPUT'  

  98.    INSERT INTO @EXISTS_FOLDER EXEC SP_EXECUTESQL @EXISTS_SHAREFLODER_SQL,N'@BACK_PATH NVARCHAR(128)',@BACK_PATH  

  99.    IF EXISTS (SELECT 1 FROM @EXISTS_FOLDER WHERE COL LIKE '%File Not Found%')  

  100.    BEGIN  

  101.        PRINT '['+@BACK_PATH+'] is not a valid !'  

  102.        RETURN 2;  

  103.    END  

  104.    INSERT INTO @EXISTS_INSTANCE_FOLDER EXEC SP_EXECUTESQL @EXISTS_INSTANCE_SQL,N'@BACK_PATH NVARCHAR(128),@Instance NVARCHAR(128)',@BACK_PATH,@Instance  

  105.    IF EXISTS (SELECT 1 FROM @EXISTS_INSTANCE_FOLDER WHERE COL LIKE '%File Not Found%')  

  106.    BEGIN  

  107.        EXEC SP_EXECUTESQL @MKDIR_INSTANCE_SQL,N'@BACK_PATH NVARCHAR(128),@Instance NVARCHAR(128)',@BACK_PATH,@Instance  

  108.    END  

  109.    INSERT INTO @EXISTS_FOLDER EXEC SP_EXECUTESQL @EXISTS_SQL,N'@BACK_PATH NVARCHAR(128),@Instance NVARCHAR(128),@DB_NAME NVARCHAR(128)',@BACK_PATH,@Instance,@DB_NAME  

  110.    IF EXISTS(SELECT 1 FROM @EXISTS_FOLDER WHERE COL LIKE '%File Not Found%')  

  111.    BEGIN  

  112.        EXEC SP_EXECUTESQL @MKDIR_SQL,N'@BACK_PATH NVARCHAR(128),@Instance NVARCHAR(128),@DB_NAME NVARCHAR(128)',@BACK_PATH,@Instance,@DB_NAME  

  113.    END  

  114.    IF RIGHT(@BACK_PATH,1)='\'  

  115.    BEGIN  

  116.        SET @BACK_PATH=LEFT(@BACK_PATH,LEN(@BACK_PATH)-1)  

  117.    END  

  118.    DECLARE @ERROR INT  

  119.    DECLARE @BACKUP_FILENAME VARCHAR(512)  

  120.    DECLARE @BACKUP_BEGIN DATETIME  

  121.        IF @TYPE='FULL'  

  122.        BEGIN  

  123.            SET @BACKUP_FULL_SQL=N'BACKUP DATABASE'+CHAR(9)+'['+@DB_NAME+']'+CHAR(9)  

  124.                                +'TO DISK= '''+@BACK_PATH+'\'+@Instance+'\'+@DB_NAME+'\'+@DB_NAME+@DATE_SERIAL_NUMBER+'.BAK'''+CHAR(9)+'WITH NOFORMAT, NOINIT,'  

  125.                                +'NAME=N'''+@DB_NAME+'-FULL Database Backup'', SKIP, NOREWIND, NOUNLOAD,  STATS = 10'  

  126.            SET @BACKUP_FILENAME=@DB_NAME+@DATE_SERIAL_NUMBER+'.BAK'  

  127.            --EXEC (@BACKUP_FULL_SQL)  

  128.            SET @BACKUP_BEGIN=GETDATE()  

  129.            EXEC SP_EXECUTESQL @BACKUP_FULL_SQL,N'@DB_NAME NVARCHAR(128),@BACK_PATH NVARCHAR(128),@Instance NVARCHAR(128),@DATE_SERIAL_NUMBER NVARCHAR(16)',@DB_NAME,@BACK_PATH,@Instance,@DATE_SERIAL_NUMBER  

  130.            SET @ERROR=@@ERROR  

  131.        END  

  132.        IF @TYPE='DIFF'  

  133.        BEGIN  

  134.            SET @BACKUP_DIFF_SQL=N'BACKUP DATABASE'+CHAR(9)+'['+@DB_NAME+']'+CHAR(9)  

  135.                                +'TO DISK= '''+@BACK_PATH+'\'+@Instance+'\'+@DB_NAME+'\'+@DB_NAME+@DATE_SERIAL_NUMBER+'.DIFF'''+CHAR(9)+'WITH  DIFFERENTIAL , NOFORMAT, NOINIT,'  

  136.                                +'NAME=N'''+@DB_NAME+'-Differential Database Backup'', SKIP, NOREWIND, NOUNLOAD,  STATS = 10'  

  137.            SET @BACKUP_FILENAME=@DB_NAME+@DATE_SERIAL_NUMBER+'.DIFF'  

  138.            --EXEC (@BACKUP_DIFF_SQL)  

  139.            SET @BACKUP_BEGIN=GETDATE()  

  140.            EXEC SP_EXECUTESQL @BACKUP_DIFF_SQL,N'@DB_NAME NVARCHAR(128),@BACK_PATH NVARCHAR(128),@Instance NVARCHAR(128),@DATE_SERIAL_NUMBER NVARCHAR(16)',@DB_NAME,@BACK_PATH,@Instance,@DATE_SERIAL_NUMBER  

  141.            SET @ERROR=@@ERROR  

  142.        END  

  143.        IF @TYPE='LOG'  

  144.        BEGIN  

  145.            SET @BACKUP_LOG_SQL=N'BACKUP LOG'+CHAR(9)+'['+@DB_NAME+']'+CHAR(9)  

  146.                                +'TO DISK= '''+@BACK_PATH+'\'+@Instance+'\'+@DB_NAME+'\'+@DB_NAME+@DATE_SERIAL_NUMBER+'.TRN'''+CHAR(9)+'WITH NOFORMAT, NOINIT,'  

  147.                                +'NAME=N'''+@DB_NAME+'-Transaction Log Backup'', SKIP, NOREWIND, NOUNLOAD,  STATS = 10'  

  148.            SET @BACKUP_FILENAME=@DB_NAME+@DATE_SERIAL_NUMBER+'.TRN'  

  149.            --EXEC (@BACKUP_LOG_SQL)  

  150.            SET @BACKUP_BEGIN=GETDATE()  

  151.            EXEC SP_EXECUTESQL @BACKUP_LOG_SQL,N'@DB_NAME NVARCHAR(128),@BACK_PATH NVARCHAR(128),@Instance NVARCHAR(128),@DATE_SERIAL_NUMBER NVARCHAR(16)',@DB_NAME,@BACK_PATH,@Instance,@DATE_SERIAL_NUMBER  

  152.            SET @ERROR=@@ERROR  

  153.        END  

  154.        IF @ERROR=0  

  155.        BEGIN  

  156.            --INSERT INTO DBA_Maintenance.dbo.db_backup_record select @DB_NAME,@BACKUP_BEGIN,getdate(), @BACK_PATH+'\'+@Instance+'\'+@DB_NAME,@TYPE,@BACKUP_FILENAME,0  --version 1.0  

  157.            INSERT INTO DBA_Maintenance.dbo.db_backup_record select @DB_NAME,@BACKUP_BEGIN,getdate(), @BACK_PATH+'\'+@Instance+'\'+@DB_NAME+'\'+@BACKUP_FILENAME,@TYPE,0  --version 1.1  

  158.        END  

  159.    SET NOCOUNT OFF  

  160. END  

  161. ---还原

  162. USE MASTER

  163. GO

  164. EXEC sp_configure 'show advanced options', 1;

  165. RECONFIGURE WITH OVERRIDE;

  166. EXEC sp_configure 'xp_cmdshell', 1;

  167. RECONFIGURE WITH OVERRIDE;

  168. GO

  169. EXEC MASTER ..xp_cmdshell 'net use \\sharef.cn\Share\DataBackup\SQLBackupData01 "password" /user:sharef\username'  

  170. GO

  171. GO

  172. CREATE LOGIN [backup_link_user] WITH PASSWORD=N'admindba@0001221', DEFAULT_DATABASE=[master], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF

  173. GO

  174. USE [DBA_Maintenance]

  175. GO

  176. CREATE USER [backup_link_user] FOR LOGIN [backup_link_user]

  177. GO

  178. USE [DBA_Maintenance]

  179. GO

  180. ALTER ROLE [db_datareader] ADD MEMBER [backup_link_user]

  181. GO

  182. USE [DBA_Maintenance]

  183. GO

  184. ALTER ROLE [db_datawriter] ADD MEMBER [backup_link_user]

  185. GO

  186. USE [msdb]

  187. GO

  188. CREATE USER [backup_link_user] FOR LOGIN [backup_link_user]

  189. GO

  190. USE [msdb]

  191. GO

  192. ALTER ROLE [db_datareader] ADD MEMBER [backup_link_user]

  193. GO

  194. EXEC sp_addlinkedserver  

  195.      @server='VCST004003IS01' ,--被访问的服务器别名

  196.      @srvproduct='' ,

  197.      @provider='SQLOLEDB' ,

  198.      @datasrc='vcst004003sl01.sharef.cn\vcst004003is01'    --要访问的服务器

  199. EXEC sp_addlinkedsrvlogin  

  200.     'VCST004003IS01', --被访问的服务器别名

  201.     'false',

  202.     NULL,

  203.     'backup_link_user', --帐号

  204.     'admindba@0001221' --密码

  205. GO

  206. CREATE PROCEDURE [dbo].[PRO_AccordingTo_Parameters_Restore_DB]

  207. (

  208. @Instance nvarchar(128)=NULL,--暂时无用

  209. @DB_NAME NVARCHAR(128)=NULL,

  210. @TYPE  NVARCHAR(16), --FULL DIFF LOG

  211. @RESTORE_PATH NVARCHAR(128)=N'\\sharef.cn\Share\DataBackup\SQLBackupData01\SQLBackupData01\VCST004005IS01',

  212. @LOCAL_DBFILES_PATH NVARCHAR(512)=N'T:\MSSQL11.VWSR027002IS02\MSSQL\DATA\',

  213. @DATE_TIME DATETIME=NULL

  214. )

  215. WITH ENCRYPTION

  216. AS

  217. BEGIN

  218.    SET NOCOUNT ON

  219.    DECLARE @RESTORE_PATH_FILENAME NVARCHAR(512)

  220.    DECLARE @RESTORE_FULL_SQL NVARCHAR(4000)

  221.    DECLARE @RESTORE_DIFF_SQL NVARCHAR(4000)

  222.    DECLARE @RESTORE_LOG_SQL NVARCHAR(4000)

  223.    DECLARE @ERROR INT=0

  224.    DECLARE @i int=1

  225.    DECLARE @count int=1

  226.    DECLARE @RESTORE_BEGIN DATETIME

  227.    DECLARE @EXISTS_FOLDER TABLE(COL NVARCHAR(512))

  228.    DECLARE @EXISTS_RESTOREDB_FOLDER_SQL NVARCHAR(512)

  229.    IF @DATE_TIME IS NULL AND @TYPE='FULL'

  230.    BEGIN

  231.        SET @DATE_TIME=DATEADD(day,-3,getdate())

  232.    END

  233.    IF @DATE_TIME IS NULL AND @TYPE='DIFF'

  234.    BEGIN

  235.        SET @DATE_TIME=DATEADD(hour,-3,getdate())

  236.    END

  237.    IF @TYPE NOT IN ('FULL','DIFF','LOG')

  238.    BEGIN

  239.        PRINT 'Please Specify The Type Of The Restore Database ([full], [log] or [diff])!'

  240.        RETURN;

  241.    END

  242.    IF @RESTORE_PATH IS NULL

  243.    BEGIN

  244.        PRINT 'The Path To Restore Database Cannot Be Empty!'

  245.        RETURN;

  246.    END

  247.    ELSE

  248.    BEGIN

  249.    SET @EXISTS_RESTOREDB_FOLDER_SQL=N'EXEC MASTER..XP_CMDSHELL ''DIR'+CHAR(9)+''+@RESTORE_PATH+'\'+@DB_NAME+''''

  250.    INSERT INTO @EXISTS_FOLDER EXEC SP_EXECUTESQL @EXISTS_RESTOREDB_FOLDER_SQL,N'@RESTORE_PATH NVARCHAR(128),@DB_NAME NVARCHAR(128)',@RESTORE_PATH,@DB_NAME

  251.    IF EXISTS (SELECT 1 FROM @EXISTS_FOLDER WHERE COL LIKE '%File Not Found%')

  252.    BEGIN

  253.        PRINT '['+@RESTORE_PATH+'] is not a valid !'

  254.        RETURN;

  255.    END

  256.    END

  257.    IF RIGHT(@RESTORE_PATH,1)='\'

  258.    BEGIN

  259.        SET @RESTORE_PATH=LEFT(@RESTORE_PATH,LEN(@RESTORE_PATH)-1)

  260.    END

  261.    DECLARE @tmp TABLE (rn int,dbname sysname,backup_type varchar(16),backup_path_filename nvarchar(512))

  262.    DECLARE @RESTORE_TYPE VARCHAR(6)

  263.    IF @TYPE='FULL'

  264.    BEGIN

  265.        SET @RESTORE_TYPE='D'

  266.    END

  267.    IF @TYPE='DIFF'

  268.    BEGIN

  269.        SET @RESTORE_TYPE='I'

  270.    END

  271.    IF @TYPE='LOG'

  272.    BEGIN

  273.        SET @RESTORE_TYPE='L'

  274.    END

  275.    INSERT INTO  @tmp SELECT ROW_NUMBER() over(order by database_name)rn,BACKUP_DATA.database_name, BACKUP_DATA.backup_type,BACKUP_DATA.physical_device_name

  276.                             FROM

  277.                                (

  278.                                SELECT

  279.                                    bjs.database_name,

  280.                                 CASE bjs.type

  281.                                       WHEN 'D' THEN 'Database'

  282.                                       WHEN 'L' THEN 'Log'

  283.                                       WHEN 'I' THEN 'DIFF'

  284.                                   END AS backup_type,

  285.                                   bkmf.physical_device_name,

  286.                                   bjs.first_lsn,bjs.last_lsn,

  287.                                   bjs.database_backup_lsn,

  288.                                   bjs.backup_finish_date

  289.                                FROM   [VCST004005sl01].[msdb].[dbo].[backupmediafamily] AS bkmf  WITH(NOLOCK)

  290.                                        INNER JOIN [VCST004005sl01].[msdb].[dbo].[backupset] AS bjs  WITH(NOLOCK)

  291.                                            ON bkmf. media_set_id=bjs.media_set_id

  292.                                   WHERE bjs.RECOVERY_MODEL='FULL'

  293.                                            AND bjs.type=@RESTORE_TYPE

  294.                                            AND bjs.backup_finish_date>=@DATE_TIME

  295.                                            AND bjs.database_name not in ('master','msdb','model','tempdb','DBA_Maintenance')

  296.                                    )AS BACKUP_DATA

  297.                                   WHERE  NOT EXISTS( SELECT 1

  298.                                                        FROM msdb .dbo. backupset AS bus  WITH(NOLOCK)

  299.                                                            WHERE BACKUP_DATA.database_name=bus.database_name

  300.                                                                    AND BACKUP_DATA.first_lsn= bus.first_lsn

  301.                                                                    AND BACKUP_DATA.last_lsn=bus.last_lsn

  302.                                                                    AND bus.backup_finish_date>=@DATE_TIME

  303.                                                                    AND bus.type=@RESTORE_TYPE

  304.                                                )

  305.                                        AND NOT EXISTS (SELECT 1 FROM (SELECT database_name,MAX(backup_finish_date)backup_finish_date

  306.                                                                        FROM msdb .dbo. backupset AS bus  WITH(NOLOCK)

  307.                                                                        WHERE bus.backup_finish_date>=@DATE_TIME

  308.                                                                                AND type=@RESTORE_TYPE

  309.                                                                        GROUP BY database_name

  310.                                                                    )tmp

  311.                                                            WHERE BACKUP_DATA.database_name=tmp.database_name

  312.                                                                    AND BACKUP_DATA.backup_finish_date<=tmp.backup_finish_date

  313.                                                    )

  314.    SELECT @count=count(*) from @tmp

  315.    WHILE @i<=@count

  316.        BEGIN

  317.            SET @DB_NAME=NULL

  318.            SET @RESTORE_PATH_FILENAME=NULL

  319.            SET @RESTORE_PATH=NULL

  320.            SET @ERROR=0

  321.            SELECT @DB_NAME=dbname,

  322.                    @RESTORE_PATH_FILENAME=backup_path_filename

  323.                FROM @tmp  WHERE rn=@i

  324.            DECLARE @FileListInfo TABLE

  325.            (

  326.                LogicalName nvarchar(512) null,

  327.                PhysicalName nvarchar(512) null,

  328.                Type varchar(16) null,

  329.                FileGroupName nvarchar(128) null,

  330.                FileSize bigint null ,

  331.                FileMaxSize Bigint null,

  332.                FileId bigint,

  333.                CreateLSN numeric(25,0),

  334.                DropLSN numeric(25,0) NULL,

  335.                UniqueID uniqueidentifier,

  336.                ReadOnlyLSN numeric(25,0) NULL,

  337.                ReadWriteLSN numeric(25,0) NULL,

  338.                BackupSizeInBytes bigint,

  339.                SourceBlockSize int,

  340.                FileGroupID int,

  341.                LogGroupGUID uniqueidentifier NULL,

  342.                DifferentialBaseLSN numeric(25,0) NULL,

  343.                DifferentialBaseGUID uniqueidentifier,

  344.                IsReadOnly bit,

  345.                IsPresent bit,

  346.                TDEThumbprint nvarchar(128)

  347.            )

  348.            DECLARE @Get_FilelistSQL nvarchar(max)

  349.            SET @Get_FilelistSQL = N'RESTORE FILELISTONLY FROM  DISK = N'''+@RESTORE_PATH_FILENAME+''''+CHAR(9)

  350.            INSERT INTO  @FileListInfo EXEC SP_EXECUTESQL @Get_FilelistSQL

  351.            DECLARE @LogicalName_D NVARCHAR(512)

  352.            DECLARE @LogicalName_L NVARCHAR(512)

  353.            SELECT @LogicalName_D=LogicalName FROM @FileListInfo WHERE Type='D'

  354.            SELECT @LogicalName_L=LogicalName FROM @FileListInfo WHERE Type='L'

  355.            IF @TYPE='FULL'

  356.                BEGIN

  357.                    SET @RESTORE_FULL_SQL=N'RESTORE DATABASE'+CHAR(9)+'['+@DB_NAME+']'+CHAR(9)

  358.                                        +'FROM DISK= '''+@RESTORE_PATH_FILENAME+''''+CHAR(9)

  359.                                        +'WITH  FILE = 1,MOVE N'''+@DB_NAME+''' TO N'''+@LOCAL_DBFILES_PATH+@LogicalName_D+'.MDF'',

  360.                                         MOVE N'''+@DB_NAME+'_LOG '' TO N'''+@LOCAL_DBFILES_PATH+@LogicalName_L+'.LDF'',NORECOVERY,  NOUNLOAD,  REPLACE,  STATS = 5'

  361.                    SET @RESTORE_BEGIN =GETDATE()

  362.                    EXEC SP_EXECUTESQL @RESTORE_FULL_SQL

  363.                                        ,N'@DB_NAME NVARCHAR(128),@RESTORE_PATH_FILENAME NVARCHAR(512),@LOCAL_DBFILES_PATH NVARCHAR(512),@LogicalName_D NVARCHAR(128),@LogicalName_L NVARCHAR(128)'

  364.                                        ,@DB_NAME,@RESTORE_PATH_FILENAME,@LOCAL_DBFILES_PATH,@LogicalName_D,@LogicalName_L

  365.                    SET @ERROR=@ERROR+@@ERROR

  366.                END

  367.            IF @TYPE='DIFF'

  368.                BEGIN

  369.                    SET @RESTORE_DIFF_SQL=N'RESTORE DATABASE'+CHAR(9)+'['+@DB_NAME+']'+CHAR(9)

  370.                                        +'FROM DISK= '''+@RESTORE_PATH_FILENAME+''''+CHAR(9)

  371.                                        +'WITH  FILE = 1,MOVE N'''+@DB_NAME+''' TO N'''+@LOCAL_DBFILES_PATH+@LogicalName_D+'.MDF'',

  372.                                         MOVE N'''+@DB_NAME+'_LOG '' TO N'''+@LOCAL_DBFILES_PATH+@LogicalName_L+'.LDF'',NORECOVERY,  NOUNLOAD,  REPLACE,  STATS = 10'

  373.                    SET @RESTORE_BEGIN =GETDATE()

  374.                    EXEC SP_EXECUTESQL @RESTORE_DIFF_SQL

  375.                                        ,N'@DB_NAME NVARCHAR(128),@RESTORE_PATH_FILENAME NVARCHAR(512),@LOCAL_DBFILES_PATH NVARCHAR(512),@LogicalName_D NVARCHAR(128),@LogicalName_L NVARCHAR(128)'

  376.                                        ,@DB_NAME,@RESTORE_PATH_FILENAME,@LOCAL_DBFILES_PATH,@LogicalName_D,@LogicalName_L

  377.                    SET @ERROR=@ERROR+@@ERROR

  378.                END

  379.            IF @TYPE='LOG'

  380.                BEGIN

  381.                    SET @RESTORE_LOG_SQL=N'RESTORE LOG'+CHAR(9)+'['+@DB_NAME+']'+CHAR(9)

  382.                                        +'FROM DISK= '''+@RESTORE_PATH_FILENAME+''''+CHAR(9)

  383.                                        +'WITH  FILE = 1,NORECOVERY,  NOUNLOAD,  STATS = 10'

  384.                    SET @RESTORE_BEGIN =GETDATE()

  385.                    EXEC SP_EXECUTESQL @RESTORE_LOG_SQL

  386.                                        ,N'@DB_NAME NVARCHAR(128),@RESTORE_PATH_FILENAME NVARCHAR(512)'

  387.                                        ,@DB_NAME,@RESTORE_PATH_FILENAME

  388.                    SET @ERROR=@ERROR+@@ERROR

  389.                END

  390.            IF @ERROR=0

  391.                BEGIN

  392.                    INSERT INTO DBA_Maintenance.dbo.db_restory_record (dbname,restore_begin,restore_end,restore_type,restore_filename,flag)

  393.                        VALUES (@DB_NAME,@RESTORE_BEGIN,GETDATE(),@TYPE,@RESTORE_PATH_FILENAME,1)

  394.                END

  395.            SET @i=@i+1

  396.        END

  397.    SET NOCOUNT OFF

  398. END

  399. GO




推荐本站淘宝优惠价购买喜欢的宝贝:

image.png

本文链接:https://hqyman.cn/post/4538.html 非本站原创文章欢迎转载,原创文章需保留本站地址!

分享到:
打赏





休息一下~~


« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

请先 登录 再评论,若不是会员请先 注册

您的IP地址是: