只要发生数据传输数据存储和数据交换,就有可能产生数据故障。这时,如果没有采取数据备份和数据恢复手段与措施,就会导致数据的丢失。有时候造成的损失是无法弥补和无法估量的。
下面分别对Oracle 、SQL sever、DB2备份及恢复时间进行测试:
一、Oracle基于时间点的恢复
(1)首先做好冷备份
(2)创建测试数据
其语名如下:
D:/>sqlplus "/ as sysdba"
SQL*Plus: Release 9.2.0.6.0 - Production on Thurs Jun 14 11:56:43 2007
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
Connected to an idle instance.
11:56:44 SQL> startup
ORACLE instance started.
Total System Global Area 101785428 bytes
Fixed Size 454484 bytes
Variable Size 75497472 bytes
Database Buffers 25165824 bytes
Redo Buffers 667648 bytes
Database mounted.
Database opened.
11:57:01 SQL> create table test (name varchar2(20));
Table created.
Elapsed: 00:00:00.04
11:57:23 SQL> insert into test values('aaaaaaaaaaaaaaaaaaaa');
1 row created.
Elapsed: 00:00:00.00
11:57:23 SQL> insert into test values('bbbbbbbbbbbbbbbbbbbb');
1 row created.
Elapsed: 00:00:00.00
11:57:23 SQL> insert into test values('cccccccccccccccccccc');
1 row created.
Elapsed: 00:00:00.00
11:57:24 SQL> commit;
Commit complete.
Elapsed: 00:00:00.00
11:57:28 SQL>
--注意这个时间,是Commit完成时间
11:57:29 SQL> drop table test;
Table dropped.
Elapsed: 00:00:00.07
11:57:34 SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
11:57:45 SQL> exit
Disconnected from Oracle9i Enterprise Edition Release 9.2.0.6.0 - Production
With the Partitioning, OLAP and Oracle Data Mining options
Server Release 9.2.0.6.0 - Production
(3)恢复备份数据
保留当前日志
D:/>sqlplus "/ as sysdba"
SQL*Plus: Release 9.2.0.6.0 - Production on Thurs Jun 14 11:58:04 2007
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
Connected to an idle instance.
11:58:04 SQL> startup mount;
ORACLE instance started.
Total System Global Area 101785428 bytes
Fixed Size 454484 bytes
Variable Size 75497472 bytes
Database Buffers 25165824 bytes
Redo Buffers 667648 bytes
Database mounted.
11:58:15 SQL> alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';
Session altered.
Elapsed: 00:00:00.00
11:58:17 SQL> recover database until time '2007-06-14 11:57:28';
Media recovery complete.
--恢复到提交完成时刻
11:58:33 SQL> alter database open resetlogs;
Database altered.
Elapsed: 00:00:05.08
11:58:46 SQL> select * from test;
no rows selected
Elapsed: 00:00:00.00
结论:
Oracle能够恢复的时间精度为1秒,但是在Oracle数据库内部,用以产生SCN的时间点有更为精确的精度。所以,如果你指定秒级恢复,如11:57:28
二、DB2 数据库备份及恢复的测试
(1)备份数据库
[db2inst1@bytest ~ 22]$db2 backup db sample online include logs
Backup successful. The timestamp for this backup image is : 20070614120837[db2inst1@bytest ~ 23]$
(2)删除数据库
[db2inst1@bytest ~ 5]$db2 drop db sample
SQL1035N The database is currently in use. SQLSTATE=57019
[db2inst1@bytest ~ 6]$db2 force application all
DB20000I The FORCE APPLICATION command completed successfully.
DB21024I This command is asynchronous and may not be effective immediately.[db2inst1@bytest ~ 7]$db2 drop db sample
DB20000I The DROP DATABASE command completed successfully.
[db2inst1@bytest ~ 8]$
(3)恢复数据库
[db2inst1@bytest~27]$db2 restore db sample taken at 20070614120837 logtarget /home/db2inst1/db2log/
DB20000I The RESTORE DATABASE command completed successfully.
[db2inst1@bytest ~ 28]$
(4)查看时间标记
根据时间标记可以在上面截取如下信息:
Op Obj Timestamp+Sequence Type Dev Earliest Log Current Log Backup ID
-- --- ------------------ ---- --- ------------ ------------ --------------
B D 20070614120837001 N D S0000000.LOG S0000000.LOG
----------------------------------------------------------------------------
Contains 2 tablespace(s):00001 SYSCATSPACE
00002 USERSPACE1
----------------------------------------------------------------------------
Comment: DB2 BACKUP SAMPLE ONLINE
Start Time: 20070614120837
End Time: 20070614120902
Status: A
----------------------------------------------------------------------------
EID: 2 Location: /home/db2inst1
从这段信息中可以得知备份的开始时间(Start Time: 20070614120837)和结束时间(End Time: 20070614120902)备份时间耗时:25分钟
以及进行前滚所需要的日志(S0000000.LOG),该日志文件已经在/home/db2inst1/db2log/中。其中,End Time: 20070614120902为备份的结束时间,同时它也是前滚时的最小恢复时间点(PIT),也就是说,
如果按照指定时间点恢复,最小要恢复到这个时间点,如果用提前于这个时间的时间来前滚,将会返回信息提示:
[db2inst1@bytest~31]$db2” Rollforward db sample to 2007-06-14-12.09.00.000000 using local time and stop overflow log path(/home/db2inst1/db2log/)“
SQL1275N The stoptime passed to roll-forward must be greater than or equal to
"2007-06-14-12.09.02.000000", because database "SAMPLE" on node(s) "0"
contains information later than the specified time.
[db2inst1@bytest ~ 32]$
所以至少要恢复到这个时间点。命令如下:
[db2inst1@bytest ~ 33]$db2 "rollforward db sample to 2007-06-14-12.09.02.000000 using local time and stop overflow log path(/home/db2inst1/db2log/)"Rollforward StatusInput database alias = sample
Number of nodes have returned status = 1Node number = 0
Rollforward status = not pending
Next log file to be read =
Log files processed = S0000000.LOG - S0000000.LOG
Last committed transaction = 2007-06-14-12.09.02.000000DB20000I The ROLLFORWARD command completed successfully.[db2inst1@bytest ~ 34]$db2 connect to sampleDatabase Connection InformationDatabase server = DB2/LINUX 8.2.0
SQL authorization ID = DB2INST1
Local database alias = SAMPLE[db2inst1@bytest ~ 35]$此时,数据库是可用状态。
三、SQL sever 备份及恢复的测试
(1) 建一个测试数据库test
(2) 建一个表,插入点记录
create table a(c1 varchar(2))
go
insert into a values('aa')
go
insert into a values('bb')
go
(3) 作完全备份,到文件test_1.bak
(4)在作一点修改
insert into a values('cc')
go
create table b(c1 int)
go
insert into b values(1)
go
insert into b values(2)
go
(5)shutdown 数据库服务器
(6) 用ultraedit编辑数据库文件test_data.mdf,随便修改点字节内容,相当于数据库遭到致命的损坏
(7) 启动数据库,并且运行企业管理器,点开数据库,看到test变成灰色,而且显示置疑
(8) 运行isql -SLocalhost -Usa -P
1> backup log test TO DISK='D:Program FilesMicrosoft SQL ServerMSSQLBACKUP
est_2.bak' WITH NO_TRUNCATE
2>go
已处理 2 页,这些页属于数据库 'test' 的文件 'TEST_Log'(位于文件 1 上)。
BACKUP LOG 操作成功地处理了 2 页,花费了 0.111 秒(0.087 MB/秒)。
(9) 进行恢复最老的完全备份
1> RESTORE DATABASE test FROM DISK='D:Program FilesMicrosoft SQL ServerMSSQL
BACKUP est_1.bak' WITH NORECOVERY
2> go
已处理 96 页,这些页属于数据库 'test' 的文件 'TEST_Data'(位于文件 1 上)。
已处理 1 页,这些页属于数据库 'test' 的文件 'TEST_Log'(位于文件 1 上)。
RESTORE DATABASE 操作成功地处理了 97 页,花费了 0.107 秒(7.368 MB/秒)。
(10) 恢复最近的日志
1> RESTORE LOG test FROM DISK='D:Program FilesMicrosoft SQL ServerMSSQLBACKU
P est_2.bak' WITH RECOVERY
2> go
已处理 2 页,这些页属于数据库 'test' 的文件 'TEST_Log'(位于文件 1 上)。
RESTORE LOG 操作成功地处理了 2 页,花费了 0.056 秒(0.173 MB/秒)。
数据已经完全恢复了,可以使用了。
select * from a
go
总结,应该先有一个完善的数据库备份计划。在本例中,如果没有一个完全备份的话,数据库的恢复就不可能。