Skip to content

Instantly share code, notes, and snippets.

@Spindel
Created February 1, 2017 12:09
Show Gist options
  • Save Spindel/8dfba6ffa20883ae3a6290d1ca670e16 to your computer and use it in GitHub Desktop.
Save Spindel/8dfba6ffa20883ae3a6290d1ca670e16 to your computer and use it in GitHub Desktop.
#! /bin/bash -e
# Note "-e" above, as that's what prevents removing old dump
# when new dump fails.
TSPEC=$(date "+%F_%T")
filename="/backup/modio-$TSPEC"
workname="/backup/running"
backup="/backup/old-in-progress"
function send {
test -e /usr/bin/zabbix_sender && echo "- $1 $2" | \
/usr/bin/zabbix_sender -c /etc/zabbix/zabbix_agentd.conf -s $(hostname --fqdn) -i -
}
function errtrap {
send pgbackup.status 2
}
# stash away old dump
[ -f "$backup" ] && logger "pgbackup: backup dump in place, removing"
[ -f "$backup" ] && rm -f "$backup"
[ -f "$workname" ] && logger "pgbackup: existing dump in place, removing"
# stash away old dump
[ -f "$workname" ] && mv "$workname" "$backup"
# generate new dump, remove old if successful
send pgbackup.status 0
trap errtrap EXIT
su - postgres -c "/bin/nice /usr/bin/pg_dump --format=custom -f $workname modio"
mv "$workname" "$filename"
logger "pgabackup: Done backing up to $filename"
send pgbackup.status 1
trap - EXIT
send pgbackup.size $(stat --printf="%s" "$filename")
[ -f "$backup" ] && rm "$backup"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment