scripts/dump_postgresql.sh
2019-12-03 22:58:47 +01:00

31 lines
1 KiB
Bash
Executable file

#!/bin/sh
BACKUPDIR=/home/backups/postgresql
LOGFILE=${BACKUPDIR}/my_dump_postgresql.log
SENDNSCA=/usr/local/sbin/send_nsca\ -H\ <nagios_host>\ -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 " ---> backing up ${i}..." >> ${LOGFILE}
pg_dump --port 5432 ${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}