备份脚本
备份7天内的数据
#! /bin/bash
cur_time=$(date '+%Y-%m-%d')
sevendays_time=$(date -d -7days '+%Y-%m-%d')
export PGPASSWORD=your password
echo 'Starting Backup PostgreSQL ...'
rm -rf pgsql-backup.$sevendays_time.tar.gz
pg_dumpall -h 192.168.1.234 -U postgres -p 5432 -f "pgsql-backup.$cur_time.dmp"
tar zcvf "/opt/backup/pgsql-backup.$cur_time.tar.gz" *.dmp
echo "Remove temp file ..."
rm -rf pgsql-backup.*.dmp
echo "Finish Backup ..."
定时执行脚本
编辑 crontab
crontab -e
0 2 * * * /opt/bin/backup.sh
docker方式的备份
需要预先配置一个挂载目录,用于备份存储备份文件
因为之前挂载了目录 /var/lib/postgresql/data 至host目录,可以选择将备份文件存放之对应目录下
#! /bin/bash
cur_time=$(date '+%Y-%m-%d')
yourDirect=/user/local/data/pg9.6
sevendays_time=$(date -d -7days '+%Y-%m-%d')
cd /opt/pg && pwd
echo 'Starting Backup PostgreSQL ...'
rm -rf "$yourDirect/backup/pgsql-backup.$sevendays_time.tar.gz"
docker exec pg9.6 pg_dumpall -h 127.0.0.1 -U postgres -p 5432 -f "$yourDirect/backup/pgsql-backup.$cur_time.dmp"
tar zcvf "/opt/pg/backup/pgsql-backup.$cur_time.tar.gz" *.dmp
echo "Remove temp file ..."
rm -rf pgsql-backup.*.dmp
echo "Finish Backup ..."
最后,在host上使用crontab 配置定时任务