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