SQL server 备份到网络驱动器,需要在SQL中映射网络驱动器。
SQL server可以通过xp_cmdshell执行cmd命令,由于存在安全隐患,xp_cmdshell 默认是关闭的。
首先需启用xp_cmdshell,然后执行映射命令。
cmd的映射和删除映射命令如下:
net use Z: \\IP\路径 密码 /user:IP\登录名
net use Z: /del
-- SQL 2008r2备份到网络驱动器
-- 启用xp_cmdshell: EXEC sp_configure 'show advanced options', 1;
-- 关闭xp_cmdshell: EXEC sp_configure 'show advanced options', 0;
-- 创建映射: exec master..xp_cmdshell 'net use Z: \\IP\SHARE password /user:IP\Administrator';
-- 删除映射: exec master..xp_cmdshell 'net use Z: /del' ;
-- 验证映射是否成功: exec master..xp_cmdshell 'dir Z:';
EXEC sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
EXEC sp_configure 'xp_cmdshell', 1;
GO
RECONFIGURE;
GO
exec master..xp_cmdshell 'net use Z: \\192.168.10.14\DBbak ****** /user:192.168.10.14\admin';
GO
EXEC sp_configure 'show advanced options', 0;
GO
RECONFIGURE;
GO