每日冷备份,并自动删除 7 日以前的备份,建议对热备份数据进行备份
环境:centos7
192.168.0.20:15432 ( 备份库,读库)
1 创建备份目录
mkdir -p /home/data/backup/
2 编写备份脚本
vim /home/data/backup/backup_script.sh
cur_time=$(date '+%Y-%m-%d')
sevendays_time=$(date -d -7days '+%Y-%m-%d')
echo "开始备份数据库..."
rm -f /home/data/backup/pgsql-backup.$sevendays_time.tar.gz
echo "."
/usr/pgsql-12/bin/pg_dumpall -U postgres -p 15432 > "pgsql-backup.$cur_time.dmp"
echo "."
tar zcf "/home/data/backup/pgsql-backup.$cur_time.tar.gz" *.dmp
echo "."
rm -f /home/data/backup/pgsql-backup.*.dmp
echo "."
echo "备份完成"
赋予可执行权限chmod a+x /home/data/backup/backup_script.sh
3 设置定时任务
设置每天晚上 2 点进行备份crontab -e
0 2 * * * /home/data/backup/backup_script.sh
来源:CSDN
作者:kaiqiu666
链接:https://blog.csdn.net/kaiqiu666/article/details/104755347