异地备份数据库
1.备份服务器中设置共享文件夹
2.源服务器数据库中添加异地备份代理作业
EXEC sp_configure 'show advanced options', 1;RECONFIGURE;
EXEC sp_configure 'xp_cmdshell', 1;RECONFIGURE;
declare @machine nvarchar(50) = '192.168.11.10' --服务器IP
declare @path nvarchar(50) = 'DBBackup\KH_01' --服务器共享目录名称
declare @user nvarchar(50) = 'admin' --服务器的用户名
declare @pwd nvarchar(50) = 'Admin1@3' --服务器的密码
declare @database nvarchar(50) = 'KH_01' --本地要备份的数据库名称
if left(@path,1) != '\' set @path = '\'+@path
declare @sql nvarchar(800) = 'net
use \\'+@machine+@path+' "'+@pwd+'"
/user:'+@machine+'\'+@user
--建立连接类似:'net use \\192.168.200.175\208bf " " /user:192.168.200.175\happy'
Exec KH_01..xp_cmdshell @sql
declare @date datetime = getdate();
Declare @fileName nvarchar(200) = '\\'+@machine+@path+'\'+@database + replace(Convert(nvarchar,@date,120 ) ,':','_') + '.bak'
--备份数据库
Backup Database KH_01 To disk = @fileName With Format
set @sql = 'net use \\'+@machine+@path+'
/delete'
--断开连接类似:'net use \\192.168.200.175\208bf /delete'
Exec KH_01..xp_cmdshell @sql