How to check if postgresql backup was successful?
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.
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:
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
Dragan Matic
Updated on April 09, 2020Comments
-
Dragan Matic about 4 years
We have a postgresql database that is backed up nightly from a cron job with a following command:
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: 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.
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?
-
Dragan Matic almost 13 yearsThe problem is that this database is actually at customer's site, and I'm sure they would know how to read a log file. I suppose I could modify this and to send an e-mail only if something went wrong.
-
Berry Langerak almost 13 years@Dragan Matic: Ah, you didn't state that in your original message. I've expanded the example to send mail only when pg_dump returned something other than "0" (which is success). Also, you should consider using a dedicated "backup user" which is allowed to backup, and use .pgpass instead of sudo. Well, at least, I don't like to sudo if I can help it ;)