编写脚本

vi /home/postgres/db-backup/backup-db.sh

脚本内容

#!/bin/bash
cur_time=$(date '+%Y-%m-%d')
sevendays_time=$(date -d -7days '+%Y-%m-%d')
echo "Starting Backup PostgreSQL ..."
rm -rf pgsql-backup.$sevendays_time.tar.gz
/usr/pgsql-11/bin/pg_dump -U postgres WildlifeMonitoring > "/home/postgres/db-backup/WildlifeMonitoring-$cur_time.bak"
tar zcvf "/home/postgres/db-backup/pgsql-backup.$cur_time.tar.gz" *.bak
echo "Remove temp file ..."
rm -rf pgsql-backup.*.dmp
echo "Finish Backup ..."

编写定时任务

crontab  -e
0 0 2 * * ? /home/postgres/db-backup/backup-db.sh
systemctl restart crond