From 9dd0cb98f80279b1216afb362bf0c00ca4bd11cc Mon Sep 17 00:00:00 2001 From: n Date: Tue, 3 Dec 2019 21:30:20 +0100 Subject: [PATCH] dump all Postgres databases (and send a report to the monitoring system) --- README.md | 1 + dump_postgresql.sh | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100755 dump_postgresql.sh diff --git a/README.md b/README.md index 8580a07..4a18f11 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ Scripts * [cleanmaildir.sh](https://forge.tourmentine.com/n/scripts/src/master/cleanmaildir.sh) => delete old mail in maildirs * [dodo.sh](https://forge.tourmentine.com/n/scripts/src/master/dodo.sh) => turn off screen using DPMS * [dump_mysql.sh](https://forge.tourmentine.com/n/scripts/src/master/dump_mysql.sh) => dump all mysql databases (and send a report to the monitoring system) + * [dump_postgresql.sh](https://forge.tourmentine.com/n/scripts/src/master/dump_postgresql.sh) => dump all Postgres databases (and send a report to the monitoring system). based on [dump_mysql.sh](https://forge.tourmentine.com/n/scripts/src/master/dump_mysql.sh). * [opdsupdates.py](https://forge.tourmentine.com/n/scripts/src/master/opdsupdates.py) => look for OPDS catalog updates and send a mail if any * [randomwallpaper.sh](https://forge.tourmentine.com/n/scripts/src/master/randomwallpaper.sh) => display a random wallpaper * [secupdate](https://forge.tourmentine.com/n/scripts/src/master/secupdate) => apply security updates & recompile kernel (FreeBSD) diff --git a/dump_postgresql.sh b/dump_postgresql.sh new file mode 100755 index 0000000..03d13cd --- /dev/null +++ b/dump_postgresql.sh @@ -0,0 +1,31 @@ +#!/bin/sh + +BACKUPDIR=/home/backups/postgresql +LOGFILE=${BACKUPDIR}/my_dump_postgresql.log +SENDNSCA=/usr/local/sbin/send_nsca\ -H\ \ -d\ ';;'\ -c\ /usr/local/etc/nagios/send_nsca.cfg +HOSTNAME=`/bin/hostname -f | /usr/bin/cut -d '.' -f 1,2` +backup_success=1 + +mkdir -p ${BACKUPDIR} +rm -f ${LOGFILE} +cd ${BACKUPDIR} +echo "start" >> ${LOGFILE} +for i in `psql --list --tuples-only --port 6432 | grep -v template0 | grep -v template1 | cut -f 2 -d " " | sed '/^$/d' | tr "\n" " "`; +do + echo -n " ---> backuping ${i}..." >> ${LOGFILE} + pg_dump --port 6432 ${i} > ${i}.sql && rm -f ${i}.sql.bz2 && bzip2 ${i}.sql >> ${LOGFILE}; + if [ $? -eq 0 ]; + then + echo " OK" >> ${LOGFILE}; + else + backup_success=0 + fi +done + +if [ $backup_success -eq 1 ] +then + echo "${HOSTNAME};;postgresql_backups;;0;OK - backup succeeded at `date`" | ${SENDNSCA} >/dev/null +else + echo "${HOSTNAME};;postgresql_backups;;2;CRITICAL - backup failed at `date`, please check ${LOGFILE} for complete report." | ${SENDNSCA} >/dev/null +fi +echo "end" >> ${LOGFILE}