handlers/pgsql: make more use of the new lib/vserver functionality
[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 # If vservers are enabled, check that $vsname exists, and decide if the
14 # handler should use it or if it should just operate on the host.
15 local usevserver=no
16 if [ $vservers_are_available = yes ]; then
17    if ! vservers_exist "$vsname" ; then
18       fatal "The vserver given in vsname ($vsname) does not exist."
19    else
20       info "using vserver '$vsname'"
21       usevserver=yes
22    fi
23 fi
24
25 # As needed, make sure that :
26 #   * the specified vserver is running
27 #   * the specified vserver or host has the needed executables
28 if [ $usevserver = yes ]; then
29     info "examining vserver '$vsname'"
30     vroot="$VROOTDIR/$vsname"
31     # is it running ?
32     running=`$VSERVERINFO $vsname RUNNING`
33     if [ "$running" = "1" ]; then
34         if [ "$databases" == "all" ]; then
35             [ -x "$vroot`$VSERVER $vsname exec which $PGSQLDUMPALL`" ] || \
36                 fatal "Can't find $PGSQLDUMPALL in vserver $vsname."
37         else
38             [ -x "$vroot`$VSERVER $vsname exec which $PGSQLDUMP`" ] || \
39                 fatal "Can't find $PGSQLDUMP in vserver $vsname."
40         fi
41     else
42         fatal "vserver $vsname is not running."
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'"
58
59 # give backup dir the good uid and permissions
60 # (in respect to the vserver, if $usevserver = yes)
61 pguid=`getent passwd postgres | awk -F: '{print $3}'`
62 [ -n "$pguid" ] || \
63     fatal "No user called postgres`[ $usevserver = no ] || echo \" on vserver $vsname\"`."
64 debug "chown $pguid $vroot$backupdir"
65 chown $pguid $vroot$backupdir
66 debug "chmod 700 $vroot$backupdir"
67 chmod 700 $vroot$backupdir
68
69 # if $databases = all, use pg_dumpall
70 if [ "$databases" == "all" ]; then
71     if [ $usevserver = yes ]; then
72         execstr="$VSERVER $vsname exec su - postgres -c $PGSQLDUMPALL > $backupdir/${vsname}.sql"
73     else
74         execstr="su - postgres -c $PGSQLDUMPALL > $backupdir/${localhost}-all.sql"
75     fi
76     debug "$execstr"
77     if [ ! $test ]; then
78        output=`$execstr 2>&1`
79        code=$?
80         if [ "$code" == "0" ]; then
81             debug $output
82             info "Successfully finished dump of pgsql cluster"
83         else
84             warning $output
85             warning "Failed to dump pgsql cluster"
86         fi
87     fi
88     
89 # else use pg_dump on each specified database
90 else
91     for db in $databases; do
92         if [ $usevserver = yes ]
93             then
94             execstr="$VSERVER $vsname exec su - postgres -c $PGSQLDUMP $db > $backupdir/${db}.sql"
95         else
96             execstr="su - postgres -c $PGSQLDUMP $db > $backupdir/${db}.sql"
97         fi
98         debug "$execstr"
99         if [ ! $test ]; then
100             output=`$execstr 2>&1`
101             code=$?
102             if [ "$code" == "0" ]; then
103                 debug $output
104                 info "Successfully finished dump of pgsql database ${db}"
105             else
106                 warning $output
107                 warning "Failed to dump pgsql database ${db}"
108             fi
109         fi
110     done
111 fi
112
113 if [ "$compress" == "yes" ]; then
114     output=`$GZIP -f $vroot$backupdir/*.sql 2>&1`
115     debug $output
116 fi
117
118 return 0
119