postgresql的備份可以借助於腳本和contron來完成
備份腳本
該腳本主要用於一天進行一次備份,備份保留一個星期。如果是其它的備份策略請對應的調整相關的參數
#!/bin/bash
## 設置備份的地址
backuppath=/var/lib/pgsql/9.4/backups
# 獲取當前時間
cur_time=$(date '+%Y-%m-%d')
sevendays_time=$(date -d -7days '+%Y-%m-%d')
rm -f $backuppath/{dbname}$sevendays_time.bak
pg_dump --port 5432 --username "postgres" --no-password --format custom --blobs --verbose --file "$backuppath/{dbname}$cur_time.bak" "{dbname}"
還原腳本
backuppath=/var/lib/pgsql/9.5/backups
read -p "please enter restore date:" cur_date
pg_restore --host "127.0.0.1" --port "5432" --username "postgres" --no-password --dbname "{dbname}" --verbose --clean "$backuppath/{dbname}$cur_date.bak"