sql server 定時備份資料庫

來源:互聯網
上載者:User

標籤:comm   共用   time   set   時間   password   sub   Database Backup   --   

CREATE  PROCEDURE [dbo].[SP_DBBackup_EveryNight_Local] @cycle INT,---儲存周期@IsLocal INT,---是否為本地  0表示是 1表示否@SavePath NVARCHAR(100),---檔案儲存路徑 \\192.168.19.102\DataBaseBack@DBPrefix NVARCHAR(50),---組建檔案的首碼@Server VARCHAR(50),---伺服器IP  192.168.19.102@UserName VARCHAR(50),---登入伺服器使用者名稱  administrator@PassWord VARCHAR(20)---登入伺服器密碼   Soft_2016AS    BEGIN          DECLARE @backpath2 VARCHAR(100)          DECLARE @command VARCHAR(200)                   --建立暫存資料表        CREATE TABLE #Filetabel            (              FILEPATH VARCHAR(100) NULL            )    --調用dos命令登入伺服器    IF @IsLocal = 1    BEGINSET @command = ‘net use ‘ + @SavePath + ‘ ‘ + @PassWord + ‘ /user:‘ + @Server + ‘\‘ + @UserName         EXEC master..xp_cmdshell @command      END        --將共用目錄所有檔案名稱添加至暫存資料表        SET @command = ‘dir /b ‘ +  @SavePath        INSERT  INTO #Filetabel                EXEC master..xp_cmdshell @command                        DELETE  FROM #Filetabel        WHERE   FILEPATH IS NULL                WHILE EXISTS ( SELECT   *                       FROM     #Filetabel )            BEGIN                DECLARE @fileName VARCHAR(100)                SET @fileName = ( SELECT TOP 1                                            *                                  FROM      #Filetabel                                  ORDER BY  FILEPATH                                )--定義變數擷取檔案時間                DECLARE @fileTime VARCHAR(20)                DECLARE @fDateTime DATETIME                IF @fileName IS NOT NULL                    BEGIN                        SET @fileTime = SUBSTRING(@fileName, 17, 8)                        SET @fDateTime = CONVERT(DATETIME, @fileTime)                        DECLARE @lastTime DATETIME                        SET @lastTime = DATEADD(DAY, [email protected],                                                CONVERT(VARCHAR(10), GETDATE(), 111))                        IF @fDateTime <= @lastTime                            BEGIN --刪除備份                                DECLARE @filePath VARCHAR(100)                                SET @filePath = ‘del ‘ + @SavePath + ‘\‘ + @fileName                                EXEC master..xp_cmdshell @filePath                            END                                                        --刪除該條資料                                DELETE  FROM #Filetabel                                WHERE   FILEPATH = @fileName                    END            END                        --刪除暫存資料表                                DROP TABLE #Filetabel                                    --將Database Backup到伺服器                                SET @backpath2 = @SavePath + ‘\‘ + @DBPrefix + ‘_‘ + REPLACE(CONVERT(VARCHAR(10), GETDATE(), 120),                                              ‘-‘, ‘‘) + ‘_back.bak‘                                  BACKUP DATABASE [LGS] TO [email protected] WITH INIT ,FORMAT       END GO

  

執行EXEC [dbo].[SP_DBBackup_EveryNight_Local] 5,0,‘F:\LGSCMS_20141117_Back‘,‘LGS‘,‘‘,‘‘,‘‘

sql server 定時備份資料庫

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.