案例说明:
KingbaseES V8R3集群自带了timingbackup.sh的脚本,可以通过一个脚本执行逻辑和物理备份,逻辑备份采用sys_dump,物理备份适用sys_basebackup,本案例详细记录了脚本的使用。
适用版本: KingbaseES V8R3
1、查看数据库归档配置
[kingbase@node101 bin]$ cat ../data/kingbase.conf |grep -i archive_
archive_command = 'test ! -f /home/kingbase/cluster/HAR3/archivedir/%f && cp %p /home/kingbase/cluster/HAR3/archivedir/%f'
archive_mode=on
archive_dest='/home/kingbase/cluster/HAR3/archivedir
2、配置备份配置文件1)脚本文件存储路径
[kingbase@node101 r3_cluster_backup]$ pwd
/home/kingbase/cluster/HAR3/db/kb_scripts/kb_backup/r3_cluster_backup
[root@node101 r3_cluster_backup]# ls -lh
total 16K
-rwxr-xr-x 1 kingbase kingbase 1.1K Oct 25 11:31 backup.conf
-rwxr-xr-x 1 kingbase kingbase 9.9K Oct 25 14:08 timingbackup.sh
2)配置backup.conf文件
Tips:备份需要在主库上执行,并且脚本需要root用户执行。
[kingbase@node101 r3_cluster_backup]$ cat backup.conf |grep -v ^#|grep -v ^$
kb_path=/home/kingbase/cluster/HAR3/db/bin/
cron_user=kingbase
cron_day=1
cron_hour=2
db_host=127.0.0.1
db_port=54321
db_user=SYSTEM
pass_encryption=0
db_pass=123456
backup_path=/data/kingbase/r3_bk
archive_path=/home/kingbase/cluster/HAR3/archivedir
db_list='prod' # 逻辑备份需要指定备份的数据库
keep_day=7
physical_keep_backup_file_num=3
logical_keep_backup_file_num=7
backup_mode=both # both执行逻辑和物理备份,logical逻辑备份,physical物理备份。
3、执行备份脚本
Tips:备份根据backup.conf的配置,执行相应的备份,并且在备份结束时,自动清理冗余的历史备份和归档日志。
[root@node101 r3_cluster_backup]# sh timingbackup.sh backup
Tue Oct 25 11:49:31 CST 2022 backup begin...
Tue Oct 25 11:49:31 CST 2022 logical backup and tar logical_backup_20221025114931.tar.gz done.
Tue Oct 25 11:49:31 CST 2022 remove backup file by retention strategy done.
Tue Oct 25 11:49:35 CST 2022 physical backup and tar physical_backup_20221025114931.tar.gz done.
Tue Oct 25 11:49:35 CST 2022 remove backup file by retention strategy done.
Tue Oct 25 11:49:35 CST 2022 remove archives done.
Tue Oct 25 11:49:35 CST 2022 backup done.
查看备份结果:
4、归档日志的清理
Tips:在执行sys_basebackup后,在wal归档日志目录下,创建backup的wal日志文件,然后调用sys_archiveclean工具清理backup以前的wal日志。
1、查看归档日志信息
[kingbase@node101 sys_xlog]$ ls -lh
total 273M
-rw------- 1 kingbase kingbase 16M Oct 25 11:48 00000001000000000000000D
-rw------- 1 kingbase kingbase 16M Oct 25 11:48 00000001000000000000000E
-rw------- 1 kingbase kingbase 16M Oct 25 11:49 00000001000000000000000F
-rw------- 1 kingbase kingbase 16M Oct 25 11:49 000000010000000000000010
-rw------- 1 kingbase kingbase 306 Oct 25 11:49 000000010000000000000010.00000028.backup
-rw------- 1 kingbase kingbase 16M Oct 25 11:49 000000010000000000000011
2、查看备份日志backup.log
.......
Tue Oct 25 11:49:31 CST 2022 /home/kingbase/cluster/HAR3/db/bin//sys_dump -h 127.0.0.1 -p 54321 -U SYSTEM -W ****** -Fc -f /data/kingbase/r3_bk/logical/PROD_20221025114931.dmp PROD
Tue Oct 25 11:49:31 CST 2022 rm -fr --interactive=never *_20221025114931.dmp
Tue Oct 25 11:49:31 CST 2022 /home/kingbase/cluster/HAR3/db/bin//sys_basebackup -h127.0.0.1 -p 54321 -U SYSTEM -W ****** -F p -X stream -D /data/kingbase/r3_bk/physical/20221025114931_data
sys_archivecleanup: keep WAL file "/home/kingbase/cluster/HAR3/archivedir/000000010000000000000010" and later
sys_archivecleanup: removing file "/home/kingbase/cluster/HAR3/archivedir/00000001000000000000000E"
sys_archivecleanup: removing file "/home/kingbase/cluster/HAR3/archivedir/00000001000000000000000F"
如下图所示,wal日志清理:
5、总结
1、此脚本对于逻辑备份,需要指定数据库备份,对于数据库发生变化的实例,要及时更新backup.conf配置。
2、物理备份采用sys_basebackup,不支持增量备份,对于数据量大的环境,要谨慎使用,增量备份需要使用sys_rman执行。