postgresql定时备份 需求: 每天凌晨4点按日期执行自动备份,生产.dump文件。备份文件保存15天。 #vim /var/lib/pgsql/backup.sh
#!/bin/bash
cur_time=$(date '+%Y-%m-%d')
fifteendays_time=$(date -d -15days '+%Y-%m-%d')
rm -rf /var/lib/pgsql/pgsql-backup.$fifteendays_time.dump
echo "Starting Backup PostgreSQL ..."
/usr/local/postgresql/bin/pg_dumpall -U postgres -p 3433 > /var/lib/pgsql/"pgsql-backup.$cur_time.dump"
echo "Finish Backup ..."
#chmod 755 /var/lib/pgsql/back.up
#su - postgres # crontab -e 00 4 * * * /var/lib/pgsql/backup.sh
还原
#psql -h localhost -p port -U user_name -d datebase_name -t table_name < /var/lib/pgsql/'date'.dump
#指定ip,端口,用户,库,表 恢复。