dd1a2ea90b16b3a4d827de0feee26b500ef6ad3d
[matthijs/upstream/backupninja.git] / handlers / pgsql
1 #
2 # PostgreSQL handler script for backupninja
3 #
4
5 getconf backupdir /var/backups/postgres
6 getconf databases all
7 getconf compress yes
8 getconf vsname
9
10 localhost=`hostname`
11
12 # If vservers are configured, decide if the handler should
13 # use them or if it should just operate on the host
14 if [ "$vservers" == "yes" ]; then
15     if [ ! -z $vsname ]; then           
16         info "using vserver '$vsname'"
17         usevserver=1
18     else
19         info "no vserver name specified, actions will be performed on the host"
20     fi
21 fi
22
23 # As needed, make sure that :
24 #   * the specified vserver exists and is running
25 #   * the specified vserver or host has the needed executables
26 if [ $usevserver ]; then
27     info "examining vserver '$vsname'"
28     # does it exist ?
29     vroot="$VROOTDIR/$vsname"
30     [ -d $vroot ] || fatal "vserver '$vsname' does not exist at '$vroot'"
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)
61 pguid=`getent passwd postgres | awk -F: '{print $3}'`
62 [ -n "$pguid" ] || \
63     fatal "No user called postgres`[ $usevserver != 1 ] || 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 ]; 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 ]
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