使用存檔數據庫archive database實現Sybase ASE對象級別的恢復

存檔數據庫訪問通過將數據庫轉儲(“存檔”)視作傳統的只讀數據庫,從而允許數據庫管理員驗證或選擇性地恢復數據庫轉儲中的數據,此類數據庫也稱作“存檔數據庫”。

與傳統數據庫不同,存檔數據庫使用實際的數據庫轉儲作爲其主磁盤存儲設備,利用最小的傳統存儲量來表示數據庫轉儲恢復過程中產生的新頁或修改頁。由於數據庫轉儲已包含許多(或大多數)數據庫頁的映像,因此不必使用 Backup Server 將頁從存檔轉換爲傳統的數據庫存儲,就可以裝載存檔數據庫。因此,裝載速度明顯快於傳統數據庫。
 
存檔數據庫的用途:
1.對從生產數據庫生成的轉儲的最新副本運行數據庫一致性檢查。
2.檢查數據庫轉儲的完整性。
3.數據庫轉儲的對象級別恢復。

使用存檔數據庫的典型步驟:

1.創建scratch數據庫

use master
go

disk init
name='scratchdb_dat',
physname='d:\syb_data\scratchdb_dat.dat',
size='10m'
go

disk init
name='scratchdb_log',
physname='d:\syb_data\scratchdb_log.dat',
size='5m'
go

create database scratchdb on scratchdb_dat='10m' log on scratchdb_log='5m'
go

2.將剛創建的數據庫指定爲空數據庫
sp_dboption "scratchdb","scratch database","true"
go

打開檢查點自動截斷日誌的選項:
sp_dboption "scratchdb","trunc. log on chkpt","true"
go


3.創建存檔數據庫(archive database)

use master
go
disk init
name='archivedb_dev',
physname='d:\syb_data\archivedb_dev.dat',
size='20m'
go

create archive database archivedb
on archivedb_dev='20m'
with scratch_database = scratchdb
go

使用sp_helpdb archivedb查看數據庫狀態,

1> sp_helpdb archivedb
2> go
 name      db_size       owner dbid created
         status
 ——— ————- —– —- ————
         ————————————————————————–
 archivedb       20.0 MB sa       6 Jul 15, 2011
         don't recover, read only, no free space acctg, mixed log and data, archive

(1 row affected)
 name      attribute_class         attribute        int_value char_value
         comments
 ——— ———————– —————- ——— ———-
         ——–
 archivedb Archive Database Access scratch database      NULL scratchdb
         NULL
 device_fragments               size          usage
         created                   free kbytes
 —————————— ————- ——————–
         ————————- —————-
 archivedb_dev                        20.0 MB data only
         Jul 15 2011 10:35AM                  20396
(return status = 0)
1> use archivedb
2> go
Msg 966, Level 14, State 1:
Server 'TEST', Line 1:
The archive database with id '6' is unavailable as it is yet to be loaded.

可以看到存檔數據庫archivedb是隻讀的,目前不可訪問。

4.裝載一個數據庫轉儲文件
load database archivedb from "d:\test.dmp"
go

5.使數據庫聯機
online database archivedb
go

online database 執行撤消恢復操作,在此期間,已修改或分配的頁可能會重新映射到修改頁面區域。
若裝載數據庫時使用了 with norecovery,則您不需要將此數據庫聯機,因爲裝載過程會自動使數據庫聯機,而不用運行恢復撤消過程。

6.使用 dbcc 命令檢查存檔數據庫的一致性
dbcc checkdb(archivedb)
go

在上面的裝載數據庫轉儲文件步驟中也可以使用:
1> load database archivedb from "d:\test.dmp" with norecovery
2> go
The archive database 'archivedb' has been brought online automatically. It may
have some inconsistencies as a result of not running recovery.

load database 命令的 with norecovery 選項允許將數據庫轉儲裝載到存檔數據庫中而不恢復任何數據,從而可減少裝載所需的時間。

 

演示對象級別的恢復
 

1.準備測試環境

–*****************************************************
use PDB
go
create table obj_level_recover(id int not null,name varchar(30) null)
go
insert into obj_level_recover
select id,name from sysobjects 
go
commit
go

select getdate()
go
dump database PDB to 'd:\PDB.dmp'
go

–*****************************************************
–誤更新數據
update obj_level_recover
set id=id+10
go
–創建另外一張測試表
create table testtbl(id int not null,name varchar(50) null)
go
insert into testtbl values(1,'存檔數據庫 archive database')
insert into testtbl values(2,'對象級別恢復 object level recovery')
go

select getdate()
go
–日誌備份1
dump tran PDB to 'd:\PDB_tran1.dmp'
go
–*****************************************************

–誤刪除表obj_level_recover
drop table obj_level_recover
go
insert into testtbl values(3,'load database … with norecovery')
go
commit
go
select getdate()
go
–誤刪除表 testtbl
drop table testtbl
go
–日誌備份2
dump tran PDB to 'd:\PDB_tran2.dmp'
go
–*****************************************************
 

分析測試環境中的備份策略,發現表obj_level_recover在全備後被誤更新,且更新後的數據保留在了日誌備份1中。測試表testtbl在日誌備份1後被誤刪除,數據庫日誌備份2中包含測試表testtbl新插入的記錄但是該表已被刪除。

 

2.對象級別的恢復步驟

繼續使用上面創建好的存檔數據庫archivedb。爲了恢復誤刪除的表obj_level_recover,首先做一個全庫的恢復。

load database archivedb from 'd:\PDB.dmp'
go
online database archivedb
go

導出誤刪除表obj_level_recover的數據,使用bcp或者select into。

bcp archivedb..obj_level_recover out d:\obj_level_recover.bcpout -c -Usa -P -Stest

或者

select * into tempdb.dbo.BAK_obj_level_recover from archivedb..obj_level_recover

注意:select into新創建的表不要放在存檔數據庫archivedb中,因爲archivedb是隻讀的。

 

恢復另外一張測試表testtbl的數據,

load tran archivedb from 'd:\PDB_tran1.dmp'

go

online database archivedb

go

use archivedb

go

1> select * from testtbl
2> go
 id          name
 ———– ————————————————–
           1 存檔數據庫 archive database
           2 對象級別恢復 object level recovery

(2 rows affected)

以上只恢復了測試表testtbl的部分數據。在日誌備份1後,測試表testtbl也進行了一些更新操作。如果想恢復testtbl在被刪除前的數據的話,需要使用基於時間點的日誌恢復技術。存檔數據庫的日誌恢復不具備時間點恢復的功能。只能使用傳統的數據庫恢復方法了。

use master
go
load tran archivedb from 'd:\PDB_tran2.dmp'
go
online database archivedb
go

1> select *  from testtbl
2> go
Msg 208, Level 16, State 1:
Server 'TEST', Line 2:
testtbl not found. Specify owner.objectname or use sp_help to check whether the object exists (sp_help may produce lots of output).

可見,使用存檔數據庫的日誌恢復功能不能實現恢復表到誤操作前的狀態的功能!

 

查看存檔數據庫相關的屬性

1> sp_helpdb archivedb
2> go
 name      db_size       owner dbid created      status
 ——— ————- —– —- ———— —————————————
 archivedb       20.0 MB sa       6 Jul 15, 2011 read only, no free space acctg, archive

(1 row affected)
 name      attribute_class         attribute               int_value char_value       comments
 ——— ———————– ———————– ——— —————- ——–
 archivedb Archive Database Access scratch database             NULL scratchdb        NULL
 archivedb Archive Database Access dump device                     0 d:\PDB.dmp       NULL
 archivedb Archive Database Access transaction dump device         2 d:\PDB_tran2.dmp NULL
 device_fragments               size          usage                created                   free kbytes
 —————————— ————- ——————– ————————- —————-
 archivedb_dev                        19.0 MB data only            Jul 15 2011 11:37AM                  19244
 archivedb_dev                         1.0 MB data only            Jul 15 2011 11:37AM                   1008
(return status = 0)

1> select * from master.dbo.sysusages where dbid=6
2> go
 dbid   segmap      lstart      size        vstart      pad    unreservedpgs crdate                     vdevno
 —— ———– ———– ———– ———– —— ————- ————————– ———–
      6         128           0        4864           0   NULL          4811        Jul 15 2011 11:37AM          11
      6         256        4864         256        9728   NULL           252        Jul 15 2011 11:37AM          11

(2 rows affected)

查看存檔數據庫archivedb的虛擬設備:

select class,attribute,object_type,object_cinfo,object from master.dbo.sysattributes where class=28

1> sp_autoformat 'sysattributes','class,attribute,object_type,object_cinfo,object',' where class=28'
2> go
 class attribute object_type object_cinfo object
 —– ——— ———– ———— ——
    28         0 D           scratchdb         6
    28         1 D           SYSDEV$__12       6
    28         2 D           SYSDEV$__14       6

(3 rows affected)

存檔數據庫的虛擬邏輯設備信息:

1> sp_helpdevice SYSDEV$__12
2> go
 device_name physical_name description                                                   status cntrltype vdevno vpn_low
 vpn_high
 ———– ————- ————————————————————- —— ——— —— ——-
 ——–
 SYSDEV$__12 d:\PDB.dmp    file system device, special, archive database disk, read-only     12         0     12       0
     1253

(1 row affected)
(return status = 0)
1> sp_helpdevice SYSDEV$__14
2> go
 device_name physical_name    description                                                   status cntrltype vdevno vpn_
low vpn_high
 ———– —————- ————————————————————- —— ——— —— —-
— ——–
 SYSDEV$__14 d:\PDB_tran2.dmp file system device, special, archive database disk, read-only     12         0     14
  0       66

(1 row affected)
(return status = 0)

查看存檔數據庫的頁面映射關係表:

1> select top 10 *  from scratchdb..sysaltusages
2> go
 dbid   location    lstart      size        vstart      vdevno      segmap
 —— ———– ———– ———– ———– ———– ———–
      6           8          24           1       10234          11           3
      6           8          80           1       10236          11           3
      6           5           0          32           8          12           3
      6           5          32         227          74          12           3
      6           5         264           2         528          12           3
      6           5         272           1         532          12           3
      6           5         280           2         534          12           3
      6           5         288           1         538          12           3
      6           5         296           2         540          12           3
      6           5         304           1         544          12           3

(10 rows affected)

刪除歸檔數據庫:

drop database archivedb

go

再次查看存檔數據庫相關的信息:

1> select count(*) from master.dbo.sysattributes where class=28
2> go

 ———–
           0

(1 row affected)
1> select name from master..sysdevices where name in ('SYSDEV$__12','SYSDEV$__14')
2> go
 name
 ——————————

(0 rows affected)
1> select count(*) from scratchdb..sysaltusages
2> go

 ———–
           0

(1 row affected)

可見,系統表sysdevices中的虛擬邏輯設備'SYSDEV$__12','SYSDEV$__14'被刪除,“空數據庫”scratchdb中的頁面映射表sysaltusages也被清空了。

–EOF–

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章