首页 > 数据库 > SQL Server 2005 自动远程数据库备份
2014
12-11

SQL Server 2005 自动远程数据库备份

  由于项目需要,需要对目标服务器上的数据库每天进行备份并转移,查阅网上的一些帮助,结合自己的实际需要,写了这篇文章,希望对有同样需求的朋友有所帮助。目标服务器:192.168.1.197,备份服务器:192.168.0.194

  1、在备份服务器上新建一个文件夹(我的是C:\DbBackupTest),首先要确定对备份服务器要有足够的权限(权限设置如下图所示),最好是“Administrators”组账户。其次 到备份服务器上,打开组策略->计算机配置->Windows设置->安全设置->本地策略->安全选项->网络访问:本地账户的共享和安全模式->经典-本地用户以自己的身份验证。

3f40ba8a-4285-4be2-9e47-891e5661fab3

  2.1、在目标服务器上打开Sql Server 2005,新建一个作业,然后新建一个“步骤”(如下图所示),指定目标数据库后,编写如下脚本(注意空格和反斜线),然后点击“确定”,完成第一步。

  –开启xp_cmdshell

  EXEC sp_configure ‘show advanced options’, 1;RECONFIGURE;

  EXEC sp_configure ‘xp_cmdshell’, 1;RECONFIGURE;

  –建立连接

  Exec xp_cmdshell ‘net use \\192.168.0.194\DbBackupTest  “这里填写备份服务器的登录密码”/user:192.168.0.194\administrator’

  –备份数据库

  Declare @Path Nvarchar(200)

  Set @Path = ‘\\192.168.0.194\DbBackupTest\DigitalGolf_New_’ + Convert(Nvarchar,Getdate(),112) + ‘.bak’

  Backup Database DigitalGolf_New

  To disk = @Path

  With Format

  –断开连接

  Exec xp_cmdshell ‘net use \\192.168.0.194\DbBackupTest  /delete’

0f8f0d04-6d61-4685-9c2d-525e82c08c31

  2.2、新建“计划”,如下图所示:

740fbde5-e515-40ef-bad6-ed63c4b99fd9

  3、经过上面的两步,就已经完成了,你可以直接在作业上点击右键执行测试是否已经备份成功^-^。

  4、相关的帮助链接:

  http://tech.huweishen.com/gongju/1423.html
  http://www.cnblogs.com/059212315/archive/2012/08/10/2631627.html
  http://hi.baidu.com/ziyoubird/item/03440b226f5a824646996290
  http://database.51cto.com/art/201011/233117.htm

  4、更多的备份的形式参考帮助如下:(转载自:http://hi.baidu.com/ziyoubird/item/03440b226f5a824646996290

  –完整备份
  Backup Database NorthwindCS
  To disk=’G:\Backup\NorthwindCS_Full_20070908.bak’

  –差异备份
  Backup Database NorthwindCS
  To disk=’G:\Backup\NorthwindCS_Diff_20070908.bak’
  With Differential

  –日志备份,默认截断日志
  Backup Log NorthwindCS
  To disk=’G:\Backup\NorthwindCS_Log_20070908.bak’

  –日志备份,不截断日志
  Backup Log NorthwindCS
  To disk=’G:\Backup\NorthwindCS_Log_20070908.bak’
  With No_Truncate

  –截断日志不保留
  Backup Log NorthwindCS
  With No_Log

  –或者
  Backup Log NorthwindCS
  With Truncate_Only

  –截断之后日志文件不会变小
  –有必要可以进行收缩

  –文件备份
  Exec Sp_Helpdb NorthwindCS –查看数据文件
  Backup Database NorthwindCS
  File=’NorthwindCS’ –数据文件的逻辑名
  To disk=’G:\Backup\NorthwindCS_File_20070908.bak’

  –文件组备份
  Exec Sp_Helpdb NorthwindCS –查看数据文件
  Backup Database NorthwindCS
  FileGroup=’Primary’ –数据文件的逻辑名
  To disk=’G:\Backup\NorthwindCS_FileGroup_20070908.bak’
  With init

  –分割备份到多个目标
  –恢复的时候不允许丢失任何一个目标
  Backup Database NorthwindCS
  To disk=’G:\Backup\NorthwindCS_Full_1.bak’
  ,disk=’G:\Backup\NorthwindCS_Full_2.bak’

  –镜像备份
  –每个目标都是相同的
  Backup Database NorthwindCS
  To disk=’G:\Backup\NorthwindCS_Mirror_1.bak’
  Mirror
  To disk=’G:\Backup\NorthwindCS_Mirror_2.bak’
  With Format –第一次做镜像备份的时候格式化目标

  –镜像备份到本地和远程
  Backup Database NorthwindCS
  To disk=’G:\Backup\NorthwindCS_Mirror_1.bak’
  Mirror
  To disk=’\\192.168.1.200\Backup\NorthwindCS_Mirror_2.bak’
  With Format

  –每天生成一个备份文件
  Declare @Path Nvarchar(2000)
  Set @Path =’G:\Backup\NorthwindCS_Full_’
  +Convert(Nvarchar,Getdate(),112)+’.bak’
  Backup Database NorthwindCS
  To http://www.hack58.net/Article/html/3/7/2008/mailt disk=@Path

  –从NoRecovery或者
  –Standby模式恢复数据库为可用
  Restore Database NorthwindCS_Bak
  With Recovery

  –查看目标备份中的备份集
  Restore HeaderOnly
  From Disk =’G:\Backup\NorthwindCS_Full_20070908.bak’

  –查看目标备份的第一个备份集的信息
  Restore FileListOnly
  From Disk =’G:\Backup\NorthwindCS_Full_20070908_2.bak’
  With File=1

  –查看目标备份的卷标
  Restore LabelOnly
  From Disk =’G:\Backup\NorthwindCS_Full_20070908_2.bak’

  –备份设置密码保护备份
  Backup Database NorthwindCS
  To disk=’G:\Backup\NorthwindCS_Full_20070908.bak’
  With Password = ’123′,init
  Restore Database NorthwindCS
  From disk=’G:\Backup\NorthwindCS_Full_20070908.bak’
  With Password = ’123′

编程技巧