SQL远程备份
SQL遠(yuǎn)程備份 原文:SQL遠(yuǎn)程備份 set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go -- ============================================= -- Author: dcrenl -- Create date: 2013-9-5 9:54:46 -- Description: WEB服務(wù)器遠(yuǎn)程備份數(shù)據(jù)庫, --此方法需要打開WEB服務(wù)器的共享功能. --驗(yàn)證方法同一局域網(wǎng)其它機(jī)器里在運(yùn)行里輸入 \\IP\C$ 要求輸入用戶名和密碼,如果可以訪問此方法有效 -- --異地備份還有很多種方法 --1.將數(shù)據(jù)庫備份完之后在數(shù)據(jù)庫服務(wù)上創(chuàng)建一個(gè)庫,添加表,創(chuàng)建一個(gè)Image類型字段, -- 然后使用sp_textcopy把備份文件寫入數(shù)據(jù)庫,再從WEB服務(wù)器將備份文件讀取出來,不過此方法讀取出來的備份文件有可能會(huì)損壞 --2.在WEB服務(wù)器安裝SQL Server 使用SQLDMO(Interop.SQLDMO.dll)或 --Microsoft.SqlServer.ConnectionInfo.dll 、 Microsoft.SqlServer.Management.Sdk.Sfc.dll --(以上兩個(gè)DLL文件位置:Program Files\Microsoft SQL Server\100\SDK\Assemblies) --生成數(shù)據(jù)庫表、存儲(chǔ)過程和函數(shù)等腳本,然后在WEB服務(wù)上的SQL Server上創(chuàng)建數(shù)據(jù)庫, --使用生成的腳本創(chuàng)建表等,然后將數(shù)據(jù)Insert到WEB服務(wù)器上的數(shù)據(jù)庫進(jìn)行備份 -- ============================================= CREATE PROCEDURE [dbo].[RemoteBack] @DataDisk nvarchar(max),--需要在數(shù)據(jù)庫服務(wù)器上映射的盤符 @WEBAddr nvarchar(max),--WEB服務(wù)器地址 @WEBDisk nvarchar(max),--WEB服務(wù)器共享目錄(例如:D$ 或 C$\Windows) @Password nvarchar(max),--WEB服務(wù)器密碼 @UserName nvarchar(max),--WEB服務(wù)器用戶名 @DataName nvarchar(max)--需要備份的數(shù)據(jù)庫名 AS BEGIN SET NOCOUNT ON; --設(shè)置日期時(shí)間串 DECLARE ?@DateTime DateTime set @DateTime = getdate() DECLARE @FileTime nvarchar(max) set @FileTime = convert(nvarchar(50), year(@DateTime)) + right(cast(month(@DateTime)+100 as varchar),2) + right(cast(day(@DateTime)+100 as varchar),2) + right(cast(DateName(hour,GetDate())+100 as varchar),2) + right(cast(DateName(minute,GetDate())+100 as varchar),2) + right(cast(DateName(second,GetDate())+100 as varchar),2) --打開高級設(shè)置 exec sp_configure 'show advanced options',1 reconfigure --開啟xp_cmdshell exec sp_configure 'xp_cmdshell',1 reconfigure --將WEB服務(wù)器路徑映射到數(shù)據(jù)庫服務(wù)器 exec ('master..xp_cmdshell ''net use ' + @DataDisk + ': \\' + @WEBAddr + '\' + @WEBDisk + ' "' + @Password + '" /user:' + @WEBAddr + '\' + @UserName + '''') --開始備份并復(fù)制到WEB服務(wù)器映射到SQL服務(wù)上的路徑 exec('backup database ' + @DataName + ' to disk='''+@DataDisk+':\' + @DataName +'_'+ @FileTime +'.bak'' with init') --關(guān)閉映射路徑 exec ('master..xp_cmdshell ''net use ' + @DataDisk + ': /delete''') --關(guān)閉xp_cmdshell exec sp_configure 'xp_cmdshell',0 reconfigure --關(guān)閉高級設(shè)置 exec sp_configure 'show advanced options',0 reconfigure END
?
posted on 2014-11-12 00:13 NET未來之路 閱讀(...) 評論(...) 編輯 收藏轉(zhuǎn)載于:https://www.cnblogs.com/lonelyxmas/p/4090928.html
總結(jié)
- 上一篇: 设计模式--23、访问者模式
- 下一篇: python安装mysqlclient模