scripts/dump_postgresql.sh

32 lines
862 B
Bash
Raw Normal View History

#!/bin/sh
BACKUPDIR=/home/backups/postgresql
LOGFILE=${BACKUPDIR}/my_dump_postgresql.log
2023-08-10 21:47:21 +02:00
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}
2023-08-10 21:47:21 +02:00
cd ${BACKUPDIR} || backup_success=0
echo "start" >> ${LOGFILE}
2023-08-10 21:47:21 +02:00
for i in $(psql --list --tuples-only --port 6432 | grep -v template0 | grep -v template1 | cut -f 2 -d " " | sed '/^$/d' | tr "\n" " ");
do
2023-08-10 21:47:21 +02:00
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
2023-08-10 21:47:21 +02:00
${SENDZABBIX} --value 0 >/dev/null
else
2023-08-10 21:47:21 +02:00
${SENDZABBIX} --value 1 >/dev/null
fi
echo "end" >> ${LOGFILE}