在当今这个数据驱动的时代,数据库的安全性和可靠性对于任何企业来说都是至关重要的。数据库备份和恢复是确保数据安全、防止数据丢失的关键环节。本篇文章我们来讨论如何使用ontape工具进行数据库的备份与恢复,包括增量备份、逻辑日志备份以及整个数据库系统的恢复等功能。无论您是数据库管理员还是IT专业人员,希望本文都能为您提供一些帮助。

ontape工具简介

ontape是数据库备份和恢复工具,它支持多种备份方式,包括全备份、增量备份以及逻辑日志备份。此外,它还支持从备份中恢复整个数据库系统或特定的dbspace。ontape工具的使用不仅限于备份,它还支持标准的I/O读写操作,如文件和管道等。在使用ontape之前,用户需对配置文件做出一定修改。

文件的修改配置

当使用ontape进行备份时,首先需要指定备份将要复制到的存储设备,并指定其块的大小以及最大容量等相关参数,如下图所示。其中,TAPEDEV制定了系统备份的存储设备,LTAPEDEV制定了逻辑日志备份的存储设备。

[root@node1 hdr]# onstat -c | grep TAPE
Your evaluation license will expire on 2024-10-18 00:00:00
# TAPEDEV      - The tape device path for backups. To use standard
# TAPEBLK      - The tape block size, in KB, for backups
# TAPESIZE     - The maximum amount of data to put on one backup
#                positive integral multiple of TAPEBLK.
TAPEDEV /dev/null
TAPEBLK 32
TAPESIZE 0
# LTAPEDEV     - The tape device path for logical logs
# LTAPEBLK     - The tape block size, in KB, for backing up logical
# LTAPESIZE    - The maximum amount of data to put on one logical
#                positive integral multiple of LTAPEBLK.
LTAPEDEV         /home/hdr/backups
LTAPEBLK 32
LTAPESIZE 0

其中各个参数含义见下表:

TAPEDEV

指定备份设备路径

TAPEBLK

指定备份的块大小(KB)

TAPESIZE

指定备份设备的大小(KB)

LTAPEDEV

指定逻辑日志备份设备路径

LTAPEBLK

指定逻辑日志备份的块大小(KB)

LTAPESIZE

指定逻辑日志备份设备的大小(KB)

可通过修改参数文件,或使用 onmode -wf 命令,设置GBase 8s的参数。一般情况下除设备路径外其他参数保持默认即可。

[root@node1 hdr]# cd $GBASEDBTDIR
[root@node1 hdr]# mkdir backups
[root@node1 hdr]# chown gbasedbt:gbasedbt backups
[root@node1 hdr]# chmod 777 backups
[root@node1 hdr]# onmode -wf "TAPEDEV=$GBASEDBTDIR/backups"
Your evaluation license will expire on 2024-10-18 00:00:00
Value of TAPEDEV has been changed to /home/hdr/backups.
[root@node1 hdr]# onmode -wf "LTAPEDEV=$GBASEDBTDIR/backups"
Your evaluation license will expire on 2024-10-18 00:00:00
Current value for LTAPEDEV (/home/hdr/backups) was saved in config file.

[root@node1 hdr]#  onstat -c | grep TAPEDEV
Your evaluation license will expire on 2024-10-18 00:00:00
# TAPEDEV      - The tape device path for backups. To use standard
TAPEDEV          /home/hdr/backups
# LTAPEDEV     - The tape device path for logical logs
LTAPEDEV         /home/hdr/backups

当TAPEDEV被设置为STDIO时,数据库备份时将向设备进行标准的I/O输出。

ontape备份命令详解

{ -a [-d]  |
  -c  |
  -l [-C | -X] [-d]  |
  -p [-e] [-rename {-f <filename> |
               -p <old path> -o <old offset> -n <new path> -o <new offset>...}]
     [-t tape_device_path [-v]] [-d]  |
  -S [-d]  |
  -r [-rename {-f <filename> |
               -p <old path> -o <old offset> -n <new path> -o <new offset>...}]
     [-D DBspace_list] [-t tape_device_path [-v]] [-d]  |
  -s [[-L archive_level][-F]] [-A database_list] [-B database_list]
     [-N database_list] [-U database_list] [-t tape_device_path [-v]] [-d]  }

-a 自动备份逻辑日志

-c 连续备份逻辑日志

-d 非交互模式,用于从目录备份或恢复

-l 逻辑恢复

-p 物理恢复(-e 用于外部物理恢复)

-r 完全恢复列出的 DBspaces/BLOBspaces

-s 归档整个系统

-A 将以下数据库设置为 ANSI 日志模式

-B 将以下数据库设置为缓冲日志模式

-C 连续逻辑日志恢复

-F 不更新归档信息的备份(对 HDR、克隆等有用)

-N 将以下数据库设置为无日志模式

-S 仅恢复(挽救)逻辑日志

-U 将以下数据库设置为非缓冲日志模式

-X 完成连续逻辑日志恢复并将服务器置于静默模式

-rename 在冷恢复期间重命名数据块

   with -rename options :

      -f <filename> 包含映射数据块路径名和偏移量列表的文件的路径名

      -p 数据块的旧路径名

      -o 数据块的旧偏移量

      -n 数据块的新路径名

      -o 数据块的新偏移量

-t 设置当前备份或恢复的磁带设备路径。对于备份到标准输出或从标准输入恢复,请使用 STDIO。

-v 将信息性消息写入标准错误输出

ontape备份操作

如下所示,当执行ontape -s进行备份时,服务器将首先提示用户选择备份的级别,接着提示指定备份设备。若服务器不能打开指定设备,则系统将会提示设备必须是在线的,需要重新指定。

[root@node1 hdr]# ontape -s
Your evaluation license will expire on 2024-10-18 00:00:00
Please enter the level of archive to be performed (0, 1, or 2) 0
100 percent done.
File created: /home/hdr/backups/node1_199_L0

Please label this tape as number 1 in the arc tape sequence.
This tape contains the following logical logs:

4

Program over.

修改数据库日志记录方式

可以使用ontape的-A、-B、-N和-U选项来修改数据库日志模式。

$> ontape -s -B   : 修改为缓冲日志模式

$> ontape -s -U  : 修改为无缓冲日志模式

$> ontape -s -N  : 修改为无日志模式$

$> ontape -s -A  : 修改为ansi logging模式,从这个模式无法切换到其他模式,因此一般不使用该项。

在切换数据库的日志方式时,服务器会对数据库加一个独占锁,因此此时用户不能使用这个数据库,否则会报告错误。

ontape备份逻辑日志

ontape -a选项表示备份所有未备份逻辑日志;-c选项表示在逻辑日志写满后自动进行备份,-c选项需要ontape始终处于运行状态,并且需要专门的设备来存储逻辑日志。

[root@node1 hdr]# ontape -a
Your evaluation license will expire on 2024-10-18 00:00:00

Performing automatic backup of logical logs.

File created: /home/hdr/backups/node1_199_Log0000000001
File created: /home/hdr/backups/node1_199_Log0000000002
File created: /home/hdr/backups/node1_199_Log0000000003
Do you want to back up the current logical log? (y/n) y
File created: /home/hdr/backups/node1_199_Log0000000004

Program over.

ontape热恢复操作

当服务器处于在线模式,并且需要恢复的不是rootdbs或逻辑日志所在的dbspace时,才可以使用热恢复。假设dbs2的磁盘发生了故障,且dbs2不包含日志,也不是rootdbs,则可以通过热恢复的方式来恢复dbs2。首先,通过物理回复,恢复dbs2的0、1、2级备份,接着按照故障发生前的逻辑日志进行数据库恢复。

创建dbs2:

[root@node1 tmp]# cd $GBASEDBTDIR/tmp
[root@node1 tmp]# touch dbs2
[root@node1 tmp]# chown gbasedbt:gbasedbt dbs2
[root@node1 tmp]# chmod 660 dbs2
[root@node1 tmp]# onspaces -c -d dbs2 -p $GBASEDBTDIR/tmp/dbs2 -o 0 -s 20000
Your evaluation license will expire on 2024-10-18 00:00:00
Verifying physical disk space, please wait ...
Space successfully added.

** WARNING **  A level 0 archive of Root DBSpace will need to be done.

进行0级备份:

[root@node1 tmp]# ontape -s
Your evaluation license will expire on 2024-10-18 00:00:00
Please enter the level of archive to be performed (0, 1, or 2) 0
100 percent done.
File created: /home/hdr/backups/node1_199_L0

Please label this tape as number 1 in the arc tape sequence.
This tape contains the following logical logs:

 5

Program over.

手动删除dbs2文件以模拟故障。此时,准备进行恢复,重新创建dbs2文件并更改权限:

[root@node1 tmp]# rm -rf dbs2
[root@node1 tmp]# touch dbs2
[root@node1 tmp]# chown gbasedbt:gbasedbt dbs2
[root@node1 tmp]# chmod 660 dbs2
[root@node1 tmp]# ll dbs2
-rw-rw---- 1 gbasedbt gbasedbt 0 Sep 15 13:13 dbs2

使用ontape对dbs2进行恢复。若需恢复的dbs处于在线状态,则ontape会首先将它转换到离线状态,之后会将备份文件的信息打印给用户,以便用户确认是否进行恢复。0、1、2级备份恢复结束后,ontape会继续进行逻辑备份,当整个恢复过程完全结束时,ontape会将dbs恢复为在线模式。

[root@node1 tmp]# ontape -r -D dbs2
Your evaluation license will expire on 2024-10-18 00:00:00
DBspace 'dbs2' is online; restoring 'dbs2' will bring all chunks
comprising the DBspace OFFLINE and will terminate all active
transactions and queries accessing the DBspace.

OK to continue?y
Restore will use level 0 archive file /home/hdr/backups/node1_199_L0. Press Return to continue ...


Archive Tape Information

Tape type:      Archive Backup Tape
Online version: GBase Database Server Version 12.10.FC4G1TL
Archive date:   Sun Sep 15 13:10:12 2024
User id:        root
Terminal id:    /dev/pts/1
Archive level:  0
Tape device:    /home/hdr/backups/
Tape blocksize (in k): 32
Tape size (in k): system defined for directory
Tape number in series: 1
Continue restore? (y/n)y

Spaces to restore:1 [dbs2]
Restore a level 1 archive (y/n) y
Ready for level 1 tape
Restore will use level 1 archive file /home/hdr/backups/node1_199_L1. Press Return to continue ...


Archive Tape Information

Tape type:      Archive Backup Tape
Online version: GBase Database Server Version 12.10.FC4G1TL
Archive date:   Sun Sep 15 13:10:20 2024
User id:        root
Terminal id:    /dev/pts/1
Archive level:  1
Tape device:    /home/hdr/backups/
Tape blocksize (in k): 32
Tape size (in k): system defined for directory
Tape number in series: 1
Restore a level 2 archive (y/n) n
Do you want to restore log tapes? (y/n)y

Roll forward should start with log number 5
Restore will use log backup file /home/hdr/backups/node1_199_Log0000000005. Press Return to continue ...

File /home/hdr/backups/node1_199_Log0000000005 not found, continuing ...
Recommendation: continue the warm restore until  log 5 is replayed.
The last log that has been restored is: 0. If you want to stop the
restore at this log, choose commit, otherwise, choose suspend.
If you have not performed a backup that includes the log 5,
perform a backup using the ontape -a command. Continue the suspended
log restore using the ontape -l command.
Do you want to suspend (y), commit (n) or abort (CTRL/C) logical restore?
y

Program over.

此时dbs2已恢复。恢复完成后,数据库将进入Quiescent模式,通过onmode -m切换至online模式。

ontape冷恢复操作

当服务器不能启动,或者需要恢复的是rootdbs或包含逻辑日志的dbs时,需要进行冷恢复。

在进行冷恢复时,首先需要关闭数据库,然后执行ontape -r命令。

[root@node1 tmp]# ontape -r
Your evaluation license will expire on 2024-10-18 00:00:00
Restore will use level 0 archive file /home/hdr/backups/node1_199_L0. Press Return to continue ...


Archive Tape Information

Tape type:      Archive Backup Tape
Online version: GBase Database Server Version 12.10.FC4G1TL
Archive date:   Sun Sep 15 13:10:12 2024
User id:        root
Terminal id:    /dev/pts/1
Archive level:  0
Tape device:    /home/hdr/backups/
Tape blocksize (in k): 32
Tape size (in k): system defined for directory
Tape number in series: 1

Spaces to restore:1 [rootdbs]
2 [dbs2]

Archive Information

GBase Database Server Copyright 2001, 2021  General Data Corporation
Initialization Time       09/15/2024 09:56:33
System Page Size          2048
Version                   32
Index Page Logging        OFF
Archive CheckPoint Time   09/15/2024 13:07:59

Dbspaces
number   flags     fchunk   nchunks  flags     owner                            name
1        60001    1         1        N  BA     gbasedbt                          rootdbs                                                                      
2        60001    2         1        N  BA     gbasedbt                          dbs2                                                                        


Chunks
chk/dbs offset   size     free      bpages   flags pathname
1   1    0        10240000 5059090           PO-B- /home/hdr/gbase001_dbs/rootdbs
2   2    0        10000    9947               PO-B- /home/hdr/tmp/dbs2

Continue restore? (y/n)y
Do you want to back up the logs? (y/n)y
File created: /home/hdr/backups/node1_199_Log0000000005
Log salvage is complete, continuing restore of archive.
Your evaluation license will expire on 2024-10-18 00:00:00
Restore a level 1 archive (y/n) n
Do you want to restore log tapes? (y/n)y

Roll forward should start with log number 5
Restore will use log backup file /home/hdr/backups/node1_199_Log0000000005. Press Return to continue ...

Rollforward log file /home/hdr/backups/node1_199_Log0000000005 ...

Program over.

冷恢复过程基本与热恢复一致。在冷恢复执行完ontape -r之后将会恢复包含rootdbs和逻辑日志的dbs在内的dbspace。

使用STDIO标准输入和管道输出

ontape还支持标准输入和管道输出,可以通过配置TAPEDEV参数为STDIO,或在命令行中执行-t STDIO来指定要使用的管道。由于读或写会被直接转发到UNIX管道,所以可使用任意UNIX命令来进行数据处理。

使用STDID后,将不再有交互式提醒。所以备份时需要在命令行中写明备份级别,恢复时要将每个恢复级别的文件列出。下面是三个使用STDIO的例子。

先后进行0级和1级恢复:

[root@node1 tmp]# cat $GBASEDBTDIR/backups/backupFile \
> $GBASEDBTDIR/backups/backupFile0 | ontape -r

备份:

[root@node1 backups]# ontape -s -L 0 | compress > node1_ontape_0
Your evaluation license will expire on 2024-10-18 00:00:00
[root@node1 backups]# chown gbasedbt:gbasedbt node1_ontape_0
[root@node1 backups]# chmod 660 node1_ontape_0           
[root@node1 backups]# ll node1_ontape_0
-rw-rw---- 1 gbasedbt gbasedbt 179 Sep 15 13:43 node1_ontape_0

恢复:

[root@node1 backups]# zcat node1_ontape_0 | ontape -r
Your evaluation license will expire on 2024-10-18 00:00:00
Restore will use level 0 archive file /home/hdr/backups/node1_199_L0. Press Return to continue ...

Archive Tape Information

Tape type:      Archive Backup Tape
Online version: GBase Database Server Version 12.10.FC4G1TL
Archive date:   Sun Sep 15 13:43:04 2024
User id:        root
Terminal id:    /dev/pts/1
Archive level:  0
Tape device:    /home/hdr/backups/
Tape blocksize (in k): 32
Tape size (in k): system defined for directory
Tape number in series: 1
Continue restore? (y/n)

 

数据库的备份与恢复是确保数据安全的重要措施。通过本文的详细介绍,您应该对如何使用ontape工具进行数据库的备份和恢复有了全面的了解。无论您是需要进行日常的备份操作,还是面对数据丢失的紧急恢复情况,ontape工具都能为您提供支持。确保您的数据安全,从掌握正确的备份和恢复技术开始。