#!/bin/bash
source /etc/profile
# 定义host、账号、密码,需要自定义
pg_host='172.16.4.8'
pg_user='postgres'
export PGPASSWORD=postgres
pg_port='5432'
# 定义备份目录、备份库、文件格式,需要自定义
date_day=`date +%F`
bak_dir='/tmp/pg-bak'
mkdir -p $bak_dir
pg_dump -h $pg_host -U $pg_user -d gis -f $bak_dir/$date_day.sql
if [[ $? -eq 0 ]];then
echo "$(date +%F-%T) 备份成功" >> $bak_dir/pg_bak.log
cd $bak_dir && tar zcf $date_day.tar $date_day.sql
if [[ $? -eq 0 ]];then
rm -f $bak_dir/$date_day.sql
fi
fi
find $bak_dir -mtime +15 -exec rm {} \;