63062f7084175454940c4eff9be561f73d991a54
[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 configured, decide if the handler should
14 # use them 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 "At least one of the vservers listed in vsnames ($vsnames) 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 exists and is running
27 #   * the specified vserver or host has the needed executables
28 if [ $usevserver = yes ]; then
29     info "examining vserver '$vsname'"
30     # does it exist ?
31     vroot="$VROOTDIR/$vsname"
32     [ -d $vroot ] || fatal "vserver '$vsname' does not exist at '$vroot'"
33     # is it running ?
34     running=`$VSERVERINFO $vsname RUNNING`
35     if [ "$running" = "1" ]; then
36         if [ "$databases" == "all" ]; then
37             [ -x "$vroot`$VSERVER $vsname exec which $PGSQLDUMPALL`" ] || \
38                 fatal "Can't find $PGSQLDUMPALL in vserver $vsname."
39         else
40             [ -x "$vroot`$VSERVER $vsname exec which $PGSQLDUMP`" ] || \
41                 fatal "Can't find $PGSQLDUMP in vserver $vsname."
42         fi
43     else
44         fatal "vserver $vsname is not running."
45     fi
46 else
47     if [ "$databases" == "all" ]; then
48         [ -x "`which $PGSQLDUMPALL`" ] || \
49             fatal "Can't find $PGSQLDUMPALL."
50     else
51         [ -x "`which $PGSQLDUMP`" ] || \
52             fatal "Can't find $PGSQLDUMP."
53     fi
54 fi
55
56 # create backup dir, the vroot variable will be empty if no vsname was specified
57 # and will proceed to operate on the host
58 [ -d $vroot$backupdir ] || (debug "mkdir -p $vroot$backupdir"; mkdir -p $vroot$backupdir)
59 [ -d $vroot$backupdir ] || fatal "Backup directory '$vroot$backupdir'"
60
61 # give backup dir the good uid and permissions
62 # (in respect to the vserver, if $usevserver = yes)
63 pguid=`getent passwd postgres | awk -F: '{print $3}'`
64 [ -n "$pguid" ] || \
65     fatal "No user called postgres`[ $usevserver = no ] || echo \" on vserver $vsname\"`."
66 debug "chown $pguid $vroot$backupdir"
67 chown $pguid $vroot$backupdir
68 debug "chmod 700 $vroot$backupdir"
69 chmod 700 $vroot$backupdir
70
71 # if $databases = all, use pg_dumpall
72 if [ "$databases" == "all" ]; then
73     if [ $usevserver = yes ]; then
74         execstr="$VSERVER $vsname exec su - postgres -c $PGSQLDUMPALL > $backupdir/${vsname}.sql"
75     else
76         execstr="su - postgres -c $PGSQLDUMPALL > $backupdir/${localhost}-all.sql"
77     fi
78     debug "$execstr"
79     if [ ! $test ]; then
80        output=`$execstr 2>&1`
81        code=$?
82         if [ "$code" == "0" ]; then
83             debug $output
84             info "Successfully finished dump of pgsql cluster"
85         else
86             warning $output
87             warning "Failed to dump pgsql cluster"
88         fi
89     fi
90     
91 # else use pg_dump on each specified database
92 else
93     for db in $databases; do
94         if [ $usevserver = yes ]
95             then
96             execstr="$VSERVER $vsname exec su - postgres -c $PGSQLDUMP $db > $backupdir/${db}.sql"
97         else
98             execstr="su - postgres -c $PGSQLDUMP $db > $backupdir/${db}.sql"
99         fi
100         debug "$execstr"
101         if [ ! $test ]; then
102             output=`$execstr 2>&1`
103             code=$?
104             if [ "$code" == "0" ]; then
105                 debug $output
106                 info "Successfully finished dump of pgsql database ${db}"
107             else
108                 warning $output
109                 warning "Failed to dump pgsql database ${db}"
110             fi
111         fi
112     done
113 fi
114
115 if [ "$compress" == "yes" ]; then
116     output=`$GZIP -f $vroot$backupdir/*.sql 2>&1`
117     debug $output
118 fi
119
120 return 0
121