SQL Server 遠程備份資料庫

來源:互聯網
上載者:User

SQL Server 遠程備份資料庫

經常會有定時備份SQL Server資料庫的需要。定時備份到原生話,還是挺容易的,計劃任務就可以完成,但如果是整機掛了,那備份到本機是沒意義的,那麼就需要來考慮備份到區域網路中,其它電腦裡。

下面就分享一份在網上找了之後,自己再簡單整理過的代碼,配合 SQL Server Agent中的作業功能,已經穩定運行一個星期了,每小時就備份一次。

-- 建立網路映射(Y是盤符;IP地址後面要帶共用資料夾的名稱;password是密碼,雙引號引起;account是遠程電腦的登入名稱)
exec master..xp_cmdshell 'net use Y: \\192.168.0.69\sqlbackup "password" /user:192.168.0.69\account'
-- 按日期時間做檔案名稱(注意路徑中的檔案夾,需要先建立好)
declare @filename varchar(200)
select @filename = 'Y:\DB\' + replace(replace(replace(CONVERT(varchar, getdate(), 120 ),'-',''),' ','-'),':','') + '.bak'
-- 執行備份(DB是要備份的資料庫名稱)
backup database [DB] to disk = @filename
-- 刪除網路映射(Y是盤符,同上)
exec master..xp_cmdshell 'net use Y: /delete'

本文永久更新連結地址:

相關文章

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.