2005-12-28 21:19:29 +01:00
|
|
|
# -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
|
2009-05-22 15:27:09 +02:00
|
|
|
# vim: set filetype=sh sw=3 sts=3 expandtab autoindent:
|
2005-08-09 22:28:52 +02:00
|
|
|
#
|
|
|
|
# PostgreSQL handler script for backupninja
|
|
|
|
#
|
|
|
|
|
|
|
|
getconf backupdir /var/backups/postgres
|
|
|
|
getconf databases all
|
|
|
|
getconf compress yes
|
|
|
|
getconf vsname
|
|
|
|
|
|
|
|
localhost=`hostname`
|
|
|
|
|
2006-01-19 22:58:03 +01:00
|
|
|
# Decide if the handler should operate on a vserver or on the host.
|
|
|
|
# In the former case, check that $vsname exists and is running.
|
2006-01-19 22:57:45 +01:00
|
|
|
local usevserver=no
|
2006-01-19 22:58:03 +01:00
|
|
|
local vroot
|
2006-01-19 22:57:45 +01:00
|
|
|
if [ $vservers_are_available = yes ]; then
|
2006-02-02 21:28:28 +01:00
|
|
|
if [ -n "$vsname" ]; then
|
2006-01-19 22:58:03 +01:00
|
|
|
# does it exist ?
|
|
|
|
if ! vservers_exist "$vsname" ; then
|
|
|
|
fatal "The vserver given in vsname ($vsname) does not exist."
|
|
|
|
fi
|
|
|
|
# is it running ?
|
2008-06-24 17:55:10 +02:00
|
|
|
vservers_running $vsname || fatal "The vserver $vsname is not running."
|
2006-01-19 22:58:03 +01:00
|
|
|
# everything ok
|
|
|
|
info "Using vserver '$vsname'."
|
2006-01-19 22:57:45 +01:00
|
|
|
usevserver=yes
|
2006-01-19 22:58:03 +01:00
|
|
|
vroot="$VROOTDIR/$vsname"
|
|
|
|
else
|
|
|
|
info "No vserver name specified, actions will be performed on the host."
|
2006-01-19 22:57:45 +01:00
|
|
|
fi
|
2005-08-09 22:28:52 +02:00
|
|
|
fi
|
|
|
|
|
2006-01-19 22:58:03 +01:00
|
|
|
# Make sure that the system to backup has the needed executables
|
2006-01-19 22:57:45 +01:00
|
|
|
if [ $usevserver = yes ]; then
|
2006-01-19 22:58:03 +01:00
|
|
|
debug "Examining vserver '$vsname'."
|
|
|
|
if [ "$databases" == "all" ]; then
|
|
|
|
[ -x "$vroot`$VSERVER $vsname exec which $PGSQLDUMPALL`" ] || \
|
2009-05-22 14:50:52 +02:00
|
|
|
fatal "Can't find $PGSQLDUMPALL in vserver $vsname."
|
2006-01-19 22:58:03 +01:00
|
|
|
else
|
|
|
|
[ -x "$vroot`$VSERVER $vsname exec which $PGSQLDUMP`" ] || \
|
2009-05-22 14:50:52 +02:00
|
|
|
fatal "Can't find $PGSQLDUMP in vserver $vsname."
|
2006-01-19 22:58:03 +01:00
|
|
|
fi
|
2005-08-09 22:53:19 +02:00
|
|
|
else
|
2006-01-19 22:58:03 +01:00
|
|
|
if [ "$databases" == "all" ]; then
|
|
|
|
[ -x "`which $PGSQLDUMPALL`" ] || \
|
2009-05-22 14:50:52 +02:00
|
|
|
fatal "Can't find $PGSQLDUMPALL."
|
2006-01-19 22:58:03 +01:00
|
|
|
else
|
|
|
|
[ -x "`which $PGSQLDUMP`" ] || \
|
2009-05-22 14:50:52 +02:00
|
|
|
fatal "Can't find $PGSQLDUMP."
|
2006-01-19 22:58:03 +01:00
|
|
|
fi
|
2005-08-09 22:28:52 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
# create backup dir, the vroot variable will be empty if no vsname was specified
|
|
|
|
# and will proceed to operate on the host
|
2005-08-09 22:53:19 +02:00
|
|
|
[ -d $vroot$backupdir ] || (debug "mkdir -p $vroot$backupdir"; mkdir -p $vroot$backupdir)
|
2008-06-24 17:01:35 +02:00
|
|
|
[ -d $vroot$backupdir ] || fatal "Backup directory '$vroot$backupdir' does not exist, and could not be created."
|
2005-08-09 22:28:52 +02:00
|
|
|
|
|
|
|
# give backup dir the good uid and permissions
|
2006-01-19 22:57:45 +01:00
|
|
|
# (in respect to the vserver, if $usevserver = yes)
|
2006-04-24 18:16:12 +02:00
|
|
|
if [ $usevserver = yes ]; then
|
2007-02-23 19:04:04 +01:00
|
|
|
pguid=`$VSERVER $vsname exec getent passwd $PGSQLUSER | @AWK@ -F: '{print $3}'`
|
2006-04-24 18:16:12 +02:00
|
|
|
else
|
2007-02-23 19:04:04 +01:00
|
|
|
pguid=`getent passwd $PGSQLUSER | @AWK@ -F: '{print $3}'`
|
2006-04-24 18:16:12 +02:00
|
|
|
fi
|
2005-08-09 22:53:19 +02:00
|
|
|
[ -n "$pguid" ] || \
|
2009-05-22 14:50:52 +02:00
|
|
|
fatal "No user called $PGSQLUSER`[ $usevserver = no ] || echo \" on vserver $vsname\"`."
|
2005-08-09 22:28:52 +02:00
|
|
|
debug "chown $pguid $vroot$backupdir"
|
|
|
|
chown $pguid $vroot$backupdir
|
|
|
|
debug "chmod 700 $vroot$backupdir"
|
|
|
|
chmod 700 $vroot$backupdir
|
|
|
|
|
|
|
|
# if $databases = all, use pg_dumpall
|
|
|
|
if [ "$databases" == "all" ]; then
|
2009-05-22 14:50:52 +02:00
|
|
|
if [ $usevserver = yes ]; then
|
|
|
|
if [ "$compress" == "yes" ]; then
|
2010-06-14 19:06:59 +02:00
|
|
|
execstr="$VSERVER $vsname exec su - $PGSQLUSER -c \"set -o pipefail ; $PGSQLDUMPALL | $GZIP $GZIP_OPTS > '$backupdir/${vsname}.sql.gz'\""
|
2009-05-22 14:50:52 +02:00
|
|
|
else
|
2009-12-25 02:33:13 +01:00
|
|
|
execstr="$VSERVER $vsname exec su - $PGSQLUSER -c \"$PGSQLDUMPALL > '$backupdir/${vsname}.sql'\""
|
2009-05-22 14:50:52 +02:00
|
|
|
fi
|
|
|
|
else
|
|
|
|
if [ "$compress" == "yes" ]; then
|
2010-06-14 19:06:59 +02:00
|
|
|
execstr="su - $PGSQLUSER -c \"set -o pipefail ; $PGSQLDUMPALL | $GZIP $GZIP_OPTS > '$backupdir/${localhost}-all.sql.gz'\""
|
2009-05-22 14:50:52 +02:00
|
|
|
else
|
2009-12-25 02:33:13 +01:00
|
|
|
execstr="su - $PGSQLUSER -c \"$PGSQLDUMPALL > '$backupdir/${localhost}-all.sql'\""
|
2009-05-22 14:50:52 +02:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
debug "$execstr"
|
|
|
|
if [ ! $test ]; then
|
|
|
|
output=`eval $execstr 2>&1`
|
|
|
|
code=$?
|
|
|
|
if [ "$code" == "0" ]; then
|
|
|
|
debug $output
|
|
|
|
info "Successfully finished dump of pgsql cluster"
|
|
|
|
else
|
|
|
|
warning $output
|
|
|
|
warning "Failed to dump pgsql cluster"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2005-08-09 22:28:52 +02:00
|
|
|
# else use pg_dump on each specified database
|
|
|
|
else
|
2009-05-22 14:50:52 +02:00
|
|
|
for db in $databases; do
|
|
|
|
if [ $usevserver = yes ]; then
|
|
|
|
if [ "$compress" == "yes" ]; then
|
2010-06-14 19:06:59 +02:00
|
|
|
execstr="$VSERVER $vsname exec su - $PGSQLUSER -c \"set -o pipefail ; $PGSQLDUMP $db | $GZIP $GZIP_OPTS > '$backupdir/${db}.sql.gz'\""
|
2009-05-22 14:50:52 +02:00
|
|
|
else
|
2009-12-25 02:33:13 +01:00
|
|
|
execstr="$VSERVER $vsname exec su - $PGSQLUSER -c \"$PGSQLDUMP $db | > '$backupdir/${db}.sql'\""
|
2009-05-22 14:50:52 +02:00
|
|
|
fi
|
|
|
|
else
|
|
|
|
if [ "$compress" == "yes" ]; then
|
2010-06-14 19:06:59 +02:00
|
|
|
execstr="su - $PGSQLUSER -c \"set -o pipefail ; $PGSQLDUMP $db | $GZIP $GZIP_OPTS > '$backupdir/${db}.sql.gz'\""
|
2009-05-22 14:50:52 +02:00
|
|
|
else
|
2009-12-25 02:33:13 +01:00
|
|
|
execstr="su - $PGSQLUSER -c \"$PGSQLDUMP $db > '$backupdir/${db}.sql'\""
|
2009-05-22 14:50:52 +02:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
debug "$execstr"
|
|
|
|
if [ ! $test ]; then
|
|
|
|
output=`eval $execstr 2>&1`
|
|
|
|
code=$?
|
|
|
|
if [ "$code" == "0" ]; then
|
|
|
|
debug $output
|
|
|
|
info "Successfully finished dump of pgsql database ${db}"
|
|
|
|
else
|
|
|
|
warning $output
|
|
|
|
warning "Failed to dump pgsql database ${db}"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
2005-08-09 22:28:52 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|