r3555@krups: intrigeri | 2005-11-15 13:59:57 +0100
[matthijs/upstream/backupninja.git] / handlers / mysql
1 #
2 # mysql handler script for backupninja
3 #
4
5 getconf backupdir /var/backups/mysql
6 getconf databases all
7 getconf ignores
8 getconf dbhost localhost
9 getconf hotcopy no
10 getconf sqldump no
11 getconf compress yes
12 getconf vsname
13
14 # authentication:
15 getconf user
16 getconf dbusername
17 getconf dbpassword
18 getconf configfile /etc/mysql/debian.cnf
19
20 if [ "$user" == "" ]; then
21         userset=false;
22         user=root;
23 else
24         userset=true;
25         userhome=`getent passwd "$user" | awk -F: '{print $6}'`
26         [ -f $userhome/.my.cnf ] || fatal "Can't find config file in $userhome/.my.cnf"
27 fi
28
29 ## Prepare ignore part of the command
30 ## This only works for mysqldump at the moment
31
32 ignore=''
33 for i in $ignores; do
34        ignore="$ignore --ignore-table=$i"
35 done
36
37 # If vservers are configured, decide if the handler should
38 # use them or if it should just operate on the host
39
40 if [ "$vservers" = "yes" ]
41 then
42         if [ ! -z $vsname ]
43         then            
44                 info "Using vserver '$vsname'"
45                 usevserver=1
46         else
47                 info "No vserver name specified, actions will be performed on the host"
48         fi
49 fi
50
51 # If needed, make sure that the specified vserver exists and is running.
52 if [ $usevserver ]
53 then
54         info "examining vserver '$vsname'"
55         # does it exist ?
56         vroot="$VROOTDIR/$vsname"
57         [ -d $vroot ] || fatal "vserver '$vsname' does not exist at '$vroot'"
58         # is it running ?
59         running=`$VSERVERINFO $vsname RUNNING`
60         [ $running = 1 ] || fatal "vserver $vsname is not running."
61 fi
62         
63 # create backup dirs, the vroot variable will be empty if no vsname was specified
64 # and will proceed to operate on the host
65 [ -d $vroot$backupdir ] || mkdir -p $vroot$backupdir
66 [ -d $vroot$backupdir ] || fatal "Backup directory '$vroot$backupdir'"
67 hotdir="$backupdir/hotcopy"
68 dumpdir="$backupdir/sqldump"
69
70 if [ $usevserver ]
71 then
72         [ "$sqldump" == "no" -o -d $vroot$dumpdir ] || $VSERVER $vsname exec mkdir -p $dumpdir
73         [ "$hotcopy" == "no" -o -d $vroot$hotdir ] || $VSERVER $vsname exec mkdir -p $hotdir
74 else
75         [ "$sqldump" == "no" -o -d $dumpdir ] || mkdir -p $dumpdir
76         [ "$hotcopy" == "no" -o -d $hotdir ] || mkdir -p $hotdir
77 fi
78
79 #######################################################################
80 ## AUTHENTICATION
81
82 #
83 # one of three authentication methods:
84 # 1. setting the user, so that /home/user/.my.cnf is used.
85 # 2. specifying the user and password in the handler config,
86 #    which generates a temporary .my.cnf in /root/.my.cnf
87 # 3. specify the config file with --defaults-file
88 #    (this option DOESN'T WORK WITH MYSQLHOTCOPY)
89 #
90
91 # create .my.cnf
92 # only if dbusername and dbpassword specified.
93 # we create a tmp file because we don't want to
94 # specify the password on the command line.
95
96 defaultsfile=""
97 if [ "$dbusername" != "" -a "$dbpassword" != "" ]; then
98         home=`getent passwd "root" | awk -F: '{print $6}'`
99         [ -d $home ] || fatal "Can't find root's home directory ($home)."
100         mycnf="$home/.my.cnf"
101         if [ -f $mycnf ]; then
102                 # rename temporarily
103                 tmpcnf="$home/my.cnf.disable"
104                 debug "mv $mycnf $tmpcnf"
105                 mv $mycnf $tmpcnf
106         fi
107         oldmask=`umask`
108         umask 077
109         cat > $mycnf <<EOF
110 # auto generated backupninja mysql conf
111 [mysql]
112 user=$dbusername
113 password="$dbpassword"
114
115 [mysqldump]
116 user=$dbusername
117 password="$dbpassword"
118
119 [mysqlhotcopy]
120 user=$dbusername
121 password="$dbpassword"
122 EOF
123         umask $oldmask
124         defaultsfile="--defaults-file=$mycnf"
125 elif [ "$userset" == "false" ]; then
126         # if user is set, don't use $configfile
127         defaultsfile="--defaults-file=$configfile"
128 fi
129
130 #######################################################################
131 ## HOT COPY
132
133 if [ "$hotcopy" == "yes" ]; then 
134         if [ "$databases" == "all" ]; then
135                 if [ $usevserver ]
136                 then
137                         execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
138                 else
139                         execstr="$MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
140                 fi
141                 debug "su $user -c '$execstr'"
142                 if [ ! $test ]; then
143                         output=`su $user -c "$execstr" 2>&1`
144                         code=$?
145                         if [ "$code" == "0" ]; then
146                                 debug $output
147                                 info "Successfully finished hotcopy of all mysql databases"
148                         else
149                                 warning $output
150                                 warning "Failed to hotcopy all mysql databases"
151                         fi
152                 fi
153         else    
154                 for db in $databases; do
155                         if [ $usevserver ]
156                         then
157                                 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --allowold $db $hotdir"
158                         else
159                                 execstr="$MYSQLHOTCOPY --allowold $db $hotdir"
160                         fi
161                         debug "su $user -c '$execstr'"
162                         if [ ! $test ]; then
163                                 output=`su $user -c "$execstr" 2>&1`
164                                 code=$?
165                                 if [ "$code" == "0" ]; then
166                                         debug $output
167                                         info "Successfully finished hotcopy of mysql database $db"
168                                 else
169                                         warning $output
170                                         warning "Failed to hotcopy mysql database $db"
171                                 fi
172                         fi
173                 done
174         fi
175 fi
176
177 ##########################################################################
178 ## SQL DUMP
179
180 if [ "$sqldump" == "yes" ]; then
181         if [ "$databases" == "all" ]; then
182                 if [ $usevserver ]
183                 then
184                         databases=`echo 'show databases' | $VSERVER $vsname exec su $user -c "$MYSQL $defaultsfile" | grep -v Database`
185                 else
186                         databases=`echo 'show databases' | su $user -c "$MYSQL $defaultsfile" | grep -v Database`
187                 fi
188         fi
189
190         for db in $databases; do
191                 if [ $usevserver ]
192                 then
193                         execstr="$VSERVER $vsname exec $MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db > $vroot$dumpdir/${db}.sql"
194                 else
195                         execstr="$MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db > $dumpdir/${db}.sql"
196                 fi
197                 debug "su $user -c '$execstr'"
198                 if [ ! $test ]; then
199                         output=`su $user -c "$execstr" 2>&1`
200                         code=$?
201                         if [ "$code" == "0" ]; then
202                                 debug $output
203                                 info "Successfully finished dump of mysql database $db"
204                         else
205                                 warning $output
206                                 warning "Failed to dump mysql databases $db"
207                         fi
208                 fi
209         done
210         
211         if [ "$compress" == "yes" ]; then
212                 output=`$GZIP -f $vroot$dumpdir/*.sql 2>&1`
213                 debug $output
214         fi
215 fi
216
217 # clean up tmp config file
218 if [ "$dbusername" != "" ]; then
219         ## clean up tmp config file
220         debug "rm $mycnf"
221         rm $mycnf
222         if [ -f "$tmpcnf" ]; then
223                 debug "mv $tmpcnf $mycnf"
224                 mv $tmpcnf $mycnf
225         fi
226 fi
227
228 return 0