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 for the master node will be generated based on 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 some wal logs from being lost because the new master didn't have time to set up the archive commands after the node switch, set up the archive commands for all nodes at the very beginning.
set_archive_cmd(){
echo "will set the archive command for all nodes to the host where barman is located"
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 barman configuration file"
echo " set_archive_cmd : Set the archive command for all nodes"
echo " backup_all : Backup data on all nodes"
echo " replay_resume : Execute pg_wal_replay_resume() on the 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