Last active
August 29, 2015 14:25
-
-
Save patmandenver/dfdd5f120789363592a1 to your computer and use it in GitHub Desktop.
Simple backup script for a postgres database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" | |
######################################### | |
# | |
# 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 | |
exit 1; | |
fi; | |
# Make sure the $BACKUP_DIR exists | |
if [ ! -d "$BACKUP_DIR" ]; then | |
echo "Directory $BACKUP_DIR does not exist. Exiting." 1>&2 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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