1 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
3 # PostgreSQL handler script for backupninja
6 getconf backupdir /var/backups/postgres
13 # Decide if the handler should operate on a vserver or on the host.
14 # In the former case, check that $vsname exists and is running.
17 if [ $vservers_are_available = yes ]; then
18 if [ -n "$vsname" ]; then
20 if ! vservers_exist "$vsname" ; then
21 fatal "The vserver given in vsname ($vsname) does not exist."
24 vservers_running $vsname || fatal "The vserver $vsname is not running."
26 info "Using vserver '$vsname'."
28 vroot="$VROOTDIR/$vsname"
30 info "No vserver name specified, actions will be performed on the host."
34 # Make sure that the system to backup has the needed executables
35 if [ $usevserver = yes ]; then
36 debug "Examining vserver '$vsname'."
37 if [ "$databases" == "all" ]; then
38 [ -x "$vroot`$VSERVER $vsname exec which $PGSQLDUMPALL`" ] || \
39 fatal "Can't find $PGSQLDUMPALL in vserver $vsname."
41 [ -x "$vroot`$VSERVER $vsname exec which $PGSQLDUMP`" ] || \
42 fatal "Can't find $PGSQLDUMP in vserver $vsname."
45 if [ "$databases" == "all" ]; then
46 [ -x "`which $PGSQLDUMPALL`" ] || \
47 fatal "Can't find $PGSQLDUMPALL."
49 [ -x "`which $PGSQLDUMP`" ] || \
50 fatal "Can't find $PGSQLDUMP."
54 # create backup dir, the vroot variable will be empty if no vsname was specified
55 # and will proceed to operate on the host
56 [ -d $vroot$backupdir ] || (debug "mkdir -p $vroot$backupdir"; mkdir -p $vroot$backupdir)
57 [ -d $vroot$backupdir ] || fatal "Backup directory '$vroot$backupdir' does not exist, and could not be created."
59 # give backup dir the good uid and permissions
60 # (in respect to the vserver, if $usevserver = yes)
61 if [ $usevserver = yes ]; then
62 pguid=`$VSERVER $vsname exec getent passwd $PGSQLUSER | @AWK@ -F: '{print $3}'`
64 pguid=`getent passwd $PGSQLUSER | @AWK@ -F: '{print $3}'`
67 fatal "No user called $PGSQLUSER`[ $usevserver = no ] || echo \" on vserver $vsname\"`."
68 debug "chown $pguid $vroot$backupdir"
69 chown $pguid $vroot$backupdir
70 debug "chmod 700 $vroot$backupdir"
71 chmod 700 $vroot$backupdir
73 # if $databases = all, use pg_dumpall
74 if [ "$databases" == "all" ]; then
75 if [ $usevserver = yes ]; then
76 if [ "$compress" == "yes" ]; then
77 execstr="$VSERVER $vsname exec su - $PGSQLUSER -c \"$PGSQLDUMPALL | $GZIP > $backupdir/${vsname}.sql.gz\""
79 execstr="$VSERVER $vsname exec su - $PGSQLUSER -c \"$PGSQLDUMPALL > $backupdir/${vsname}.sql\""
82 if [ "$compress" == "yes" ]; then
83 execstr="su - $PGSQLUSER -c \"$PGSQLDUMPALL | $GZIP > $backupdir/${localhost}-all.sql.gz\""
85 execstr="su - $PGSQLUSER -c \"$PGSQLDUMPALL > $backupdir/${localhost}-all.sql\""
90 output=`eval $execstr 2>&1`
92 if [ "$code" == "0" ]; then
94 info "Successfully finished dump of pgsql cluster"
97 warning "Failed to dump pgsql cluster"
101 # else use pg_dump on each specified database
103 for db in $databases; do
104 if [ $usevserver = yes ]; then
105 if [ "$compress" == "yes" ]; then
106 execstr="$VSERVER $vsname exec su - $PGSQLUSER -c \"$PGSQLDUMP $db | $GZIP > $backupdir/${db}.sql.gz\""
108 execstr="$VSERVER $vsname exec su - $PGSQLUSER -c \"$PGSQLDUMP $db | > $backupdir/${db}.sql\""
111 if [ "$compress" == "yes" ]; then
112 execstr="su - $PGSQLUSER -c \"$PGSQLDUMP $db | $GZIP > $backupdir/${db}.sql.gz\""
114 execstr="su - $PGSQLUSER -c \"$PGSQLDUMP $db > $backupdir/${db}.sql\""
119 output=`eval $execstr 2>&1`
121 if [ "$code" == "0" ]; then
123 info "Successfully finished dump of pgsql database ${db}"
126 warning "Failed to dump pgsql database ${db}"