Added pgsql (PostgreSQL) handler, with vservers support.
[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
15 if [ "$vservers" == "yes" ]
16     then
17     if [ ! -z $vsname ]
18         then            
19         info "Using vserver '$vsname'"
20         usevserver=1
21     else
22         info "No vserver name specified, actions will be performed on the host"
23     fi
24 fi
25
26 # Check to make sure that the specified vserver exists
27 if [ $usevserver ]
28     then
29     vroot="$VROOTDIR/$vsname"
30     [ -d $vroot ] || fatal "vserver '$vsname' does not exist at '$vroot'"
31 fi
32
33 # create backup dir, the vroot variable will be empty if no vsname was specified
34 # and will proceed to operate on the host
35 [ -d $vroot$backupdir ] || mkdir -p $vroot$backupdir
36 [ -d $vroot$backupdir ] || fatal "Backup directory '$vroot$backupdir'"
37
38 # give backup dir the good uid and permissions
39 # (in respect to the vserver, if $usevserver)
40 pguid=`grep '^postgres:' $vroot/etc/passwd | awk -F: '{print $3}'`
41 debug "chown $pguid $vroot$backupdir"
42 chown $pguid $vroot$backupdir
43 debug "chmod 700 $vroot$backupdir"
44 chmod 700 $vroot$backupdir
45
46 # if $databases = all, use pg_dumpall
47 if [ "$databases" == "all" ]; then
48     if [ $usevserver ]; then
49         execstr="$VSERVER $vsname exec su - postgres -c $PGSQLDUMPALL > $vroot$backupdir/${vsname}.sql"
50     else
51         execstr="su - postgres -c $PGSQLDUMPALL > $backupdir/${localhost}-all.sql"
52     fi
53     debug "$execstr"
54     if [ ! $test ]; then
55         output=`$execstr 2>&1`
56         code=$?
57         if [ "$code" == "0" ]; then
58             debug $output
59             info "Successfully finished dump of pgsql cluster"
60         else
61             warning $output
62             warning "Failed to dump pgsql cluster"
63         fi
64     fi
65     
66 # else use pg_dump on each specified database
67 else
68     for db in $databases; do
69         if [ $usevserver ]
70             then
71             execstr="$VSERVER $vsname exec su - postgres -c $PGSQLDUMP $db > $vroot$backupdir/${db}.sql"
72         else
73             execstr="su - postgres -c $PGSQLDUMP $db > $backupdir/${db}.sql"
74         fi
75         debug "$execstr"
76         if [ ! $test ]; then
77             output=`$execstr 2>&1`
78             code=$?
79             if [ "$code" == "0" ]; then
80                 debug $output
81                 info "Successfully finished dump of pgsql database ${db}"
82             else
83                 warning $output
84                 warning "Failed to dump pgsql database ${db}"
85             fi
86         fi
87     done
88 fi
89
90 if [ "$compress" == "yes" ]; then
91     output=`$GZIP -f $vroot$backupdir/*.sql 2>&1`
92     debug $output
93 fi
94
95 return 0
96