Manager Handbook for Distributed AntDB-T - P20–3
Published: 2023-08-23 04:26:19

Generate configuration files for all master node

After configuring the relevant parameters in the script, executesh http://barman-operate.sh generate_config to generate configuration files for all master nodes. After execution, check in the configuration file directory.

Set archive command for all master nodes

After the configuration file is checked, execute sh http://barman-operate.sh set_archive_cmd to set archive command for all master nodes. After execution, login to adbmgr and executeshow param nodename archive_command to check if the node’s archive command is set correctly.

Forced Log Switch

After the configuration is complete, perform a log switch operation via barman to ensure that the nodes can be archived correctly:

```
barman switch-xlog --force --archive datanode_225m 
```
Because there are many nodes in the cluster version, you can use barman switch-xlog — force — archive allto perform a log switch for all nodes.

barman configuration script

```
#!/bin/bash 
#sh barman_operate.sh generate_config|set_archive_cmd|backup_all 
 
trans_nodetype(){ 
        nodetype=$1 
        if [ "$nodetype" = "g" ]; then 
                echo "gtmcoord master" 
        elif [ "$nodetype" = "p" ]; then 
                echo "gtmcoord slave" 
        elif [ "$nodetype" = "c" ]; then 
                echo "coordinator master" 
        elif [ "$nodetype" = "d" ]; then 
                echo "datanode master" 
        elif [ "$nodetype" = "b" ]; then 
                echo "datanode slave" 
        fi 
} 
 
##Only the master node is backed up, so only the master node is given the production configuration file. 
generate_config(){ 
        echo "The configuration file of the master node will be generated according to the latest structure of the current cluster" 
        sleep 3 
        for m_node in ${masternodes[@]} 
        do 
                hostuser=`echo $m_node |cut -f 1 -d '|'` 
                hostaddr=`echo $m_node |cut -f 2 -d '|'` 
                hostname=`echo $m_node |cut -f 3 -d '|'` 
                nodetype=`echo $m_node |cut -f 4 -d '|'` 
                nodetype=`trans_nodetype $nodetype ` 
                nodename=`echo $m_node |cut -f 5 -d '|'` 
                nodeport=`echo $m_node |cut -f 6 -d '|'` 
                echo "$hostuser,$hostaddr,$hostname,$nodetype,$nodename,$nodeport" 
                cat > ${barman_home}/conf/${nodename}.conf << EOF 
[${nodename}] 
description = "$nodetype $nodename" 
ssh_command = ssh ${hostuser}@${hostaddr} -q 
conninfo = host=${hostaddr} port=${nodeport} user=${hostuser} dbname=postgres 
backup_method = rsync 
backup_options = exclusive_backup 
parallel_jobs = 3 
archiver = on 
archiver_batch_size = 50 
EOF 
        done 
} 
 
##In order to prevent the new master from setting the archive command and losing part of the wal log after the node switch, the archive command of all nodes is set at the beginning.
set_archive_cmd(){ 
        echo "Set the archive command for all nodes to the host where Barman resides" 
        sleep 3 
        for node in ${nodes[@]} 
        do 
                nodename=`echo $node |cut -f 5 -d '|'` 
                nodetype=`echo $node |cut -f 4 -d '|'` 
                nodetype=`trans_nodetype $nodetype` 
                #echo "$nodename,$nodetype" 
                #echo "set $nodetype $nodename(archive_command = 'rsync -a %p $barman_user@$barman_host:$barman_home/data/$nodename/incoming/%f');" 
                psql -h $mgrhost -p $mgrport -d postgres << EOF 
set $nodetype $nodename(archive_mode = on); 
set $nodetype $nodename(archive_command = 'rsync -a %p $barman_user@$barman_host:$barman_home/data/$nodename/incoming/%f'); 
EOF 
        done 
} 
 
barman_backup_node(){ 
        for m_node in ${masternodes[@]} 
        do 
                hostuser=`echo $m_node |cut -f 1 -d '|'` 
                hostaddr=`echo $m_node |cut -f 2 -d '|'` 
                hostname=`echo $m_node |cut -f 3 -d '|'` 
                nodetype=`echo $m_node |cut -f 4 -d '|'` 
                nodetype=`trans_nodetype $nodetype ` 
                nodename=`echo $m_node |cut -f 5 -d '|'` 
                nodeport=`echo $m_node |cut -f 6 -d '|'` 
                logfile=${barman_home}/log/barman_backup_${nodename}.log 
                barman switch-xlog --force --archive $nodename 
                nohup barman backup --immediate-checkpoint $nodename >> $logfile 2>&1 & 
        done 
} 
 
barman_replay_resume(){ 
        for m_node in ${masternodes[@]} 
        do 
                hostuser=`echo $m_node |cut -f 1 -d '|'` 
                hostaddr=`echo $m_node |cut -f 2 -d '|'` 
                hostname=`echo $m_node |cut -f 3 -d '|'` 
                nodetype=`echo $m_node |cut -f 4 -d '|'` 
                nodetype=`trans_nodetype $nodetype ` 
                nodename=`echo $m_node |cut -f 5 -d '|'` 
                nodeport=`echo $m_node |cut -f 6 -d '|'` 
                echo "execute pg_wal_replay_resume on $nodetype $nodename" 
                psql -h $hostaddr -p $nodeport -d postgres -U $hostuser << EOF 
select pg_wal_replay_resume(); 
EOF 
        done 
} 
 
usage(){ 
        echo " usage:" 
        echo " sh $0 generate_config|set_archive_cmd|backup_all" 
        echo " generate_config : Generate a barman configuration file" 
        echo " set_archive_cmd : Set the archive command for all nodes" 
        echo " backup_all : Back up data for all nodes" 
        echo " replay_resume : Execute pg_wal_replay_resume() on master node after recovery" 
} 
 
 
# var 
 
barman_home="/backup/antdb/barman" 
barman_host="server3" 
barman_user="antdb" 
mgrhost="server3" 
mgrport=16655 
 
alias mgrsql='psql "port=$mgrport host=$mgrhost dbname=postgres options='\''-c command_mode=sql'\''"' 
nodesql="select host.hostuser||'|'||host.hostaddr||'|'||host.hostname||'|'||node.nodetype||'|'||node.nodename||'|'||node.nodeport from pg_catalog.mgr_node node, pg_catalog.mgr_host host where 1=1 and node.nodehost=host.oid order by node.nodename;" 
nodes=`mgrsql -q -t -c "$nodesql"` 
masternodesql="select host.hostuser||'|'||host.hostaddr||'|'||host.hostname||'|'||node.nodetype||'|'||node.nodename||'|'||node.nodeport from pg_catalog.mgr_node node, pg_catalog.mgr_host host where 1=1 and node.nodehost=host.oid and node.nodetype in ('g','d','c') order by node.nodename;" 
masternodes=`mgrsql -q -t -c "$masternodesql"` 
operate=$1 
 
if [ "x"$operate == "xgenerate_config" ];then 
        generate_config 
elif [ "x"$operate == "xset_archive_cmd" ];then 
        set_archive_cmd 
elif [ "x"$operate == "xbackup_all" ];then 
        barman_backup_node 
elif [ "x"$operate == "xreplay_resume" ];then 
        barman_replay_resume 
else 
        echo " option is invalid" 
        usage 
fi 
```
Note that the following variables are to be modified in the script:

•

barman_home

•

barman_host

•

barman_user

•

mgrhost

•

mgrport