PostgreSQL basebackup备份

#!/bin/sh

lst=(
127.0.0.1:5432:xm_rep:123456
127.0.0.1:5432:xm_rep:123456
)

for item in ${lst[@]}
do
        host=$(echo ${item} | awk -F':' '{print $1}')
        port=$(echo ${item} | awk -F':' '{print $2}')
        user=$(echo ${item} | awk -F':' '{print $3}')
        passwd=$(echo ${item} | awk -F':' '{print $4}')

TMP="/home/work/data/tmp/dump.sh"

t=`date '+%Y%m%d%H%M%S'`
d="data${port}_${t}"

rm -f $TMP
# create expect script
cat > $TMP << EOF
set timeout 432000
spawn /home/work/app/postgresql-9.6.1/bin/pg_basebackup -h ${host} -p ${port} -U ${user} -F p -x -R -l backup${t} -r 30M -D /home/work/data/backup/postgresql/${d}
expect "Password:"
send "${passwd}\n"
expect eof
exit
EOF

/usr/bin/expect -f $TMP

cd /home/work/data/backup/postgresql/

tar zcf ${d}.tar.gz ${d}

find ./ -mtime +15 -name "*.tar.gz" -exec rm -rf {} \;

rm -rf ${d}

rm -f $TMP
done



pg_backup.sh 
#!/bin/sh

lst=(
127.0.0.1:5016:xm_rep:password
127.0.0.1:5006:xm_rep:password
127.0.0.1:5018:xm_rep:password
127.0.0.1:5022:xm_rep:password
)

> ~/.pgpass
chmod 600 ~/.pgpass

for item in ${lst[@]}
do
    host=$(echo ${item} | awk -F':' '{print $1}')
    port=$(echo ${item} | awk -F':' '{print $2}')
    db=$(echo ${item} | awk -F':' '{print $3}')
    user=$(echo ${item} | awk -F':' '{print $4}')
    passwd=$(echo ${item} | awk -F':' '{print $5}')
    echo "${host}:${port}:*:${user}:${passwd}" >> ~/.pgpass

    t=`date '+%Y%m%d%H%M%S'`
    d="${host}_${port}_${db}_data${port}_${t}"

    /home/work/app/postgresql/bin/pg_basebackup -h ${host} -p ${port} -U ${user} -w -F t -P -x -R -l backup${t} -r 30M -D - | lz4 -B4 | openssl enc -e -aes-256-cbc -k "key"
> /home/work/data/backup/postgresql/${d}.tar.lz4

    find /home/work/data/backup/postgresql/ -mtime +15 -name "*.tar.lz4" -exec rm -rf {} \;
done

你可能感兴趣的:(PostgreSQL)