此場景為單機數據庫節點內部備份,方便部署和操作,但備份REPO與數據庫實例處于同一個物理主機,冗余度較低。
前期準備
配置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: <cluster_dir>/kingbase/bin/sys_backup.conf
#dest dir: <cluster_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
root@192.168.40.110'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 ...
root@192.168.40.110's password: local <-> root@192.168.40.110 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
root@192.168.40.110'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" & )