#!/bin/bash
#
source /home/postgres/.bash_profile
IP=$(/sbin/ifconfig |grep -B1 "inet" |awk '{ if ( $1 == "inet" ) { print $2 } else if ( $2 == "Link" ) { printf "%s:" ,$1 } else if ($3 == "mtu" ){ printf "%s" ,$1}}'|awk -F: '{ if ( $2 == "addr" ) { print $1 ": " $3 } else { print $1 ": " $2 } }'|grep -E "eth0|em1|eth1|eth4|ens"|awk '{print $2}')
echo $IP
Port=`netstat -tunlp|grep postgres |grep -v grep|grep -v tcp6|awk '{print $4}'|awk -F ':' '{print $2}'|sort -u`
for i in `echo $Port`
do
PORT=$i
if [ ! -d /data/dbbackup/${IP}_${PORT} ];then
mkdir -pv /data/dbbackup/${IP}_${PORT}/arch
chmod 700 /data/dbbackup/${IP}_${PORT}/arch
fi
Date=`date +%Y%m%d`
if [ ! -d /data/dbbackup/${IP}_${PORT}/base_db_${Date} ];then
mkdir -pv /data/dbbackup/${IP}_${PORT}/base_db_${Date}
chmod 700 /data/dbbackup/${IP}_${PORT}/base_db_${Date}
fi
pgdata="/data/pgsql14/5432/data"
backup_dir="/data/dbbackup/${IP}_${PORT}/base_db_${Date}"
arch_dir="/data/dbbackup/${IP}_${PORT}/arch/"
xlog_dir=`eval echo $pgdata`
echo ${xlog_dir}
PG_VERSION=`cat ${xlog_dir}/PG_VERSION`
num=`echo "$PG_VERSION < 10"|bc`
if [ "$num" -eq 1 ];then
xlog_dirpath=$xlog_dir/pg_xlog/
else
xlog_dirpath=$xlog_dir/pg_wal/
fi
pgbackup=`which pg_basebackup`
echo "Backup the $PORT base_db start !"
$pgbackup -U postgres -h 127.0.0.1 -p $PORT -D ${backup_dir} -v -P -Ft -z -Z3
echo "Backup the $PORT archive logs start !"
rsync -avz --delete ${xlog_dirpath} ${arch_dir}
done