Skip to content

Instantly share code, notes, and snippets.

@paulmooring
Last active December 19, 2015 09:49
Show Gist options
  • Save paulmooring/5936312 to your computer and use it in GitHub Desktop.
Save paulmooring/5936312 to your computer and use it in GitHub Desktop.
backup script for postgresql
#!/usr/bin/env bash
# Author:: Paul Mooring (<[email protected]>)
# Copyright:: Copyright (c) 2013 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
########## Default arguments ##########
verb=0
continue_on_error=1
dir='/tmp'
file_suffix=$(date +%Y%m%d_%H%M).bkup
#######################################
debug(){
if [ -z $2 ] ; then
lvl=0
else
lvl=$2
fi
if [ $verb -gt $lvl ] ; then
echo $1
fi
}
help_msg(){
cat << EOF
Usage: ${0} [options]
Options:
-q Quiet
-v Verbose
-h Help
-e Continue on pg_dump error
-d <dir> Backup directory
-s <extention> String to append to backup file name
EOF
}
pg_bkup(){
bkup_dir=$1
bkup_file=$2
bkup_db=$3
if [ -z $4 ] ; then
die_on_error=1
else
die_on_error=$4
fi
debug "pg_bckup() for $bkup_dir $bkup_file $bkup_db" 2
debug "Checking if database exists..."
db_chk=$(psql -lqt | cut -d \| -f 1|grep -q ${bkup_db})
if [[ ${db_chk} -ne 0 ]] ; then
echo "${bkup_db} database does not exist!"
exit 2
fi
debug "Checking if backup directory exists..."
if [ ! -d $bkup_dir ] ; then
echo "${bkup_dir} doesn't exist or isn't a directory!"
exit 3
fi
debug "Checking if backup file exists..."
if [ -e ${bkup_dir}/${bkup_file} ] ; then
echo "${bkup_dir}/${bkup_file} already exists!"
exit 4
fi
debug "running: pg_dump -Fc -b -f ${bkup_dir}/${bkup_file} $bkup_db"
pg_dump -Fc -b -f ${bkup_dir}/${bkup_file} $bkup_db
dump_stat=$?
if [[ $dump_stat -ne 0 ]] ; then
echo "error backing up $bkup_db"
if [[ $die_on_error -gt 0 ]] ; then
exit $dump_stat
fi
fi
}
while getopts ":vqhd:s:" opt; do
case $opt in
h)
help_msg
exit 0
;;
v)
verb=$(expr $verb + 1)
;;
q)
verb=$(expr $verb - 1)
;;
e)
cont_on_error=0
;;
d)
dir=${OPTARG}
;;
s)
file_suffix=${OPTARG}
;;
\?)
echo "Invalid option: -$OPTARG" >&2
help_msg
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
help_msg
exit 1
;;
esac
done
bkup_dir=${dir}/$(date +%Y%m%d)
mkdir -p $bkup_dir
dbs=$(psql -l -t | cut -d'|' -f1 | sed -e 's/ //g' -e '/^$/d')
for db in $dbs ; do
if ! [[ $db == "template0" || $db == "template1" ]] ; then
pg_bkup $bkup_dir ${db}_${file_suffix} $db $continue_on_error
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment