2 # mysql handler script for backupninja
5 getconf backupdir /var/backups/mysql
8 getconf dbhost localhost
18 getconf configfile /etc/mysql/debian.cnf
21 # If vservers are configured, decide if the handler should
22 # use them or if it should just operate on the host
24 if [ "$vservers" = "yes" ]
28 info "Using vserver '$vsname'"
31 info "No vserver name specified, actions will be performed on the host"
35 # If needed, make sure that the specified vserver exists and is running.
38 info "Examining vserver '$vsname'"
40 vroot="$VROOTDIR/$vsname"
41 [ -d $vroot ] || fatal "vserver '$vsname' does not exist at '$vroot'"
43 $VSERVERINFO -q $vsname RUNNING
46 fatal "vserver $vsname is not running."
50 if [ "$user" == "" ]; then
57 userhome=`$VSERVER $vsname exec getent passwd "$user" | awk -F: '{print $6}'`
60 fatal "User $user not found in /etc/passwd"
62 userhome="$vroot$userhome"
63 info "User home set to: $userhome"
64 [ -f $userhome/.my.cnf ] || fatal "Can't find config file in $userhome/.my.cnf"
66 userhome=`getent passwd "$user" | awk -F: '{print $6}'`
69 fatal "User $user not found in /etc/passwd"
71 info "User home set to: $userhome"
72 [ -f $userhome/.my.cnf ] || fatal "Can't find config file in $userhome/.my.cnf"
76 ## Prepare ignore part of the command
77 ## This only works for mysqldump at the moment
81 ignore="$ignore --ignore-table=$i"
84 # create backup dirs, $vroot will be empty if no vsname was specified
85 # and we will instead proceed to operate on the host
86 [ -d $vroot$backupdir ] || mkdir -p $vroot$backupdir
87 [ -d $vroot$backupdir ] || fatal "Backup directory '$vroot$backupdir'"
88 hotdir="$backupdir/hotcopy"
89 dumpdir="$backupdir/sqldump"
93 [ "$sqldump" == "no" -o -d $vroot$dumpdir ] || $VSERVER $vsname exec mkdir -p $dumpdir
94 [ "$hotcopy" == "no" -o -d $vroot$hotdir ] || $VSERVER $vsname exec mkdir -p $hotdir
96 [ "$sqldump" == "no" -o -d $dumpdir ] || mkdir -p $dumpdir
97 [ "$hotcopy" == "no" -o -d $hotdir ] || mkdir -p $hotdir
100 #######################################################################
104 # one of three authentication methods:
105 # 1. setting the user, so that /home/user/.my.cnf is used.
106 # 2. specifying the user and password in the handler config,
107 # which generates a temporary .my.cnf in /root/.my.cnf
108 # 3. specify the config file with --defaults-file
109 # (this option DOESN'T WORK WITH MYSQLHOTCOPY)
113 # only if dbusername and dbpassword specified.
114 # we create a tmp file because we don't want to
115 # specify the password on the command line.
118 if [ "$dbusername" != "" -a "$dbpassword" != "" ]
122 home=`$VSERVER $vsname exec getent passwd "root" | awk -F: '{print $6}'`
124 home=`getent passwd "root" | awk -F: '{print $6}'`
126 [ -d $home ] || fatal "Can't find root's home directory ($home)."
127 mycnf="$home/.my.cnf"
131 tmpcnf="$home/my.cnf.disable"
132 debug "mv $mycnf $tmpcnf"
138 # auto generated backupninja mysql conf
141 password="$dbpassword"
145 password="$dbpassword"
149 password="$dbpassword"
152 defaultsfile="--defaults-file=$mycnf"
153 elif [ "$userset" == "false" ]; then
154 # if user is set, don't use $configfile
155 defaultsfile="--defaults-file=$configfile"
158 #######################################################################
161 if [ "$hotcopy" == "yes" ]; then
162 if [ "$databases" == "all" ]; then
165 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
167 execstr="$MYSQLHOTCOPY --quiet --allowold --regexp /.\*/./.\*/ $hotdir"
169 debug "su $user -c '$execstr'"
171 output=`su $user -c "$execstr" 2>&1`
173 if [ "$code" == "0" ]; then
175 info "Successfully finished hotcopy of all mysql databases"
178 warning "Failed to hotcopy all mysql databases"
182 for db in $databases; do
185 execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --allowold $db $hotdir"
187 execstr="$MYSQLHOTCOPY --allowold $db $hotdir"
189 debug "su $user -c '$execstr'"
191 output=`su $user -c "$execstr" 2>&1`
193 if [ "$code" == "0" ]; then
195 info "Successfully finished hotcopy of mysql database $db"
198 warning "Failed to hotcopy mysql database $db"
205 ##########################################################################
208 if [ "$sqldump" == "yes" ]; then
209 if [ "$databases" == "all" ]; then
212 databases=`echo 'show databases' | $VSERVER $vsname exec su $user -c "$MYSQL $defaultsfile" | grep -v Database`
215 fatal "Something unexpected happened, the defaults file may have gone missing or is corrupt"
218 databases=`echo 'show databases' | su $user -c "$MYSQL $defaultsfile" | grep -v Database`
221 fatal "Something unexpected happened, the defaults file may have gone missing or is corrupt"
226 for db in $databases; do
229 execstr="$VSERVER $vsname exec $MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db > $vroot$dumpdir/${db}.sql"
231 execstr="$MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db > $dumpdir/${db}.sql"
233 debug "su $user -c '$execstr'"
235 output=`su $user -c "$execstr" 2>&1`
237 if [ "$code" == "0" ]; then
239 info "Successfully finished dump of mysql database $db"
242 warning "Failed to dump mysql databases $db"
247 if [ "$compress" == "yes" ]; then
248 output=`$GZIP -f $vroot$dumpdir/*.sql 2>&1`
253 # clean up tmp config file
254 if [ "$dbusername" != "" ]; then
255 ## clean up tmp config file
258 if [ -f "$tmpcnf" ]; then
259 debug "mv $tmpcnf $mycnf"