这一篇博文探讨下SQL Server 2008 备份的实际操作,事实上SQL Server 2008并不关心数据是备份到物理磁盘上还是磁带上,在SQL Server 2008数据库备份中,预定义的目标位置叫做设备。这里设备是对硬盘,磁带机等备份存储的通称。通俗些讲,SQL Server 2008是把数据备份到备份设备中。当然,SQL Server 2008同样也支持直接把数据备份到物理磁盘的物理文件上。可以通过T-SQL,SSMS图形化,SMO等创建备份设备。
为了最大化保证数据的完整性和可用性,这一篇博文的Demo全部在完整恢复模式和100兼容级别下完成,可以通过下面的语句查看当前数据库的恢复模式和兼容级别,比如查看AdventureWorks2008数据库的恢复模式和兼容级别:
select
name,recovery_model_desc,compatibility_level
from sys.databases
where DB_NAME(database_id) = 'AdventureWorks2008'
where DB_NAME(database_id) = 'AdventureWorks2008'
![](http://terryli.blog.51cto.com/attachment/201101/150305729.jpg?x-oss-process=image/resize,w_1400/format,webp)
新创建的用户数据库与model 数据库使用相同的恢复模式。若希望新数据库使用简单恢复模式,请将model 的恢复模式更改为简单。默认master,msdb,tempdb为简单恢复模式,model为完整恢复模式。
当然也可以通过SSMS图形界面查看一个数据库的恢复模式和兼容级别,SSMS->右键单击某个数据库选择属性->选项,如下图:
![](http://terryli.blog.51cto.com/attachment/201101/150335791.jpg?x-oss-process=image/resize,w_1400/format,webp)
如何创建备份设别呢?可以通过
sp_addumpdevice
存储过程,语法如下:
sp_addumpdevice
[ @devtype = ] 'device_type'
, [ @logicalname = ] 'logical_name'
, [ @physicalname = ] 'physical_name'
device_type
表示备份设备的类型,可以使用disk(磁盘)或磁带(tape)这2个枚举,disk表示用硬盘文件作为备份设备。tape表示
windows
支持的任何磁带设备。tape在未来版本中可能将不再支持,所以尽量使用disk。
logical_name
表示备份设备的逻辑名称。在 BACKUP 和 RESTORE 语句中将会被使用。
physical_name
表示备份设备的物理名称。
通过磁盘文件来创建备份设备:
use master;
go
exec sp_addumpdevice 'disk', 'DemoDB2008ToDisk', 'c:\sql2008trainning\BackupDB\DemoDB2008Device.bak';
go
exec sp_addumpdevice 'disk', 'DemoDB2008ToDisk', 'c:\sql2008trainning\BackupDB\DemoDB2008Device.bak';
还可以通过网络共享磁盘文件来创建备份设备:
use master;
go
exec sp_addumpdevice 'disk', 'DemoDB2008ToDisk', '\\192.168.1.159\ShareBakFromLiTao\DemoDB2008Device.bak';
go
exec sp_addumpdevice 'disk', 'DemoDB2008ToDisk', '\\192.168.1.159\ShareBakFromLiTao\DemoDB2008Device.bak';
当然也可以通过SSMS图形化创建备份设备,因为比较简单,这里就不再演示。
下面就来演示创建各种数据库备份,比如我有一个DemoDB2008这样的数据库,那么使用备份设备DemoDB2008ToDisk创建各种备份的代码如下:
--完整备份数据库到备份设备
backup database DemoDB2008
to DemoDB2008ToDisk --备份设备
with name= 'DemoDB2008 完整备份',
description= 'DemoDB2008 backup'
--差异备份数据库到备份设备
backup database DemoDB2008
to DemoDB2008ToDisk
with differential, name= 'DemoDB2008 差异备份'
--备份事务日志到备份设备
backup log DemoDB2008
to DemoDB2008ToDisk
with name = 'DemoDB2008 日志备份'
--仅拷贝数据库到备份设备
backup database DemoDB2008
to DemoDB2008ToDisk
with copy_only
--尾日志备份--
backup log DemoDB2008
to DemoDB2008ToDisk
with name = 'DemoDB2008 尾日志备份',norecovery
backup database DemoDB2008
to DemoDB2008ToDisk --备份设备
with name= 'DemoDB2008 完整备份',
description= 'DemoDB2008 backup'
--差异备份数据库到备份设备
backup database DemoDB2008
to DemoDB2008ToDisk
with differential, name= 'DemoDB2008 差异备份'
--备份事务日志到备份设备
backup log DemoDB2008
to DemoDB2008ToDisk
with name = 'DemoDB2008 日志备份'
--仅拷贝数据库到备份设备
backup database DemoDB2008
to DemoDB2008ToDisk
with copy_only
--尾日志备份--
backup log DemoDB2008
to DemoDB2008ToDisk
with name = 'DemoDB2008 尾日志备份',norecovery
为了演示部分备份和文件备份,这里提供一个合适的数据库,同时假定阅读这篇博文的朋友已经熟悉数据库文件类型和文件组类型,创建数据库的代码如下:
create
database DemoDBForPartial
on primary
(
name = N 'demoDB',
filename = N 'c:\dbFiles\demoDB.mdf',
size = 3,
maxsize = unlimited,
filegrowth = 1
),
(
name = N 'demoDB01',
filename = N 'c:\dbFiles\demoDB01.ndf',
size = 5,
maxsize = 1000,
filegrowth = 2
),
filegroup fgJanuary
(
name = N 'demoDB02',
filename = N 'c:\dbFiles\demoDB02.ndf',
size = 10,
maxsize = 30,
filegrowth = 5
),
filegroup fgFebruary
(
name = N 'demoDB03',
filename = N 'c:\dbFiles\demoDB03.ndf',
size = 25,
maxsize = 928,
filegrowth = 20
),
(
name = N 'demoDB04',
filename = N 'c:\dbFiles\demoDB04.ndf',
size = 25,
maxsize = 1024,
filegrowth = 20
),
filegroup fgMarch
(
name = N 'demoDB05',
filename = N 'c:\dbFiles\demoDB05.ndf',
size = 25,
maxsize = 200,
filegrowth = 20
),
(
name = N 'demoDB06',
filename = N 'c:\dbFiles\demoDB06.ndf',
size = 25,
maxsize = 50,
filegrowth = 20
)
log on
(
name = N 'demoDB_log',
filename = N 'c:\dbFiles\demoDB_log.ldf',
size = 1,
maxsize = 1000,
filegrowth = 10%
)
on primary
(
name = N 'demoDB',
filename = N 'c:\dbFiles\demoDB.mdf',
size = 3,
maxsize = unlimited,
filegrowth = 1
),
(
name = N 'demoDB01',
filename = N 'c:\dbFiles\demoDB01.ndf',
size = 5,
maxsize = 1000,
filegrowth = 2
),
filegroup fgJanuary
(
name = N 'demoDB02',
filename = N 'c:\dbFiles\demoDB02.ndf',
size = 10,
maxsize = 30,
filegrowth = 5
),
filegroup fgFebruary
(
name = N 'demoDB03',
filename = N 'c:\dbFiles\demoDB03.ndf',
size = 25,
maxsize = 928,
filegrowth = 20
),
(
name = N 'demoDB04',
filename = N 'c:\dbFiles\demoDB04.ndf',
size = 25,
maxsize = 1024,
filegrowth = 20
),
filegroup fgMarch
(
name = N 'demoDB05',
filename = N 'c:\dbFiles\demoDB05.ndf',
size = 25,
maxsize = 200,
filegrowth = 20
),
(
name = N 'demoDB06',
filename = N 'c:\dbFiles\demoDB06.ndf',
size = 25,
maxsize = 50,
filegrowth = 20
)
log on
(
name = N 'demoDB_log',
filename = N 'c:\dbFiles\demoDB_log.ldf',
size = 1,
maxsize = 1000,
filegrowth = 10%
)
首先创建备份设备DemoDBForPartialToDisk,如下:
use master;
go
exec sp_addumpdevice 'disk', 'DemoDBForPartialToDisk', 'c:\sql2008trainning\BackupDB\DemoDBForPartialDevice.bak';
go
exec sp_addumpdevice 'disk', 'DemoDBForPartialToDisk', 'c:\sql2008trainning\BackupDB\DemoDBForPartialDevice.bak';
部分备份包含主文件组、每个读写文件组以及任何指定(可选)的只读文件组中的所有数据。
第一个
部分备份
演示:由于刚创建的数据库
DemoDBForPartial
没有只读文件组,所以这个备份只包含主文件组和每个读写文件组,代码如下:
backup
database DemoDBForPartial read_write_filegroups
to DemoDBForPartialToDisk
to DemoDBForPartialToDisk
第二个部分备份演示:为了演示备份只读文件组,先修改数据库,代码如下:
alter
database DemoDBForPartial modify filegroup fgJanuary readonly
alter database DemoDBForPartial modify filegroup fgMarch readonly
alter database DemoDBForPartial modify filegroup fgMarch readonly
接着再次进行部分备份,这次指定一个只读文件组fgMarch,代码如下:
backup
database DemoDBForPartial read_write_filegroups,filegroup =
'fgMarch'
to DemoDBForPartialToDisk
to DemoDBForPartialToDisk
文件备份包含一个或多个文件(或文件组)中的所有数据,文件备份的代码如下:
文件组备份:
backup
database DemoDBForPartial filegroup =
'primary', filegroup =
'fgFebruary' , filegroup =
'fgmarch'
to DemoDBForPartialToDisk
with name = 'demodbforpartial-完整文件和文件组备份'
backup database DemoDBForPartial file = 'demodb05', file = 'demodb03', filegroup = 'primary'
to DemoDBForPartialToDisk
with name = 'demodbforpartial-完整文件和文件组备份'
to DemoDBForPartialToDisk
with name = 'demodbforpartial-完整文件和文件组备份'
backup database DemoDBForPartial file = 'demodb05', file = 'demodb03', filegroup = 'primary'
to DemoDBForPartialToDisk
with name = 'demodbforpartial-完整文件和文件组备份'
文件备份:
backup
database DemoDBForPartial
file =
'demodb03',
file =
'demodb05'
to DemoDBForPartialToDisk
with name = 'demodbforpartial-完整文件备份'
to DemoDBForPartialToDisk
with name = 'demodbforpartial-完整文件备份'
差异文件组备份:
backup
database DemoDBForPartial filegroup =
'primary', filegroup =
'fgFebruary' , filegroup =
'fgmarch'
to DemoDBForPartialToDisk
with name = 'demodbforpartial-差异文件组备份',differential
to DemoDBForPartialToDisk
with name = 'demodbforpartial-差异文件组备份',differential
如果你不习惯备份到备份设备,也可以直接备份到磁盘文件,代码如下:
--完整备份数据库到磁盘文件
backup database DemoDB2008 --备份数据库
to disk= 'c:\sql2008trainning\BackupDB\DemoDB2008.bak'
with name= 'DemoDB2008 完整备份',
description= 'DemoDB2008 backup'
--差异备份数据库到磁盘文件
backup database DemoDB2008
to disk = 'c:\sql2008trainning\BackupDB\DemoDB2008.bak'
with differential, name= 'DemoDB2008 差异备份'
--备份事务日志到磁盘文件
backup log DemoDB2008
to disk = 'c:\sql2008trainning\BackupDB\DemoDB2008.bak'
with name = 'DemoDB2008 日志备份'
--拷贝数据库到磁盘文件
backup database DemoDB2008
to disk= 'c:\sql2008trainning\BackupDB\DemoDB2008_copy.bak'
with copy_only
backup database DemoDB2008 --备份数据库
to disk= 'c:\sql2008trainning\BackupDB\DemoDB2008.bak'
with name= 'DemoDB2008 完整备份',
description= 'DemoDB2008 backup'
--差异备份数据库到磁盘文件
backup database DemoDB2008
to disk = 'c:\sql2008trainning\BackupDB\DemoDB2008.bak'
with differential, name= 'DemoDB2008 差异备份'
--备份事务日志到磁盘文件
backup log DemoDB2008
to disk = 'c:\sql2008trainning\BackupDB\DemoDB2008.bak'
with name = 'DemoDB2008 日志备份'
--拷贝数据库到磁盘文件
backup database DemoDB2008
to disk= 'c:\sql2008trainning\BackupDB\DemoDB2008_copy.bak'
with copy_only
本文转自terryli51CTO博客,原文链接:http://blog.51cto.com/terryli/485782 ,如需转载请自行联系原作者