KingbaseES V8R6 备份恢复案例 -- 自定义表空间指定目录恢复
案例说明:
KingbaseES V8R6在通过sys_rman执行物理备份恢复时,可以通过参数‘--kb1-path’,指定恢复的数据(data)目录,但如果原备份中包含自定义表空间时,需要建立表空间映射,再执行恢复。
适用版本:
KingbaseES V8R6
一、案例复现
1、建立自定义表空间
test=# create tablespace user01 location '/data/kingbase/v8r6_C7/tbs';
CREATE TABLESPACE
test=# \db+
List of tablespaces
Name | Owner | Location | Access privileges | Options | Size | Description
-------------+--------+----------------------------+-------------------+---------+---------+-------------
sys_default | system | | | | 64 MB |
sys_global | system | | | | 658 kB |
sysaudit | system | | | | 24 kB |
user01 | system | /data/kingbase/v8r6_C7/tbs | | | 0 bytes |
(4 rows)
prod=# create table u_tbs1 (id int ,name varchar(10)) tablespace user01;
CREATE TABLE
prod=# \d+ u_tbs1;
Table "public.u_tbs1"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
--------+----------------------------+-----------+----------+---------+----------+--------------+-------------
id | integer | | | | plain | |
name | character varying(10 char) | | | | extended | |
Tablespace: "user01"
Access method: heap
2、执行数据库的全备
[kingbase@node102 bin]$ /opt/Kingbase/ES/V8R6_C7/Server/bin/sys_rman --config=/home/kingbase/kbbr_repo/sys_rman.conf --stanza=kingbase --archive-copy --type=full backup
3、查看数据库备份信息
[kingbase@node102 bin]$ ./sys_rman --config=/home/kingbase/kbbr_repo/sys_rman.conf --stanza=kingbase info
stanza: kingbase
status: ok
cipher: none
db (current)
wal archive min/max (V008R006C007B0012): 000000010000000000000001/000000010000000000000005
full backup: 20230202-144709F
timestamp start/stop: 2023-02-02 14:47:09 / 2023-02-02 14:47:12
wal start/stop: 000000010000000000000005 / 000000010000000000000005
database size: 81.8MB, database backup size: 81.8MB
repo1: backup set size: 81.8MB, backup size: 81.8MB
4、关闭数据库服务执行备份恢复
1)关闭数据库服务
[kingbase@node102 bin]$ ./sys_ctl stop -D /data/kingbase/v8r6_C7/data/
waiting for server to shut down.... done
server stopped
2)建立自定义恢复目录并恢复数据库
# 建立恢复目录
[kingbase@node102 v8r6_C7]$ mkdir -p /data/kingbase/v8c7/data
#执行数据恢复
[kingbase@node102 bin]$ /opt/Kingbase/ES/V8R6_C7/Server/bin/sys_rman --config=/home/kingbase/kbbr_repo/sys_rman.conf --stanza=kingbase --kb1-path=/data/kingbase/v8c7/data --archive-mode=off restore
2023-02-02 14:51:56.189 P00 INFO: restore command begin 2.27: --archive-mode=off --band-width=0 --config=/home/kingbase/kbbr_repo/sys_rman.conf --exec-id=22745-9736a61a --link-all --log-level-console=info --log-level-file=info --log-path=/opt/Kingbase/ES/V8R6_C7/Server/log --log-subprocess --non-archived-space=1024 --kb1-path=/data/kingbase/v8c7/data --process-max=4 --repo1-path=/home/kingbase/kbbr_repo --stanza=kingbase
2023-02-02 14:51:56.201 P00 INFO: repo1: restore backup set 20230202-144709F, recovery will start at 2023-02-02 14:47:09
2023-02-02 14:51:56.201 P00 INFO: remap data directory to '/data/kingbase/v8c7/data'
ERROR: [040]: unable to restore to path '/data/kingbase/v8r6_C7/tbs/SYS_12_202209081' because it contains files
HINT: try using --delta if this is what you intended.
2023-02-02 14:51:56.202 P00 INFO: restore command end: aborted with exception [040]
---如上图所示,通过‘--kb1-path’指定了数据库恢复目录,但是自定义表空间数据仍然恢复到了原data目录下,出现恢复错误。
二、解决表空间恢复
1、创建表空间存储目录
[kingbase@node102 bin]$ mkdir -p /data/kingbase/v8c7/tbs
2、建立表空间映射并恢复数据库
[kingbase@node102 bin]$ /opt/Kingbase/ES/V8R6_C7/Server/bin/sys_rman --config=/home/kingbase/kbbr_repo/sys_rman.conf --stanza=kingbase --kb1-path=/data/kingbase/v8c7/data --archive-mode=off --tablespace-map=user01=/data/kingbase/v8c7/tbs restore
2023-02-02 14:54:10.679 P00 INFO: restore command begin 2.27: --archive-mode=off --band-width=0 --config=/home/kingbase/kbbr_repo/sys_rman.conf --exec-id=22983-6acf7342 --link-all --log-level-console=info --log-level-file=info --log-path=/opt/Kingbase/ES/V8R6_C7/Server/log --log-subprocess --non-archived-space=1024 --kb1-path=/data/kingbase/v8c7/data --process-max=4 --repo1-path=/home/kingbase/kbbr_repo --stanza=kingbase --tablespace-map=user01=/data/kingbase/v8c7/tbs
2023-02-02 14:54:10.691 P00 INFO: repo1: restore backup set 20230202-144709F, recovery will start at 2023-02-02 14:47:09
2023-02-02 14:54:10.691 P00 INFO: remap data directory to '/data/kingbase/v8c7/data'
2023-02-02 14:54:10.691 P00 INFO: map tablespace 'sys_tblspc/16559' to '/data/kingbase/v8c7/tbs'
......
2023-02-02 14:54:12.423 P00 INFO: Restore Process: FILE: 2207 / 2207 100% SZIE: 85799190 bytes / 85799190 bytes 81.8MB / 81.8MB 100%
2023-02-02 14:54:12.424 P00 INFO: write updated /data/kingbase/v8c7/data/kingbase.auto.conf
2023-02-02 14:54:12.428 P00 INFO: restore global/sys_control (performed last to ensure aborted restores cannot be started)
2023-02-02 14:54:12.429 P00 INFO: restore size = 81.8MB, file total = 2207
2023-02-02 14:54:12.429 P00 INFO: restore command end: completed successfully (1753ms)
---如上所示,数据库恢复完成。
---如上图所示,需要建立表空间映射,再执行恢复。
三、数据访问测试
1、查看自定义表空间存储信息
[kingbase@node102 data]$ cd ../tbs
[kingbase@node102 tbs]$ ls -lh
total 0
drwx------ 3 kingbase kingbase 18 Feb 2 14:54 SYS_12_202209081
2、启动数据库实例(在恢复后的data目录)
[kingbase@node102 tbs]$ cd /opt/Kingbase/ES/V8R6_C7/Server/bin
[kingbase@node102 bin]$ ./sys_ctl start -D /data/kingbase/v8c7/data/
.......
server started
3、连接数据库查看表空间信息
[kingbase@node102 bin]$ ./ksql -U system test
ksql (V8.0)
Type "help" for help.
test=# \db+
List of tablespaces
Name | Owner | Location | Access privileges | Options | Size | Description
-------------+--------+-------------------------+-------------------+---------+----------+-------------
sys_default | system | | | | 64 MB |
sys_global | system | | | | 658 kB |
sysaudit | system | | | | 24 kB |
user01 | system | /data/kingbase/v8c7/tbs | | | 18 bytes |
(4 rows)
test=# \c prod
You are now connected to database "prod" as user "system".
prod=# \d+ u_tbs1
Table "public.u_tbs1"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
--------+----------------------------+-----------+----------+---------+----------+--------------+-------------
id | integer | | | | plain | |
name | character varying(10 char) | | | | extended | |
Tablespace: "user01"
Access method: heap
---如上所示,表空间恢复到新的存储目录下,数据访问正常。
四、总结
对于KingbaseES V8R6物理备份的恢复,可以恢复到指定的目录,在生产环境下建立测试环境。但对于包含自定义表空间的备份的恢复,注意需要建立表空间映射后再执行恢复。
参考文档:
https://help.kingbase.com.cn/v8/highly/backup-restore/backup/sys-rman-5.html#id13