postgres简易的备份方案


pg安装官网:https://www.postgresql.org/download/linux/ubuntu/

如果只是安装客户端:

 sudo apt update && sudo apt upgrade

sudo apt install postgresql-client postgresql-client-common libpq-dev

crontab -e 
0 3 * * * /data/crond/pg_backup.sh

cat /data/crond/pg_backup.sh
#!/bin/bash
backupdir=/data/backup/
export PGPASSWORD=11111111
echo "backup start" `date` >> $backupdir/backup_history.log

/usr/bin/pg_dump -h 127.0.0.1 -p 5432 -U postgres -F c -b -v -f $backupdir/persimmon_production.backup_`date +%Y%m%d%H%M%S` persimmon_production  >> $backupdir/backup_history.log 2>&1

echo "backup end. " `date` >> $backupdir/backup_history.log

# 删除超过60天的备份文件
cd $backupdir && find ./ -name "persimmon*.bak" -ctime +60 -exec rm {} \;

日志文件使用系统logrotate服务,保留7天
cat /etc/logrotate.d/postgres 
/data/backup/*.log {
        su root list
        daily
        size 100M
        missingok
        rotate 7
        compress
        delaycompress
        notifempty
        create 664 deploy deploy
        copytruncate
}       


还原数据:
$ sudo pg_restore -h '127.0.0.1' -U postgres -p 5432 -d “database” -c  “file”

阅读量: 873
发布于:
修改于: