SQL Server 远程备份还原数据库

来源:互联网 发布:mk17-cqc数据 编辑:程序博客网 时间:2024/05/06 14:21

1.      检查SQL Server 服务的启动账户

检查SQL Server启动账户是否为Local System,如果是修改为“

NT AUTHORITY/NetworkService”或“Administrator”。

注:

xp_cmdshell 生成的 Windows 进程与 SQL Server 服务帐户具有相同的安全权限。而Local System账户是系统内置账户,没有网络连接缺陷的,因此需要修改SQL Server 服务的启动账户。

2.      开启SQL Server系统存储过程 xp_cmdshell

为了安全,默认情况下SQL Server系统存储过程 xp_cmdshell被禁用,可以在SSMS执行下面语句检查是否被禁用:

--启用高级选项xp_cmdshell,属于高级选项,因此需要先开启高级选项

exec master..sp_configure 'show advanced options', 1;

GO

RECONFIGURE; --立即生效,否则重启实例生效

GO

--查询xp_cmdshell状态

exec master..sp_configure 'xp_cmdshell'

返回结果:

name

minimum

maximum

config_value

run_value

xp_cmdshell

0

1

0

0

run_value值为0则表示xp_cmdshell被禁用。

使用下面语句启用xp_cmdshell

--启用高级选项

sp_configure 'show advanced options', 1;

GO

RECONFIGURE; --立即生效,否则重启实例生效

GO

--启用xp_cmdshell

sp_configure 'xp_cmdshell', 1;

GO

RECONFIGURE;

GO

3.      建立磁盘映射

执行下面语句建立网络磁盘映射。

--如果是域环境需要制定登录用户所在的域,详细参考net use 用法

--“//192.168.0.100/E$”为远程服务器的共享文件路径,根据实际修改

exec master..xp_cmdshell 'net use X: //192.168.0.100/E$ "123456" /USER:192.168.0.100/Administrator'

GO

4.      还原数据库

成功建立网络磁盘映射后就可以想操作本地硬盘一样,进行数据库备份和还原操作了。可以使用图形化操作,也可以使用T-SQL,不再详述

5.关闭共享网络连接

exec master..xp_cmdshell 'net use X: /delete'

GO

 

6.      关闭SQL Server xp_cmdshell

使用xp_cmdshell 可以执行许多系统命令,例如重新启动计算机:

 

因此为了安全起见,操作完成后要重新禁用xp_cmdshell,使用下面语句。

--关闭xp_cmdshell

sp_configure 'xp_cmdshell', 0;

GO

RECONFIGURE;

GO

--关闭高级选项

sp_configure 'show advanced options', 0;

GO

RECONFIGURE;

GO

 

 

 

 

关于xp_cmdshell的用法可参考SQL Server联机丛书。

--执行cmd命令,强制重启计算机

exec master..xp_cmdshell 'shutdown -r -f'