Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Backup postgres database with SLACK notifications (put in your slack webhook URL)
#!/bin/bash
#
# Script to backup local
# Postgres database
# Run via cron job
#
#########################################
#########################################
#
# Config section
#
#########################################
BACKUP_USER='postgres'
BACKUP_DIR="/data/db-nightly-backups"
BZIP=yes
FILE_NAME="myapp.database"
#########################################
#
# Slack Function
#
#########################################
SLACK_URL="PUT YOUR SLACK URL HERE!!!!"
function slack_post_good() {
curl -H "Content-type:application/json" \
-X POST -d \
'{
"channel":"#dev_ops",
"username" : "Postgres",
"icon_emoji" : ":postgres:",
"attachments" : [
{
"fallback": "DB backup succeeded",
"color" : "good",
"fields" : [
{
"title" : "DB backup succeded",
"value" : ".\nDB back up succeeded\nmsg: '"$1"'",
"short" : "true"
},
{
"title" : "'"$2"'",
"value" : ".\n'"$3"'",
"short" : "true"
}
]
}
]
}
' $SLACK_URL
}
function slack_post_bad() {
curl -H "Content-type:application/json" \
-X POST -d \
'{
"channel":"#dev_ops",
"username" : "Postgre",
"icon_emoji" : ":postgres:",
"attachments" : [
{
"fallback": "Alert!: DB Backup Failed",
"color" : "danger",
"fields" : [
{
"title" : "Alert!: DB Backup Failed",
"value" : ".\nDB back up failed\nmsg: '"$1"'",
"short" : "true"
}
]
}
]
}
' $SLACK_URL
}
#########################################
#
# Pre Backup Checks
#
#########################################
# Make sure we're running as the required backup user
if [ "$BACKUP_USER" != "" -a "$(id -un)" != "$BACKUP_USER" ]; then
echo "This script must be run as $BACKUP_USER not "$(id -un)" Exiting." 1>&2
slack_post_bad "This script must be run as $BACKUP_USER not "$(id -un)""
exit 1;
fi;
# Make sure the $BACKUP_DIR exists
if [ ! -d "$BACKUP_DIR" ]; then
echo "Directory $BACKUP_DIR does not exist. Exiting." 1>&2
slack_post_bad "Directory $BACKUP_DIR does not exist."
exit 1;
fi
# Make sure they can write to $BACKUP_DIR
if [ ! -w $BACKUP_DIR ] ; then
echo "Directory $BACKUP_DIR cannot be written to by $BACKUP_USER Exiting." 1>&2
slack_post_bad "Directory $BACKUP_DIR cannot be written to by $BACKUP_USER"
exit 1
fi
#########################################
#
#Timestamp That pre-pends the backup name
#Timestamp to track how long backup takes
#
#########################################
DATE_STAMP=`date +"%Y_%m_%d--%H_%M"`
TIMER_START=`date +%s`
#########################################
#
# Backup the Database
#
#########################################
FULL_FILE_NAME=$BACKUP_DIR/$DATE_STAMP"_"$FILE_NAME
if [ $BZIP = "yes" ]
then
FULL_FILE_NAME=$FULL_FILE_NAME.bz2
echo "Backing up $FULL_FILE_NAME"
if ! pg_dumpall | bzip2 -vf > $FULL_FILE_NAME; then
echo "pg_dumpall failed. $FULL_FILE_NAME. Exiting." 1>&2
slack_post_bad "pg_dumpall failed. $FULL_FILE_NAME."
exit 1
fi
else
echo "Backing up $FULL_FILE_NAME"
if ! pg_dumpall > $FULL_FILE_NAME; then
echo "pg_dumpall failed. $FULL_FILE_NAME. Exiting." 1>&2
slack_post_bad "pg_dumpall failed. $FULL_FILE_NAME."
exit 1
fi
fi
#########################################
#
# Confirm
#
#########################################
# If using bzip2 confirm the file is not corrupt
if [ $BZIP = "yes" ]
then
echo "Confirming BZIP is valid"
if ! bzip2 -tv $FULL_FILE_NAME; then
echo "BZIP backup is corrupt. $FULL_FILE_NAME Exiting." 1>&2
slack_post_bad "BZIP backup is corrupt.\nfile: $FULL_FILE_NAME"
exit 1
fi
fi
#########################################
#
# Backup Complete now notify!
#
#########################################
TIMER_END=`date +%s`
TOTAL_TIME=$(($TIMER_END - $TIMER_START))
STR_TIME="$(($TOTAL_TIME / 60)) min $(($TOTAL_TIME % 60)) sec"
#Account for backups taking hours
if (( $TOTAL_TIME >= 3600 ))
then
STR_TIME="$(($TOTAL_TIME / 3600)) hours $(( min=$TOTAL_TIME / 60, min % 60)) min $(($TOTAL_TIME % 60)) sec"
fi
#Get extra information about number of database backed up etc.
DB_COUNT=`ls $BACKUP_DIR | grep ".database." | wc -l`
DB_DIR_INFO=`ls -l --block-size=M $BACKUP_DIR | grep ".database." | awk '{print $9 " SIZE: " $5 " "}' | sed 's/.$/\\\n/g' | tr -d "\n"`
slack_post_good "Database backup Complete\nDB_LOC: $FULL_FILE_NAME\nTIME: $STR_TIME" \
"$DB_COUNT Databases in $BACKUP_DIR" \
"$DB_DIR_INFO"
echo ""
echo "================================"
echo ""
echo " DATABASE Backup Complete"
echo " Database save at "
echo " $FULL_FILE_NAME"
echo " BACKUP took $STR_TIME"
echo ""
echo "================================"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.