f334bf2fffedbee12148b2a29e55c75f6b62d189
[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       vservers_running $vsname || fatal "The vserver $vsname is not running."
25       # everything ok
26       info "Using vserver '$vsname'."
27       usevserver=yes
28       vroot="$VROOTDIR/$vsname"
29    else
30       info "No vserver name specified, actions will be performed on the host."
31    fi
32 fi
33
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."
40    else
41       [ -x "$vroot`$VSERVER $vsname exec which $PGSQLDUMP`" ] || \
42          fatal "Can't find $PGSQLDUMP in vserver $vsname."
43    fi
44 else
45    if [ "$databases" == "all" ]; then
46       [ -x "`which $PGSQLDUMPALL`" ] || \
47          fatal "Can't find $PGSQLDUMPALL."
48    else
49       [ -x "`which $PGSQLDUMP`" ] || \
50          fatal "Can't find $PGSQLDUMP."
51    fi
52 fi
53
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."
58
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}'`
63 else
64    pguid=`getent passwd $PGSQLUSER | @AWK@ -F: '{print $3}'`
65 fi
66 [ -n "$pguid" ] || \
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
72
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\""
78        else
79           execstr="$VSERVER $vsname exec su - $PGSQLUSER -c \"$PGSQLDUMPALL > $backupdir/${vsname}.sql\""
80        fi
81     else
82        if [ "$compress" == "yes" ]; then
83           execstr="su - $PGSQLUSER -c \"$PGSQLDUMPALL | $GZIP > $backupdir/${localhost}-all.sql.gz\""
84        else
85         execstr="su - $PGSQLUSER -c \"$PGSQLDUMPALL > $backupdir/${localhost}-all.sql\""
86        fi
87     fi
88     debug "$execstr"
89     if [ ! $test ]; then
90        output=`eval $execstr 2>&1`
91        code=$?
92         if [ "$code" == "0" ]; then
93             debug $output
94             info "Successfully finished dump of pgsql cluster"
95         else
96             warning $output
97             warning "Failed to dump pgsql cluster"
98         fi
99     fi
100     
101 # else use pg_dump on each specified database
102 else
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\""
107            else
108               execstr="$VSERVER $vsname exec su - $PGSQLUSER -c \"$PGSQLDUMP $db | > $backupdir/${db}.sql\""
109            fi
110         else
111            if [ "$compress" == "yes" ]; then
112               execstr="su - $PGSQLUSER -c \"$PGSQLDUMP $db | $GZIP > $backupdir/${db}.sql.gz\""
113            else
114               execstr="su - $PGSQLUSER -c \"$PGSQLDUMP $db > $backupdir/${db}.sql\""
115            fi
116         fi
117         debug "$execstr"
118         if [ ! $test ]; then
119             output=`eval $execstr 2>&1`
120             code=$?
121             if [ "$code" == "0" ]; then
122                 debug $output
123                 info "Successfully finished dump of pgsql database ${db}"
124             else
125                 warning $output
126                 warning "Failed to dump pgsql database ${db}"
127             fi
128         fi
129     done
130 fi
131
132 return 0
133