sqlserver 远程异地备份
问题描述客户的sqlserver数据库服务器每日凌晨自动备份t1数据库,现在要求同时将备份文件自动上传到一台存储服务器(有ftpserver)上
解决方案1. 开发一个window服务程序每日凌晨将本机(数据库服务器)的备份文件以ftp方式上传到存储服务器上
2. 利用sqlserver的xp_cmdshell 工具调用ftp命令将备份文件上传到存储服务器上
具体方法第一种方案不再描述
第二种方案实现如下
1. 确认存储服务器ftp服务可用,确认用户名秘密
2. 使用以下sql代码打开xp_cmdshell使用权限(sqlserver 2005默认关闭)
sp_configure 'show advanced options', 1
go
reconfigure
go
sp_configure 'xp_cmdshell', 1
go
reconfigure
go
3. 在sqlserver数据库服务器c:\dbbackup\下创建 ftp脚步文件ftpconfig.txt 内容如下,第一行用户名第二行秘密,如果匿名第一行 anonymous第二行空行
123
123
put c:\dbbackup\*.tempbak
close
quit
4. 在sqlserver数据库查询分析器测试以下sql代码,假设数据库名 testdb备份位置c:\dbbackup 存储服务器ip 192.168.0.44
declare @tempfilename varchar(100)
set @tempfilename= convert(varchar(8), getdate(), 112)+ convert(varchar(2), datepart (hour,getdate()))+ convert(varchar(2), datepart (minute,getdate())) + convert(varchar(2), datepart (second,getdate()))
print @tempfilename
declare @tempfilename1 varchar(100)
set @tempfilename1='c:\dbbackup\testdb'+ @tempfilename +'.tempbak'
backup database testdb to disk = @tempfilename1
exec xp_cmdshell 'ftp -s:c:\dbbackup\ftpconfig.txt 192.168.0.44 ' –anonymous
declare @temp varchar(100)
set @temp='ren c:\dbbackup\testdb'+ @tempfilename +'.tempbak '+'testdb'+ @tempfilename +'.bak'
print @temp
exec xp_cmdshell @temp
5. 在sqlserver数据库上建立维护计划,定时自动执行第四部分sql
