clean up and homogenize pgsql and mysql vserver checks
[matthijs/upstream/backupninja.git] / handlers / pgsql
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 [ -z "$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'"
61
62 # give backup dir the good uid and permissions
63 # (in respect to the vserver, if $usevserver = yes)
64 pguid=`getent passwd postgres | awk -F: '{print $3}'`
65 [ -n "$pguid" ] || \
66     fatal "No user called postgres`[ $usevserver = no ] || echo \" on vserver $vsname\"`."
67 debug "chown $pguid $vroot$backupdir"
68 chown $pguid $vroot$backupdir
69 debug "chmod 700 $vroot$backupdir"
70 chmod 700 $vroot$backupdir
71
72 # if $databases = all, use pg_dumpall
73 if [ "$databases" == "all" ]; then
74     if [ $usevserver = yes ]; then
75         execstr="$VSERVER $vsname exec su - postgres -c $PGSQLDUMPALL > $backupdir/${vsname}.sql"
76     else
77         execstr="su - postgres -c $PGSQLDUMPALL > $backupdir/${localhost}-all.sql"
78     fi
79     debug "$execstr"
80     if [ ! $test ]; then
81        output=`$execstr 2>&1`
82        code=$?
83         if [ "$code" == "0" ]; then
84             debug $output
85             info "Successfully finished dump of pgsql cluster"
86         else
87             warning $output
88             warning "Failed to dump pgsql cluster"
89         fi
90     fi
91     
92 # else use pg_dump on each specified database
93 else
94     for db in $databases; do
95         if [ $usevserver = yes ]
96             then
97             execstr="$VSERVER $vsname exec su - postgres -c $PGSQLDUMP $db > $backupdir/${db}.sql"
98         else
99             execstr="su - postgres -c $PGSQLDUMP $db > $backupdir/${db}.sql"
100         fi
101         debug "$execstr"
102         if [ ! $test ]; then
103             output=`$execstr 2>&1`
104             code=$?
105             if [ "$code" == "0" ]; then
106                 debug $output
107                 info "Successfully finished dump of pgsql database ${db}"
108             else
109                 warning $output
110                 warning "Failed to dump pgsql database ${db}"
111             fi
112         fi
113     done
114 fi
115
116 if [ "$compress" == "yes" ]; then
117     output=`$GZIP -f $vroot$backupdir/*.sql 2>&1`
118     debug $output
119 fi
120
121 return 0
122