pg_dump -U username -F t -b -v -f "$BACKUP_DIR/$FILENAME" dbname
创建定时任务每日2点执行一次bash文件备份数据库。
#!/bin/bash
BACKUP_DIR="/backup"
DATE=$(date +%Y%m%d%H%M%S)
FILENAME="pgsql_backup_$DATE.sql"
# 全备份
pg_dump -U username -F t -b -v -f "$BACKUP_DIR/$FILENAME" dbname
# 或者,物理备份
# pg_basebackup -h hostname -U username -D "$BACKUP_DIR" -F t -P
crontab -e
0 2 * * * /path/to/your/script/backup_pgsql.sh
全量恢复整个备份
pg_restore -U myuser -d postgres /backup