From 4215d06b05dbfef0adf053f67c7839b6ae343810 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Thu, 1 Jan 2009 21:08:09 +0100 Subject: [PATCH] mysql: Clean up vserver handling a bit. Use the same trick the ldap handler uses: Have two vars $vexec and $vdir, which are empty when running on the host and filled when running on a vserver. By prepending these vars at the right places, all of the explicit "if [ $use_vserver = yes ]" could be removed. --- handlers/mysql.in | 126 ++++++++++++++-------------------------------- 1 file changed, 39 insertions(+), 87 deletions(-) diff --git a/handlers/mysql.in b/handlers/mysql.in index 7ff194e..f79a0b2 100644 --- a/handlers/mysql.in +++ b/handlers/mysql.in @@ -36,7 +36,6 @@ if [ $vservers_are_available = yes ]; then # everything ok info "Using vserver '$vsname'." usevserver=yes - vroot="$VROOTDIR/$vsname" else info "No vserver name specified, actions will be performed on the host." fi @@ -51,7 +50,15 @@ ignore='' for i in $ignores $nodata; do ignore="$ignore --ignore-table=$i" done - + +if [ $usevserver = yes ]; then + vroot="$VROOTDIR/$vsname" + vexec="$VSERVER $vsname exec" +else + vroot="" + vexec="" +fi + # create backup dirs, $vroot will be empty if no vsname was specified # and we will instead proceed to operate on the host [ -d $vroot$backupdir ] || mkdir -p $vroot$backupdir @@ -59,14 +66,8 @@ done hotdir="$backupdir/hotcopy" dumpdir="$backupdir/sqldump" -if [ $usevserver = yes ] -then - [ "$sqldump" == "no" -o -d $vroot$dumpdir ] || $VSERVER $vsname exec mkdir -p $dumpdir - [ "$hotcopy" == "no" -o -d $vroot$hotdir ] || $VSERVER $vsname exec mkdir -p $hotdir -else - [ "$sqldump" == "no" -o -d $dumpdir ] || mkdir -p $dumpdir - [ "$hotcopy" == "no" -o -d $hotdir ] || mkdir -p $hotdir -fi +[ "$sqldump" == "no" -o -d $vroot$dumpdir ] || $vexec mkdir -p $vroot$dumpdir +[ "$hotcopy" == "no" -o -d $vroot$hotdir ] || $vexec mkdir -p $vroot$hotdir ####################################################################### ## AUTHENTICATION @@ -89,13 +90,8 @@ defaultsfile="" if [ "$dbusername" != "" -a "$dbpassword" != "" ] then - if [ $usevserver = yes ] - then - vhome=`$VSERVER $vsname exec getent passwd "root" | @AWK@ -F: '{print $6}'` - home="$vroot$vhome" - else - home=`getent passwd "root" | @AWK@ -F: '{print $6}'` - fi + vhome=`$vexec getent passwd "root" | @AWK@ -F: '{print $6}'` + home=$vroot$vhome [ -d $home ] || fatal "Can't find root's home directory ($home)." @@ -129,12 +125,7 @@ user=$dbusername password="$dbpassword" EOF umask $oldmask - if [ $usevserver = yes ] - then - defaultsfile="--defaults-extra-file=$vhome/.my.cnf" - else - defaultsfile="--defaults-extra-file=$mycnf" - fi + defaultsfile="--defaults-extra-file=$vhome/.my.cnf" fi # if a user is not set, use $configfile, otherwise use $mycnf @@ -145,27 +136,17 @@ else userset=true; if [ $usevserver = yes ] then - vuserhome=`$VSERVER $vsname exec getent passwd "$user" | @AWK@ -F: '{print $6}'` + vuserhome=`$vexec getent passwd "$user" | @AWK@ -F: '{print $6}'` if [ $? -eq 2 ] then fatal "User $user not found in /etc/passwd" fi userhome="$vroot$vuserhome" - else - userhome=`getent passwd "$user" | @AWK@ -F: '{print $6}'` - if [ $? -eq 2 ] - then - fatal "User $user not found in /etc/passwd" - fi fi debug "User home set to: $userhome" [ -f $userhome/.my.cnf ] || fatal "Can't find config file in $userhome/.my.cnf" - if [ $usevserver = yes ]; then - defaultsfile="--defaults-extra-file=$vuserhome/.my.cnf" - else - defaultsfile="--defaults-extra-file=$userhome/.my.cnf" - fi + defaultsfile="--defaults-extra-file=$vuserhome/.my.cnf" debug "using $defaultsfile" fi @@ -177,13 +158,9 @@ then info "Initializing hotcopy method" if [ "$databases" == "all" ] then - if [ $usevserver = yes ] - then - info "dbhost: $dbhost" - execstr="$VSERVER $vsname exec $MYSQLHOTCOPY -h $dbhost --quiet --allowold --regexp /.\*/./.\*/ $hotdir" - else - execstr="$MYSQLHOTCOPY -h $dbhost --quiet --allowold --regexp /.\*/./.\*/ $hotdir" - fi + info "dbhost: $dbhost" + execstr="$vexec $MYSQLHOTCOPY -h $dbhost --quiet --allowold --regexp /.\*/./.\*/ $hotdir" + debug "su $user -c \"$execstr\"" if [ ! $test ] then @@ -201,12 +178,8 @@ then else for db in $databases do - if [ $usevserver = yes ] - then - execstr="$VSERVER $vsname exec $MYSQLHOTCOPY -h $dbhost --allowold $db $hotdir" - else - execstr="$MYSQLHOTCOPY -h $dbhost --allowold $db $hotdir" - fi + execstr="$vexec $MYSQLHOTCOPY -h $dbhost --allowold $db $hotdir" + debug 'su $user -c \"$execstr\"' if [ ! $test ] then @@ -233,21 +206,13 @@ then info "Initializing SQL dump method" if [ "$databases" == "all" ] then - if [ $usevserver = yes ] - then - debug "$VSERVER $vsname exec su $user -c \"$MYSQL $defaultsfile -N -B -e 'show databases'\" | sed 's/|//g;/\+----/d'" - databases=`$VSERVER $vsname exec su $user -c "$MYSQL $defaultsfile -N -B -e 'show databases'" | sed 's/|//g;/\+----/d'` - if [ $? -ne 0 ] - then - fatal "Authentication problem, maybe user/password is wrong or mysqld is not running?" - fi - else - databases=$(su $user -c "$MYSQL $defaultsfile -N -B -e 'show databases'" | sed 's/|//g;/\+----/d') - if [ $? -ne 0 ] - then - fatal "Authentication problem, maybe user/password is wrong or mysqld is not running?" - fi - fi + debug "$vexec su $user -c \"$MYSQL $defaultsfile -N -B -e 'show databases'\" | sed 's/|//g;/\+----/d'" + # -N removes the column title and -B should remove the ASCII table borders, but doesn't always so we use sed to make sure. + databases=`$vexec su $user -c "$MYSQL $defaultsfile -N -B -e 'show databases'" | sed 's/|//g;/\+----/d'` + if [ $? -ne 0 ] + then + fatal "Authentication problem, maybe user/password is wrong or mysqld is not running?" + fi fi for db in $databases @@ -269,30 +234,17 @@ then done DUMP="( $DUMP; $DUMP_STRUCT )" fi - if [ $usevserver = yes ] - then - # Test to make sure mysqld is running, if it is not sqldump will not work - $VSERVER $vsname exec su $user -c "$MYSQLADMIN $defaultsfile ping" - if [ $? -ne 0 ]; then - fatal "Either you have an authentication problem, or mysqld doesn't appear to be running!" - fi - if [ "$compress" == "yes" ]; then - execstr="$VSERVER $vsname exec $DUMP | $GZIP > $vroot$dumpdir/${db}.sql.gz" - else - execstr="$VSERVER $vsname exec $DUMP -r $vroot$dumpdir/${db}.sql" - fi - else - # Test to make sure mysqld is running, if it is not sqldump will not work - su $user -c "$MYSQLADMIN $defaultsfile ping" - if [ $? -ne 0 ]; then - fatal "Either you have an authentication problem, or mysqld doesn't appear to be running!" - fi - if [ "$compress" == "yes" ]; then - execstr="$DUMP | $GZIP > $dumpdir/${db}.sql.gz" - else - execstr="$DUMP -r $dumpdir/${db}.sql" - fi - fi + # Test to make sure mysqld is running, if it is not sqldump will not work + $vexec su $user -c "$MYSQLADMIN $defaultsfile ping" + if [ $? -ne 0 ]; then + fatal "Either you have an authentication problem, or mysqld doesn't appear to be running!" + fi + if [ "$compress" == "yes" ]; then + execstr="$vexec $DUMP | $GZIP > $vroot$dumpdir/${db}.sql.gz" + else + execstr="$vexec $DUMP -r $vroot$dumpdir/${db}.sql" + fi + debug "su $user -c \"$execstr\"" if [ ! $test ] then -- 2.30.2