postgresql的备份与还原

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"

posted @ 2019-09-06 18:00  zhao379028604  阅读(3486)  评论(0编辑  收藏  举报