如何检查 postgresql 备份是否成功?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6341321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-20 23:04:29  来源:igfitidea点击:

How to check if postgresql backup was successful?

postgresqlbackuprestorepg-dump

提问by Dragan Matic

We have a postgresql database that is backed up nightly from a cron job with a following command:

我们有一个 postgresql 数据库,它使用以下命令从 cron 作业中每晚备份:

su postgres -c "pg_dump our_database | gzip > /home/smb/shared/database_backup.bak.gz"

recently we had a disk failure that started with a few bad sectors and during that time pg_dump exited with the following errors

最近我们有一个磁盘故障,开始时有几个坏扇区,在此期间 pg_dump 退出并出现以下错误

pg_dump: SQL command failed
pg_dump: Error message from server: ERROR: catalog is missing 17 attribute(s) from relid 20158
pd_dump: The command was: LOCK TABLE public.obvez IN ACCESS SHARE MODE

Now, since it was in cron job, nobody noticed error messages, the backup was interrupted but it wasn't zero sized, everything seemed ok and the error went unnoticed until final disk failure when we realized we didn't have backup.

现在,由于它是在 cron 作业中,没有人注意到错误消息,备份被中断,但它的大小不是零,一切似乎都很好,直到最终磁盘故障,当我们意识到我们没有备份时才注意到错误。

We managed to restore data from an older backup but now I would like to know what would be the proper way to check if pg_dump finished its job with success or not?

我们设法从较旧的备份中恢复数据,但现在我想知道检查 pg_dump 是否成功完成工作的正确方法是什么?

回答by Berry Langerak

I write the outcome into a logfile, and at the end of the cronjob, I send the content of the logfile to my e-mail address. That way, I'll know when something went wrong.

我将结果写入日志文件,并在 cronjob 结束时,将日志文件的内容发送到我的电子邮件地址。这样,我就会知道什么时候出了问题。

su postgres "pg_dump our_database 2>> $LOG_FILE | gzip > /home/smb/shared/database_backup.bak.gz"
cat $LOG_FILE | mailx $MAINTAINERS -s "Postgresql backup"

ADDENDUM: if you want to send the e-mail only if anything went wrong, you can check the return code of pg_dump:

附录:如果您只想在出现问题时才发送电子邮件,您可以检查 pg_dump 的返回码:

LOG_FILE=/tmp/pgdump.err

if ! pg_dump -U backupuser "our_database" 2> $LOG_FILE 
then 
    cat $LOG_FILE | mailx 'youremailaddress' -s "Postgresql backup failure!"
fi