KingbaseV8R6单实例定时全量备份步骤

此场景为单机数据库节点内部备份,方便部署和操作,但备份REPO与数据库实例处于同一个物理主机,冗余度较低。
KingbaseV8R6单实例定时全量备份步骤_第1张图片

前期准备

配置ksql免密登录(必须)

在Kingbase数据库运行维护中,经常用到ksql工具登录数据库,本地免密登录可以让维护数据库时,摆脱每次登库输入用户名密码;下面介绍KingbaseES V8数据库2种配置免密登录的方法。
ksql登录时,会去数据库安装用户家目录下的密码文件.encpwd中读取账户,从而登入数据库;密码文件为普通文本文件,可以vi创建,也可以使用kingbase提供的工具sys_encpwd工具来生成密码文件。
本案例采用system用户做演示,若需其他用户视情况更改。

.encpwd内容解析

每一行代表一条访问规则记录,可以配置多条记录;每条记录包含5个字段,字段与字段之间用冒号“:”分隔,5个字段分别为:主机地址、端口号、库名、用户名、密码(加密格式);
.encpwd若不存在会创建,若已存在,会追加记录。
例:

127.0.0.1:54321:test:u1:MTIzNDU2

sys_encpwd配置工具使用方法

该工具包含5个参数,5个参数均需要输入才能配置成功
参数说明:

-H, --hostname=主机地址,允许单主机地址匹配和全匹配,全匹配时使用*指定
-P, --portnum=端口号,仅允许单端口匹配
-D, --database=数据库名,允许单数据库匹配和全匹配,全匹配时使用*指定
-U, --user=用户名,仅允许单用户匹配
-W, --password=用户对应的密码,仅允许单用户匹配

注意:当前四个参数输入与原文件中的某一条记录完全相同时,会修改该条记录相应的密码,否则生成一条新的记录。

示例1:将新创建用户登录信息加入密码文件

--创建用fuwa并指定密码,用ksql测试登录需要输入密码
[kingbase@node1 ~]$ ksql -Usystem -W test
口令:
ksql (V8.0)
输入 "help" 来获取帮助信息.
test=# create user fuwa with password 'fuwa';
CREATE ROLE
test=# \q
[kingbase@node1 ~]$ ksql -Ufuwa test
ksql (V8.0)
输入 "help" 来获取帮助信息.
test=> 

--使用sys_encpwd工具配置密码,指定fuwa用户可以从任意主机、任意端口,登录任意数据库
[kingbase@node1 ~]$ sys_encpwd -H \* -P \* -D \* -U fuwa -W fuwa
[kingbase@node1 ~]$ cat .encpwd 
*:*:*:fuwa:ZnV3YQ==

参数说明:-H 指定任意主机,-P指定任意端口,-D指定任意库*,“*”为特殊字符,需要加\转义**

--测试用户fuwa免密登录
[kingbase@node1 ~]$ ksql -Ufuwa test
ksql (V8.0)
输入 "help" 来获取帮助信息.

test=> 

示例2:对system用户登录信息加入密码文件

[kingbase@node1 ~]$ sys_encpwd -H \* -P \* -D \* -U system -W kingbase
[kingbase@node1 ~]$ cat .encpwd 
*:*:*:fuwa:ZnV3YQ==
*:*:*:system:a2luZ2Jhc2U=

数据库开启归档模式(必须)

--开启归档模式
[kingbase@node1 ~]$ vi /data/kingbase.conf
#archive_mode = off             # enables archiving; off, on, or always
#archive_command = ''
改为
archive_mode = on               # enables archiving; off, on, or always
archive_command = ''

--重启库
[kingbase@node1 ~]$ sys_ctl restart -D /data/

补充:archive_command 没有被注释,内容将被脚本自动修改

规划备份目录

[root@node1 bin]# mkdir -p /kbrmanbak
[root@node1 bin]# chown -R kingbase:kingbase /kbrmanbak/
[root@node1 bin]# chmod -R 755 /kbrmanbak/

sys_rman的初始化配置

准备初始配置文件

sys_backup.conf,初始化配置文件,供操作人员修改的配置文件;
sys_rman.conf,运行时配置文件,由初始化动作自动生成,不推荐人为后续的修改。
工具按照如下顺序寻找初始化配置文件

cd /KingbaseES/V8/Server
# bin/sys_backup.conf
# share/sys_backup.conf

生成sys_backup.conf并更改

V008R006C005B0023之后的版本,支持securecmdd或sshd;之前的版本支持sshd。

--查找sys_backup.conf模板位置
[kingbase@node1 bin]$ find / -name sys_backup.conf
/KingbaseES/V8/Server/share/sys_backup.conf
/KingbaseES/V8/ClientTools/share/sys_backup.conf
/KingbaseES/V8/ManagerTools/share/sys_backup.conf

--生成正式sys_backup.conf文件
[kingbase@node1 bin]$ cp /KingbaseES/V8/Server/share/sys_backup.conf /KingbaseES/V8/Server/bin/sys_backup.conf

--正式sys_backup.conf文件  追加以下内容:
# 网络限速,单位固定为 MB/s, 默认为0,代表不限速,配置文件仅接受纯数字
# band witdh limit, fixed in MB/s, default 0 means no limit
_band_width=0
# 使用通讯协议,默认使用securecmdd,可选使用ssh,视生产环境版本更改参数值
# V008R006C005B0023之后的版本,支持securecmdd或sshd;之前的版本支持sshd。
# on means securecmdd, off means normal ssh
_use_scmd=on
# 是否快速启动备份,立即生成checkpoint,默认y
# whether to sys_start_backup() fastly, y or n
_start_fast=y
#可在指定备份时、还原时并行处理可使用的并发进程数量
--process-max=4 
# 是否在备份时使用压缩存储,默认为none不压缩 若指定压缩级别,可选范围 [ 0 - 9 ]
# whether to gz the target file, none or gz
_compress_type=gz
--compress-level=3
# 在init过程中,检查未归档的WAL的容量,
# 如果超过设置值,报错并退出init过程
# 单位固定为MB,可选 128 ~ 1024
# if the non archived WAL files , more than this setting
# show ERROR and break the backup-init, unit be fixed in MB, 128 ~ 1024
_non_archived_space=1024
# 不启用归档统计功能
_archive_statistics=n

--更改正式sys_backup.conf文件,更改后内容如下:
[kingbase@node1 bin]$ cat /KingbaseES/V8/Server/bin/sys_backup.conf
#file: sys_backup.conf
#dest dir: /kingbase/bin/sys_backup.conf
#dest dir: /kingbase/share/sys_backup.conf

#单机 single 集群 cluster 集群各节点独立备份 single-pro
# target db style enum:  single/cluster 
_target_db_style="single"
#一个数据库节点的IP,支持主机名、IPv4、IPv6地址
# one kingbase node IP  
# just provide one IP, script will use 'repmgr cluster show' get other node IP
_one_db_ip="192.168.40.110"  
#REPO备份节点的IP 本配置项支持主机名、IPv4、IPv6地址
# local repo IP, inner-REPO, must be same as one_db_ip, means repo located in one db node
# outer repo IP, outer-REPO, means repo located in outer node
_repo_ip="192.168.40.110"
# 备份服务器的标签,仅在备份范畴内使用
# label of this cluster
_stanza_name="kingbase"  
# 操作系统的用户名
# OS user name of database 
_os_user_name="kingbase" 
# 实际保存备份集的目录
# !!!! dir to store the backup files
# should be accessable for the OS user
_repo_path="/kbrmanbak/kbbr_repo"
# 保存全量备份的数目,超过此数目的全量备份将被自动移除
# count of keep, over the count FULL-backup will be remove
_repo_retention_full_count=5 
# 自动执行全量备份的间隔天数
# count of days, interval to do FULL-backup
_crond_full_days=1  
# 自动执行差异备份的间隔天数,0表示不执行
# count of days, interval to do DIFF-backup
_crond_diff_days=0 
# 自动执行增量备份的间隔天数,0表示不执行
# count of days, interval to do INCR-backup
_crond_incr_days=0
# 自动执行全量备份的时间点,2表示凌晨2点
# HOUR to do the FULL-backup
_crond_full_hour=2 
# 自动执行差异备份的时间点,3表示凌晨3点
# HOUR to do the DIFF-backup
_crond_diff_hour=3 
# 自动执行增量备份的时间点,4表示凌晨4点
# HOUR to do the INCR-backup
_crond_incr_hour=4 
# 操作系统常见命令的全路径文件名
# OS cmd define
_os_ip_cmd="/sbin/ip"
_os_rm_cmd="/bin/rm"
_os_sed_cmd="/bin/sed"
_os_grep_cmd="/bin/grep"
# 以下四个参数,仅用于单机模式
# !!! these follow 4 parameter ONLY for single style
# 单机数据库节点的数据目录
# data dir of single 
_single_data_dir="/data"
# 单机数据库节点的二进制目录
# bin dir of single
_single_bin_dir="/KingbaseES/V8/Server/bin"
# 单机数据库节点的数据库登录用户名
# database user of single
_single_db_user="system"
# 单机数据库节点的端口
# database port of single
_single_db_port="54321"
# 网络限速,单位固定为 MB/s, 默认为0,代表不限速,配置文件仅接受纯数字
# band witdh limit, fixed in MB/s, default 0 means no limit
_band_width=0
# 使用通讯协议,默认使用securecmdd,可选使用ssh,视生产环境版本更改参数值
# V008R006C005B0023之后的版本,支持securecmdd或sshd;之前的版本支持sshd。
# on means securecmdd, off means normal ssh
_use_scmd=on
# 是否快速启动备份,立即生成checkpoint,默认y
# whether to sys_start_backup() fastly, y or n
_start_fast=y
#可在指定备份时、还原时并行处理可使用的并发进程数量
--process-max=4 
# 是否在备份时使用压缩存储,默认为none不压缩 若指定压缩级别,可选范围 [ 0 - 9 ]
# whether to gz the target file, none or gz
_compress_type=gz
--compress-level=3
# 在init过程中,检查未归档的WAL的容量,
# 如果超过设置值,报错并退出init过程
# 单位固定为MB,可选 128 ~ 1024
# if the non archived WAL files , more than this setting
# show ERROR and break the backup-init, unit be fixed in MB, 128 ~ 1024
_non_archived_space=1024
# 不启用归档统计功能
_archive_statistics=n

注意事项:

[kingbase@node1 bin]$ vi /KingbaseES/V8/Server/bin/sys_backup.conf
....
# 使用通讯协议,默认使用securecmdd,可选使用ssh,视生产环境版本更改参数值
# V008R006C005B0023之后的版本,支持securecmdd或sshd;之前的版本支持sshd。
# on means securecmdd, off means normal ssh
_use_scmd=off
...

--查看版本方法
[kingbase@node1 bin]$ ksql -V
ksql (Kingbase) V008R006C005B0023

sys_rman初始化

初始化步骤

kingbase@node1 kbrmanbak]$ sys_backup.sh init
The authenticity of host '192.168.40.110 (192.168.40.110)' can't be established.
ECDSA key fingerprint is b8:54:e0:74:c5:e2:33:46:0d:7d:39:91:e3:b5:5a:6d.
Are you sure you want to continue connecting (yes/no)? yes
[email protected]'s password: 
# generate single sys_rman.conf...DONE
# update single archive_command with sys_rman.archive-push...DONE
# create stanza and check...(maybe 60+ seconds)
2023-12-22 13:43:32.069 CST [12140] 警告:  max_connections should be less than or equal than 10 (restricted by license)
2023-12-22 13:43:32.069 CST [12140] 提示:  the value of max_connect is set 10
# create stanza and check...DONE
# initial first full backup...(maybe several minutes)
# initial first full backup...DONE
# Initial sys_rman OK.
'sys_backup.sh start' should be executed when need back-rest feature.

问题处理

--问题描述:
sys_rman初始化报错
[kingbase@node1 bin]$ sys_backup.sh init
The authenticity of host '192.168.40.110 (192.168.40.110)' can't be established.
ECDSA key fingerprint is b8:54:e0:74:c5:e2:33:46:0d:7d:39:91:e3:b5:5a:6d.
Are you sure you want to continue connecting (yes/no)? yes
Please input password ...
[email protected]'s password: 

local <-> [email protected] ssh pwd-less OK.
# generate single sys_rman.conf...DONE
# update single archive_command with sys_rman.archive-push...DONE
# create stanza and check...(maybe 60+ seconds)
ERROR: check stanza failed, check log file /KingbaseES/V8/Server/log/sys_rman_check.log

--分析过程
[kingbase@node1 bin]$ tail -300f  /KingbaseES/V8/Server/log/sys_rman_check.log
2023-12-22 13:38:56.493 P00   INFO: check command begin 2.27: --config=/kbrmanbak/kbbr_repo/sys_rman.conf --log-level-console=info --log-level-file=info --log-path=/KingbaseES/V8/Server/log --log-subprocess --kb1-path=/data --kb1-port=54321 --kb1-user=system --repo1-path=/kbrmanbak/kbbr_repo --stanza=kingbase
ERROR: [068]: archive_command '[null]' must contain sys_rman
2023-12-22 13:38:57.009 P00   INFO: check command end: aborted with exception [068]

--解决办法
去掉archive_command前的#标识,去掉后如下
archive_command='/KingbaseES/V8/Server/bin/sys_rman --config /kbrmanbak/kbbr_repo/sys_rman.conf --stanza=kingbase archive-push %p'

重启库
sys_backup.sh init

删除目录
cd /kbrmanbak/
rm -rf kbbr_repo/

再次sys_rman初始化成功
kingbase@node1 kbrmanbak]$ sys_backup.sh init
The authenticity of host '192.168.40.110 (192.168.40.110)' can't be established.
ECDSA key fingerprint is b8:54:e0:74:c5:e2:33:46:0d:7d:39:91:e3:b5:5a:6d.
Are you sure you want to continue connecting (yes/no)? yes
[email protected]'s password: 
# generate single sys_rman.conf...DONE
# update single archive_command with sys_rman.archive-push...DONE
# create stanza and check...(maybe 60+ seconds)
2023-12-22 13:43:32.069 CST [12140] 警告:  max_connections should be less than or equal than 10 (restricted by license)
2023-12-22 13:43:32.069 CST [12140] 提示:  the value of max_connect is set 10
# create stanza and check...DONE
# initial first full backup...(maybe several minutes)
# initial first full backup...DONE
# Initial sys_rman OK.
'sys_backup.sh start' should be executed when need back-rest feature.

备份任务的启停

sys_backup.sh start

脚本使用crontab命令增加定时任务,使用sys_rman进行定时备份;可以通过crontab -l 查看定时任务。
全量备份、差异备份、增量备份,均为可选。
备份类型、备份周期和备份时间由配置文件sys_backup.conf指定。

[kingbase@node1 ~]$ sys_backup.sh start
Enable some sys_rman in crontab-daemon
Set full-backup in 1 days
0 2 */1 * * kingbase /KingbaseES/V8/Server/bin/sys_rman --config=/kbrmanbak/kbbr_repo/sys_rman.conf --stanza=kingbase --archive-copy --type=full backup >> /KingbaseES/V8/Server/log/sys_rman_backup_full.log 2>&1

[kingbase@node1 ~]$ crontab -l
0 2 */1 * * kingbase /KingbaseES/V8/Server/bin/sys_rman --config=/kbrmanbak/kbbr_repo/sys_rman.conf --stanza=kingbase --archive-copy --type=full backup >> /KingbaseES/V8/Server/log/sys_rman_backup_full.log 2>&1

说明:
需要去掉以下中的kingbase,不然备份提示/bin/sh: kingbase: 未找到命令
[kingbase@node1 ~]$ crontab -l
0 2 */1 * *  kingbase /KingbaseES/V8/Server/bin/sys_rman --config=/kbrmanbak/kbbr_repo/sys_rman.conf --stanza=kingbase --archive-copy --type=full backup >> /KingbaseES/V8/Server/log/sys_rman_backup_full.log 2>&1

sys_backup.sh stop

从系统CRONTAB中移除所有sys_rman相关的备份条目。

[kingbase@node1 ~]$ sys_backup.sh stop
Disable all sys_rman in crontab-daemon

[kingbase@node1 kingbase]$ crontab -l
no crontab for kingbase

sys_backup.sh pause

暂时停止定时备份动作。

sys_backup.sh pause
Puase the sys_rman...DONE

sys_backup.sh unpause

恢复被暂停的定时备份动作。

sys_backup.sh unpause
Un-Puase the sys_rman...DONE

sys_backup.conf信息变化处理

如果sys_backup.conf中关于数据库的信息(包含IP、数据库名、端口等)有所修改,需要重新初始化备份,执行 sys_backup.sh init重新初始化备份功能。
如果sys_backup.conf中关于备份周期和时间点的信息有所修改,重新配置即可,执行sys_backup.sh start重新配置自动备份周期。

以下参数修改后需要重新初始化生效

# DB节点的data目录
kb*-path=/home/kingbase/kingbase/data
# DB节点的监听端口
kb*-port=54321
# DB节点的数据库用户
kb*-user=esrep
# DB节点的IP地址
kb*-host=10.11.12.13
# DB节点的OS用户名
kb*-host-user=kingbase
# REPO节点的实际备份目录
repo1-path=/home/kingbase/kbbr_repo

修改完成后,执行以下命令完成重新初始化

/home/kingbase/cluster/project/cluster/kingbase/bin/sys_rman --config=/home/kingbase/kbbr_repo/sys_rman.conf --stanza=kingbase stanza-create
/home/kingbase/cluster/project/cluster/kingbase/bin/sys_rman --config=/home/kingbase/kbbr_repo/sys_rman.conf --stanza=kingbase check
/home/kingbase/cluster/project/cluster/kingbase/bin/sys_rman --config=/home/kingbase/kbbr_repo/sys_rman.conf --stanza=kingbase --type=full backup

以下参数修改后需要一次全量备份生效

# 是否压缩备份的数据,默认none不压缩,可选 none / gz
# 压缩将节约空间、消耗时间;不压缩将节约时间、消耗空间。
compress-type=none
# 压缩级别, 默认3, 1~9,9代表最大压缩,同时也最耗时
compress-level=3

修改完成后,执行以下命令完成一次全量备份:

/home/kingbase/cluster/project/cluster/kingbase/bin/sys_rman --config=/home/kingbase/kbbr_repo/sys_rman.conf --stanza=kingbase --type=full backup

以下参数修改后直接生效

# REPO节点保留至多的全量备份
repo*-retention-full=5
# REPO节点的sys_rman工具日志保存路径
log-path=/home/kingbase/kingbase/log
# 写入到日志文件的日志级别, info debug trace
log-level-file=info
# 执行命令时屏幕的日志级别, info debug trace
log-level-console=info
# 是否记录子进程的日志, y / n
log-subprocess=y
# 多进程的至多数目
process-max=4
# 是否快速生成checkpoint,在开启实际备份之前
start-fast=y
# 是否保留目录下的软链接
link-all=y
# 未归档的WAL文件容量的警告阈值
non-archived-space=1024
# 归档单个WAL的超时时间
archive-timeout=600
# 限速,单位固定为MB/s
band-width=0
# 备份过程中遇到校验和等问题时会执行的自定义命令,如发送告警邮件等
alert-command=( cd /opt/Kingbase/ES/Server/bin; ./sys_mail -s "%t" -b "%c" & )

你可能感兴趣的:(Kingbase)