SQL2005远程备份,共享文件夹备份方法
SQL2005远程备份,共享文件夹备份方法
备份数据库应用中,有时需要在联网的服务器之间交叉备份,
比如A是数据库服务器,B是与之相连的Web服务器,需要在A中添加作业,自动把数据库备份到B上,
这时就需要考虑远程备份,将如下脚本添加到A数据库服务器的作业中即可。
原理:添加网络驱动器映射,将备份文件备份到远程目标,最后删除网络驱动器映射。
这里要配置的数据库名为:zynkhis ,共享文件夹名称: \\192.168.16.246\his_back共享使用账号guest 密码为空。
打开新建作业, 名称 为 remotebackup
所有者 :sa
类别: 数据库维护
新建步骤
步骤名称 : remotebacksql ,
选择数据库 masteer ,
输入以下sql语句。确定保存,测试执行成功远程备份。
--打开高级设置
EXEC sp_configure ''show advanced options'', 1
RECONFIGURE
--打开xp_cmdshell扩展存储过程
EXEC sp_configure ''xp_cmdshell'', 1
RECONFIGURE
declare @strdirname varchar(100)
declare @RemotePathstr varchar(100)
declare @LocalPathstr varchar(100)
declare @DBName varchar(100)
set @strdirname=replace(substring(convert(varchar(20),getdate(),120),1,10),''-'','''') --获得日期
set @RemotePathstr = ''z:\zynkhis''+@strdirname+''.bak'' --备份路径+命名+备份日期
set @DBName=''zynkhis''
--添加网络驱动器映射
exec master..xp_cmdshell ''net use z: \\192.168.16.246\his_backup "" /user:guest'' --备份机ip\登录身份(得拥有备份路径的写权限)
--实施备份
backup database @DBName to disk=@RemotePathstr
--删除映射
exec master..xp_cmdshell ''net use z: /delete''
--关闭xp_cmdshell扩展存储过程、高级设置
EXEC sp_configure ''xp_cmdshell'', 0
RECONFIGURE
EXEC sp_configure ''show advanced options'', 0
RECONFIGURE',
@database_name=N'master',
@flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id=@jobId, @name=N'remotebackupplan',
@enabled=1,
@freq_type=4,
@freq_interval=1,
@freq_subday_type=1,
@freq_subday_interval=0,
@freq_relative_interval=0,
@freq_recurrence_factor=0,
@active_start_date=20140328,
@active_end_date=99991231,
@active_start_time=10000,
@active_end_time=235959
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback:
IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:
打开作业计划,生成SQL 语句如下。
USE [msdb]
GO
/****** 对象: Job [remotebackup] 脚本日期: 03/28/2014 16:25:02 ******/
BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
/****** 对象: JobCategory [Database Maintenance] 脚本日期: 03/28/2014 16:25:02 ******/
IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'Database Maintenance' AND category_class=1)
BEGIN
EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'Database Maintenance'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
END
DECLARE @jobId BINARY(16)
EXEC @ReturnCode = msdb.dbo.sp_add_job @job_name=N'remotebackup',
@enabled=1,
@notify_level_eventlog=2,
@notify_level_email=0,
@notify_level_netsend=0,
@notify_level_page=0,
@delete_level=0,
@description=N'无描述。',
@category_name=N'Database Maintenance',
@owner_login_name=N'sa', @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** 对象: Step [remotebacksql] 脚本日期: 03/28/2014 16:25:02 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'remotebacksql',
@step_id=1,
@cmdexec_success_code=0,
@on_success_action=1,
@on_success_step_id=0,
@on_fail_action=2,
@on_fail_step_id=0,
@retry_attempts=0,
@retry_interval=0,
@os_run_priority=0, @subsystem=N'TSQL',
@command=N'
EXEC sp_configure ''show advanced options'', 1
RECONFIGURE
--打开xp_cmdshell扩展存储过程
EXEC sp_configure ''xp_cmdshell'', 1
RECONFIGURE
declare @strdirname varchar(100)
declare @RemotePathstr varchar(100)
declare @LocalPathstr varchar(100)
declare @DBName varchar(100)
set @strdirname=replace(substring(convert(varchar(20),getdate(),120),1,10),''-'','''') --获得日期
set @RemotePathstr = ''z:\zynkhis''+@strdirname+''.bak'' --备份路径+命名+备份日期
set @DBName=''zynkhis''
--添加网络驱动器映射
exec master..xp_cmdshell ''net use z: \\192.168.16.246\his_backup "" /user:guest''
--实施备份
backup database @DBName to disk=@RemotePathstr
--删除映射
exec master..xp_cmdshell ''net use z: /delete''
--关闭xp_cmdshell扩展存储过程、高级设置
EXEC sp_configure ''xp_cmdshell'', 0
RECONFIGURE
EXEC sp_configure ''show advanced options'', 0
RECONFIGURE',
@database_name=N'master',
@flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id=@jobId, @name=N'remotebackupplan',
@enabled=1,
@freq_type=4,
@freq_interval=1,
@freq_subday_type=1,
@freq_subday_interval=0,
@freq_relative_interval=0,
@freq_recurrence_factor=0,
@active_start_date=20140328,
@active_end_date=99991231,
@active_start_time=10000,
@active_end_time=235959
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback:
IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:
目录 返回
首页