pgsql: clarify error message when backupdir does not exist
[matthijs/upstream/backupninja.git] / handlers / pgsql.in
1 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
2 #
3 # PostgreSQL handler script for backupninja
4 #
5
6 getconf backupdir /var/backups/postgres
7 getconf databases all
8 getconf compress yes
9 getconf vsname
10
11 localhost=`hostname`
12
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.
15 local usevserver=no
16 local vroot
17 if [ $vservers_are_available = yes ]; then
18    if [ -n "$vsname" ]; then
19       # does it exist ?
20       if ! vservers_exist "$vsname" ; then
21          fatal "The vserver given in vsname ($vsname) does not exist."
22       fi
23       # is it running ?
24       $VSERVERINFO -q $vsname RUNNING
25       if [ $? -ne 0 ]; then
26          fatal "The vserver $vsname is not running."
27       fi
28       # everything ok
29       info "Using vserver '$vsname'."
30       usevserver=yes
31       vroot="$VROOTDIR/$vsname"
32    else
33       info "No vserver name specified, actions will be performed on the host."
34    fi
35 fi
36
37 # Make sure that the system to backup has the needed executables
38 if [ $usevserver = yes ]; then
39    debug "Examining vserver '$vsname'."
40    if [ "$databases" == "all" ]; then
41       [ -x "$vroot`$VSERVER $vsname exec which $PGSQLDUMPALL`" ] || \
42          fatal "Can't find $PGSQLDUMPALL in vserver $vsname."
43    else
44       [ -x "$vroot`$VSERVER $vsname exec which $PGSQLDUMP`" ] || \
45          fatal "Can't find $PGSQLDUMP in vserver $vsname."
46    fi
47 else
48    if [ "$databases" == "all" ]; then
49       [ -x "`which $PGSQLDUMPALL`" ] || \
50          fatal "Can't find $PGSQLDUMPALL."
51    else
52       [ -x "`which $PGSQLDUMP`" ] || \
53          fatal "Can't find $PGSQLDUMP."
54    fi
55 fi
56
57 # create backup dir, the vroot variable will be empty if no vsname was specified
58 # and will proceed to operate on the host
59 [ -d $vroot$backupdir ] || (debug "mkdir -p $vroot$backupdir"; mkdir -p $vroot$backupdir)
60 [ -d $vroot$backupdir ] || fatal "Backup directory '$vroot$backupdir' does not exist, and could not be created."
61
62 # give backup dir the good uid and permissions
63 # (in respect to the vserver, if $usevserver = yes)
64 if [ $usevserver = yes ]; then
65    pguid=`$VSERVER $vsname exec getent passwd $PGSQLUSER | @AWK@ -F: '{print $3}'`
66 else
67    pguid=`getent passwd $PGSQLUSER | @AWK@ -F: '{print $3}'`
68 fi
69 [ -n "$pguid" ] || \
70     fatal "No user called $PGSQLUSER`[ $usevserver = no ] || echo \" on vserver $vsname\"`."
71 debug "chown $pguid $vroot$backupdir"
72 chown $pguid $vroot$backupdir
73 debug "chmod 700 $vroot$backupdir"
74 chmod 700 $vroot$backupdir
75
76 # if $databases = all, use pg_dumpall
77 if [ "$databases" == "all" ]; then
78     if [ $usevserver = yes ]; then
79        if [ "$compress" == "yes" ]; then          
80           execstr="$VSERVER $vsname exec su - $PGSQLUSER -c \"$PGSQLDUMPALL | $GZIP > $backupdir/${vsname}.sql.gz\""
81        else
82           execstr="$VSERVER $vsname exec su - $PGSQLUSER -c \"$PGSQLDUMPALL > $backupdir/${vsname}.sql\""
83        fi
84     else
85        if [ "$compress" == "yes" ]; then
86           execstr="su - $PGSQLUSER -c \"$PGSQLDUMPALL | $GZIP > $backupdir/${localhost}-all.sql.gz\""
87        else
88         execstr="su - $PGSQLUSER -c \"$PGSQLDUMPALL > $backupdir/${localhost}-all.sql\""
89        fi
90     fi
91     debug "$execstr"
92     if [ ! $test ]; then
93        output=`eval $execstr 2>&1`
94        code=$?
95         if [ "$code" == "0" ]; then
96             debug $output
97             info "Successfully finished dump of pgsql cluster"
98         else
99             warning $output
100             warning "Failed to dump pgsql cluster"
101         fi
102     fi
103     
104 # else use pg_dump on each specified database
105 else
106     for db in $databases; do
107         if [ $usevserver = yes ]; then
108            if [ "$compress" == "yes" ]; then
109               execstr="$VSERVER $vsname exec su - $PGSQLUSER -c \"$PGSQLDUMP $db | $GZIP > $backupdir/${db}.sql.gz\""
110            else
111               execstr="$VSERVER $vsname exec su - $PGSQLUSER -c \"$PGSQLDUMP $db | > $backupdir/${db}.sql\""
112            fi
113         else
114            if [ "$compress" == "yes" ]; then
115               execstr="su - $PGSQLUSER -c \"$PGSQLDUMP $db | $GZIP > $backupdir/${db}.sql.gz\""
116            else
117               execstr="su - $PGSQLUSER -c \"$PGSQLDUMP $db > $backupdir/${db}.sql\""
118            fi
119         fi
120         debug "$execstr"
121         if [ ! $test ]; then
122             output=`eval $execstr 2>&1`
123             code=$?
124             if [ "$code" == "0" ]; then
125                 debug $output
126                 info "Successfully finished dump of pgsql database ${db}"
127             else
128                 warning $output
129                 warning "Failed to dump pgsql database ${db}"
130             fi
131         fi
132     done
133 fi
134
135 return 0
136