人大金仓数据库逻辑备份

说明:人大金仓数据库安装之后的下面4个文件内容是错误的

1.备份配置相关文件

1.1  backup8.sh


#!/bin/bash
####################################################################################################################
###
###Descipt: this script is used for kingbase database backup,before you run it,you should set the variables such as
###         kdb_home,kdbback_dest,kdb_user,kdb_pass,kdb_host,kdb_port,kdb_list,keep_time and so on.
###
####################################################################################################################
####################### variable define ##########################

#load the backup confuration
#TODO:this cmd have error
#source backup8.conf 
#source /root/kb_scripts/kb_scripts/kb_backup/logical/backup8.conf

echo "kingbase database logical backup begin : $(date '+%Y-%m-%d %H:%M:%S')"

conf_file=$(dirname $(readlink -f "$0"))"/backup8.conf"
echo "conf file is: $conf_file"
source $conf_file


export PGPASSWORD=${kdb_pass}
# modify the backup8.conf style: use "xxxx" replace xxxx, just benifit to source the conf file
#kdb_home=`cat backup8.conf |grep kdb_home|awk -F'=' '{print $2}'|sed s/[[:space:]]//g`
#kdbback_dest=`cat backup8.conf |grep kdbback_dest|awk -F'=' '{print $2}'|sed s/[[:space:]]//g`
#kdb_user=`cat backup8.conf |grep kdb_user|awk -F'=' '{print $2}'|sed s/[[:space:]]//g`
#kdb_pass=`cat backup8.conf |grep kdb_pass|awk -F'=' '{print $2}'|sed s/[[:space:]]//g`
#kdb_port=`cat backup8.conf |grep kdb_port|awk -F'=' '{print $2}'|sed s/[[:space:]]//g`
#kdb_host=`cat backup8.conf |grep kdb_host|awk -F'=' '{print $2}'|sed s/[[:space:]]//g`
#kdb_list=`cat backup8.conf |grep kdb_list|awk -F'=' '{print $2}'|sed s/[[:space:]]//g`
#keep_time=`cat backup8.conf |grep keep_time|awk -F'=' '{print $2}'|sed s/[[:space:]]//g`

date=$(date '+%Y%m%d%H')
kdbback_final="${kdbback_dest}/kdbback_final"
LD_LIBRARY_PATH="${kdb_home}/lib"

####################### kingbase backup dest test ##################

[ -d ${kdbback_dest} ] || mkdir -p ${kdbback_dest}
[ -d ${kdbback_final} ] || mkdir -p ${kdbback_final}

####################### kingbase backup start  #######################

cd ${kdbback_dest}
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH
for db in `echo $kdb_list | sed 's/,/ /g'`; do
	[ -d ${db} ] || mkdir -p ${db}
        cd ${db}
	##### kingbase server check as follows

export PGPASSWORD=${kdb_pass}
	${kdb_home}/bin/ksql -p ${kdb_port} -U ${kdb_user}  -c "select now();" TEMPLATE1 > /dev/null 2>&1
        if [ $? -ne 0 ] ;then
	    echo "${date} sorry, please run the script in a kingbase server" >> backup_${db}_${date}.log 
	    mv backup_${db}_${date}.log ${kdbback_final}
	    exit 1
        else
	    echo "${date} kingbase server is ok,kingbase backup ${db} is beginning ..." >> backup_${db}_${date}.log
        fi
	##### end
        ##### kingbase backup files process as follows
export PGPASSWORD=${kdb_pass}
	${kdb_home}/bin/sys_dump -p ${kdb_port} -U ${kdb_user}  -Fc -f ${db}_${date}.dmp ${db} >> backup_${db}_${date}.log 2>&1
        if [ $? -eq 0 ] ;then
            tar zcvf ${db}_${date}.tar.gz ${db}_${date}.dmp*
            if [ $? -eq 0 ] ;then
               rm -f ${db}_${date}.dmp*
            else
               mv ${db}_${date}.dmp* ${kdbback_final} 
            fi
            find . -mtime +${keep_time} -name ${db}'_*' | xargs -I {} rm {}
        else
	    rm -f ${db}_${date}.dmp*
        fi
        ###### end
        ###### kingbase backup log files process as follows
	tar zcvf backup_log_${db}_${date}.tar.gz backup_${db}_${date}.log
        if [ $? -eq 0 ] ;then
	       rm -f backup_${db}_${date}.log
	else
	       mv backup_${db}_${date}.log ${kdbback_final}
        fi
        find . -mtime +${keep_time} -name backup_log_${db}'_*' | xargs -I {} rm {}
        ###### end
       cd ${kdbback_dest}
done

echo "kingbase database logical backup end : $(date '+%Y-%m-%d %H:%M:%S')"
echo ""

exit 0

1.2   backup8.conf 

kdb_home="/home/kingbase/cluster/projectName/sc/kingbase"
kdbback_dest="/data2/logicalbackup"
kdb_user="root"
kdb_pass="123456"
export PGPASSWORD=${kdb_pass}
kdb_port="54321"
kdb_host="127.0.0.1"  #R6逻辑备份加了kdb_list要求输入密码,R6逻辑备份不能加-W参数否则报错
kdb_list="dbName" 
#动态获取数据库列表,1、R6没有SAMPLES和TEMPLATE2  2、R6有security库R3没有。 3、R3库名为大小R6库名为小写
#kdb_list=`${kdb_home}/bin/ksql -h ${kdb_host} -p ${kdb_port} -U ${kdb_user}  -c "SELECT datname FROM sys_database where datname not in ('template1','template0','security');" template1 |head -n -2|tail -n +3`
keep_time="7"

1.3  backup8_check.sh


#!/bin/bash

####################################################################################################################                                                                                                                                                 
###                                                                                                                                                                                                                                                                  
### Descipt: this script help us to check our logical dump file 
### Author : HM
### Create : 2020-04-28
###
### Usage  :
###        1.modify the backup8.conf file: input correct database conf, like this:
###        kdb_home=/opt/Kingbase/ES/V8/Server
###        kdbback_dest = /data/kingbase/dump/
###        kdb_user=SYSTEM
###        kdb_pass=system
###        kdb_port=54323
###        kdb_host=127.0.0.1
###        kdb_list = TEST,SAMPLES
###        keep_time = 7
###
###        2.execute check:
##         ./backup8_check.sh
##         ./backup8_check.sh latest
###
####################################################################################################################


#load the backup confuration
conf_file=$(dirname $(readlink -f "$0"))"/backup8.conf"
echo "conf file is: $conf_file"
source $conf_file

####################### variable define ##########################
#kdb_home=`cat backup8.conf |grep kdb_home|awk -F'=' '{print $2}'|sed s/[[:space:]]//g`
#kdbback_dest=`cat backup8.conf |grep kdbback_dest|awk -F'=' '{print $2}'|sed s/[[:space:]]//g`
#kdb_user=`cat backup8.conf |grep kdb_user|awk -F'=' '{print $2}'|sed s/[[:space:]]//g`
#kdb_pass=`cat backup8.conf |grep kdb_pass|awk -F'=' '{print $2}'|sed s/[[:space:]]//g`
#kdb_port=`cat backup8.conf |grep kdb_port|awk -F'=' '{print $2}'|sed s/[[:space:]]//g`
#kdb_host=`cat backup8.conf |grep kdb_host|awk -F'=' '{print $2}'|sed s/[[:space:]]//g`
#kdb_list=`cat backup8.conf |grep kdb_list|awk -F'=' '{print $2}'|sed s/[[:space:]]//g`
#keep_time=`cat backup8.conf |grep keep_time|awk -F'=' '{print $2}'|sed s/[[:space:]]//g`

echo "This tool help us to check our logical dump file"
echo ""

if [[ $# -eq 1 ]] && [[ $1 = "-h" ]] || [[ $1 = "--help" ]];then
    echo "Usage:"
    echo "1.check all dump file:"
    echo "  ./backup8_check.sh "
    echo ""
    echo "2.check the latest dump file for every database:"
    echo "  ./backup8_check.sh latest"
    echo ""
    exit
fi

date=$(date '+%Y-%m-%d-%H')
kdbback_final="${kdbback_dest}/kdbback_final"
LD_LIBRARY_PATH="${kdb_home}/lib"

echo "check begin : $(date '+%Y-%m-%d %H:%M:%S')"
if [[ $# -eq 1 ]] && [[ $1 = "latest" ]];then
    echo "we just check the latest logical dump file for every database"
    check_latest=$1
else
    echo "check all logical dump file for every database"
    check_latest="all"
fi

####################### kingbase backup dest test ##################

echo "kdbback_dest: "$kdbback_dest
if [ ! -d ${kdbback_dest} ];then
    echo "backup dest is not exist, please check your backup8.conf!"
    exit -1
fi
echo ""


####################### kingbase backup check  #######################

cd ${kdbback_dest}
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH
echo "1.start check all database's logical backup file"
for db in `echo $kdb_list | sed 's/,/ /g'`; do
    echo "---------------------------------------------------------------------"
    echo "begin check database's logical backup, database name: " $db 
	if [ ! -d ${db} ]; then
	    echo ">>>database dose not have logical backup, please check it! "$kdbback_dest/$db
	    continue
    fi
    cd ${db}
    ##### kingbase server check as follows
    ${kdb_home}/bin/ksql -p ${kdb_port} -U ${kdb_user}  -c "select now();" $db> /dev/null 2>&1
    if [ $? -ne 0 ] ;then
        echo "database: $db is not alive, please check it!" 
        ###TODO: database is not alive, do't care
    else
        echo "kingbase server is runing" 
    fi
    ##### end

    ##### kingbase backup files process as follows
    backup_files=$(ls $db*.tar.gz)
    if [ $check_latest = "latest" ]; then backup_files=$(ls -lrth $db*.tar.gz|tail -n 1|awk '{print $9}');fi
    for backup_file in $backup_files;
    do
        echo "----------------------"
        echo "begin check backup file: "$backup_file
        tar xzOf $backup_file|$kdb_home/bin/sys_restore -l
        echo "end check backup file: "$backup_file
        echo "----------------------"
        echo ""
    done

    echo "end check database $db's logical backup" 
    echo "---------------------------------------------------------------------"
    echo ""

    cd ../
done

echo "2.backup file list: "
for db in `echo $kdb_list | sed 's/,/ /g'`; do
    echo "database: $db, path: $kdbback_dest/$db"
    ls -rlth ${db}/${db}*.tar.gz
    echo ""
done

echo "$(date '+%Y-%m-%d %H:%M:%S') : end check all database's logical backup file"
echo ""
exit 0

1.4  fast_deploy_backup8.sh

#!/bin/bash

####################################################################################################################                                                                                                                                                  ###                                                                                                                                                                                                                                                                   ### Descipt: this script help us update database more easily
###                                                                                                                                                                                                                                                                  
### Descipt: this script help us to deploy logical backup (backup8.sh) schedual into crontab 
### Author : HM
### Create : 2020-04-28
### 
### Usage  :
###        1.modify the backup8.conf file: input correct database conf, like this:
###        kdb_home=/opt/Kingbase/ES/V8/Server
###        kdbback_dest = /data/kingbase/dump/
###        kdb_user=SYSTEM
###        kdb_pass=system
###        kdb_port=54323
###        kdb_host=127.0.0.1
###        kdb_list = TEST,SAMPLES
###        keep_time = 7
###
###        2.execute deploy:
###        ./fast_deploy_backup8.sh
###
####################################################################################################################

echo "This tool help use to deploy logical backup (backup8.sh) schedual into crontab, must execute in local server"
echo ""

####################################################################################################################
echo "1.check back dest:"
kdbback_dest=`cat backup8.conf |grep kdbback_dest|awk -F'=' '{print $2}'|sed s/[[:space:]]//g`
if [ ! -d $kdbback_dest ];then
    echo "create back dest dir: "$kdbback_dest
    mkdir -p $kdbback_dest
    chown -R kingbase:kingbase $kdbback_dest
else
    echo "back dest is exist, do not create it"
fi

echo " "


####################################################################################################################
echo "2.check backup8.sh script:"
./backup8.sh >/dev/null 2>&1
if [ $? -ne 0 ]; then
    echo "backup test failed, please check the backup8.conf"
    echo ""
    exit
else
    echo "sucess to execute backup8.sh"
fi

echo ""


####################################################################################################################
echo "3.add backup job:"
#current_dir=$(dirname $(readlink -f "$0"))
cron_conf=$(crontab -l)
cron_exist=$(crontab -l|grep backup8.sh|wc -l)
if [[ -z $cron_conf ]] || [[ $cron_conf = "no crontab for root" ]]; then
    #echo "0 2 * * * sh $PWD/backup8.sh >/dev/null 2>&1 &" >> conf && crontab conf && rm -f conf
    echo "0 2 * * * sh $PWD/backup8.sh >>$PWD/logical_backup.log 2>&1 &" >> conf && crontab conf && rm -f conf
elif [ $cron_exist -eq 0 ];then
    crontab -l > conf && echo "0 2 * * * sh $PWD/backup8.sh >>/$PWD/logical_backup.log 2>&1 &" >> conf && crontab conf && rm -f conf
else
    echo "do not need deploy backup8.sh in crontab"
fi
echo "complated"

echo ""


####################################################################################################################
echo "4.show current crontab list:"
crontab -l

echo ""
echo "end"

# 金仓安装路径中查找  backup8.sh 文件

find / -name 'backup8.sh'

## /opt/Kingbase/ES/V8/KESRealPro/V008R006C008B0020/SupTools/kb_backup/backup8.sh

1.1 手动执行备份一次

sh -x backup8.sh

2. 定时执行备份

sudo crontab -e

##编辑 内容  (定时任务执行配置,早上11点执行:)
0 11 * * * /opt/Kingbase/ES/V8/SupTools/kb_backup/backup8.sh

2.1## 启动 定时任务
sudo systemctl start crond 

2.2## 查询 crond 的日志文件
 cd  /var/log/
 cat cron

3.备份恢复

3.1 进入金仓安装路劲  /opt/Kingbase/ES/V8/Server/bin

3.2 执行 

./sys_restore -Usystem -dDBNAME   /opt/data/db/111_2024082115.dmp

其中 system:数据库用户名  DBNAME:数据库名称

 /opt/data/db/111_2024082115.dmp:备份文件位置

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王大锤4391

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值