scripts/dump_postgresql.sh

32 lines
862 B
Bash
Executable File

#!/bin/sh
BACKUPDIR=/home/backups/postgresql
LOGFILE=${BACKUPDIR}/my_dump_postgresql.log
SENDZABBIX="zabbix_sender --config /usr/local/etc/zabbix6/zabbix_agentd.conf --key pgsql.backups"
HOSTNAME=$(/bin/hostname -f | /usr/bin/cut -d '.' -f 1,2)
backup_success=1
mkdir -p ${BACKUPDIR}
rm -f ${LOGFILE}
cd ${BACKUPDIR} || backup_success=0
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
printf " ---> backing up %s..." "$i" >> ${LOGFILE}
pg_dump --port 6432 --format c "${i}" > "${i}".dump 2>> ${LOGFILE} 2>&1;
if [ $? ];
then
echo " OK" >> ${LOGFILE};
else
backup_success=0
fi
done
if [ $backup_success -eq 1 ]
then
${SENDZABBIX} --value 0 >/dev/null
else
${SENDZABBIX} --value 1 >/dev/null
fi
echo "end" >> ${LOGFILE}