1 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
3 # mysql handler script for backupninja
6 getconf backupdir /var/backups/mysql
9 getconf dbhost localhost
19 getconf configfile /etc/mysql/debian.cnf
22 # Decide if the handler should operate on a vserver or on the host.
23 # In the former case, check that $vsname exists and is running.
26 if [ $vservers_are_available = yes ]; then
27 if [ -n "$vsname" ]; then
29 if ! vservers_exist "$vsname" ; then
30 fatal "The vserver given in vsname ($vsname) does not exist."
33 $VSERVERINFO -q $vsname RUNNING
35 fatal "The vserver $vsname is not running."
38 info "Using vserver '$vsname'."
40 vroot="$VROOTDIR/$vsname"
42 info "No vserver name specified, actions will be performed on the host."
46 ## Prepare ignore part of the command
47 ## This only works for mysqldump at the moment
51 ignore="$ignore --ignore-table=$i"
54 # create backup dirs, $vroot will be empty if no vsname was specified
55 # and we will instead proceed to operate on the host
56 [ -d $vroot$backupdir ] || mkdir -p $vroot$backupdir
57 [ -d $vroot$backupdir ] || fatal "Backup directory '$vroot$backupdir'"
58 hotdir="$backupdir/hotcopy"
59 dumpdir="$backupdir/sqldump"
61 if [ $usevserver = yes ]
63 [ "$sqldump" == "no" -o -d $vroot$dumpdir ] || $VSERVER $vsname exec mkdir -p $dumpdir
64 [ "$hotcopy" == "no" -o -d $vroot$hotdir ] || $VSERVER $vsname exec mkdir -p $hotdir
66 [ "$sqldump" == "no" -o -d $dumpdir ] || mkdir -p $dumpdir
67 [ "$hotcopy" == "no" -o -d $hotdir ] || mkdir -p $hotdir
70 #######################################################################
74 # one of three authentication methods:
75 # 1. setting the user, so that /home/user/.my.cnf is used.
76 # 2. specifying the user and password in the handler config,
77 # which generates a temporary .my.cnf in /root/.my.cnf
78 # 3. specify the config file with --defaults-file
79 # (this option DOESN'T WORK WITH MYSQLHOTCOPY)
83 # only if dbusername and dbpassword specified.
84 # we create a tmp file because we don't want to
85 # specify the password on the command line.
89 if [ "$dbusername" != "" -a "$dbpassword" != "" ]
91 if [ $usevserver = yes ]
93 vhome=`$VSERVER $vsname exec getent passwd "root" | awk -F: '{print $6}'`
96 home=`getent passwd "root" | awk -F: '{print $6}'`
99 [ -d $home ] || fatal "Can't find root's home directory ($home)."
101 mycnf="$home/.my.cnf"
106 tmpcnf="$home/my.cnf.disable"
107 debug "mv $mycnf $tmpcnf"
114 # auto generated backupninja mysql conf
118 password="$dbpassword"
123 password="$dbpassword"
128 password="$dbpassword"
131 if [ $usevserver = yes ]
133 defaultsfile="--defaults-file=$vhome/.my.cnf"
135 defaultsfile="--defaults-file=$mycnf"
139 # if a user is not set, use $configfile, otherwise use $mycnf
140 if [ "$user" == "" ]; then
142 defaultsfile="--defaults-file=$configfile"
145 if [ $usevserver = yes ]
147 vuserhome=`$VSERVER $vsname exec getent passwd "$user" | awk -F: '{print $6}'`
150 fatal "User $user not found in /etc/passwd"
152 userhome="$vroot$vuserhome"
154 userhome=`getent passwd "$user" | awk -F: '{print $6}'`
157 fatal "User $user not found in /etc/passwd"
161 debug "User home set to: $userhome"
162 [ -f $userhome/.my.cnf ] || fatal "Can't find config file in $userhome/.my.cnf"
163 defaultsfile="--defaults-file=$userhome/.my.cnf"
164 debug "using $defaultsfile"
167 #######################################################################
170 if [ "$hotcopy" == "yes" ]
172 info "Initializing hotcopy method"
173 if [ "$databases" == "all" ]
175 if [ $usevserver = yes ]
177 info "dbhost: $dbhost"
178 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY -h $dbhost --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
180 execstr="$MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
182 debug "su $user -c \"$execstr\""
185 output=`su $user -c "$execstr" 2>&1`
187 if [ "$code" == "0" ]
190 info "Successfully finished hotcopy of all mysql databases"
193 warning "Failed to hotcopy all mysql databases"
199 if [ $usevserver = yes ]
201 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --allowold $db $hotdir"
203 execstr="$MYSQLHOTCOPY --allowold $db $hotdir"
205 debug 'su $user -c \"$execstr\"'
208 output=`su $user -c "$execstr" 2>&1`
210 if [ "$code" == "0" ]
213 info "Successfully finished hotcopy of mysql database $db"
216 warning "Failed to hotcopy mysql database $db"
223 ##########################################################################
226 if [ "$sqldump" == "yes" ]
228 info "Initializing SQL dump method"
229 if [ "$databases" == "all" ]
231 if [ $usevserver = yes ]
233 debug 'echo show databases | $VSERVER $vsname exec su $user -c \"$MYSQL $defaultsfile\" | grep -v Database'
234 databases=`echo 'show databases' | $VSERVER $vsname exec su $user -c "$MYSQL $defaultsfile" | grep -v Database`
237 fatal "Authentication problem, maybe user/password is wrong"
240 databases=`echo 'show databases' | su $user -c "$MYSQL $defaultsfile" | grep -v Database`
243 fatal "Authentication problem, maybe user/password is wrong"
250 if [ $usevserver = yes ]
252 if [ "$compress" == "yes" ]; then
253 execstr="$VSERVER $vsname exec $MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db | $GZIP > $vroot$dumpdir/${db}.sql.gz"
255 execstr="$VSERVER $vsname exec $MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db > $vroot$dumpdir/${db}.sql"
258 if [ "$compress" == "yes" ]; then
259 execstr="$MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db | $GZIP > $dumpdir/${db}.sql.gz"
261 execstr="$MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db > $dumpdir/${db}.sql"
264 debug "su $user -c \"$execstr\""
267 output=`su $user -c "$execstr" 2>&1`
269 if [ "$code" == "0" ]
272 info "Successfully finished dump of mysql database $db"
275 warning "Failed to dump mysql databases $db"
280 # clean up tmp config file
281 if [ "$dbusername" != "" -a "$dbpassword" != "" ]
283 ## clean up tmp config file
288 debug "mv $tmpcnf $mycnf"