colorful_background_5-wallpaper-2560x1600

操作环境

操作系统:CentOS Linux release 7.3.1611 (Core)

数据库系统: PostgreSQL 9

IP:10.211.55.11

port: 5432

脚本

执行以下命令

vim backup.sh

chmod 755 backup.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
pg_dumpall -U postgres -p 5432 > "pgsql-backup.$cur_time.sql"
tar zcvf "/var/lib/pgsql/bak/pgsql-backup.$cur_time.tar.gz" *.sql
echo "Remove temp file ..."
rm -rf pgsql-backup.*.sql
echo "Finish Backup ..."

crontab设定定时备份

每天临晨2点自动执行备份脚本

crontab -e
#加入 0 2 * * * /var/lib/pgsql/backup.sh