X-Git-Url: https://git.stderr.nl/gitweb?a=blobdiff_plain;f=handlers%2Fmysql.in;h=a6cd023ae7b422675c342775d9e3c1f3c4557464;hb=99fc759bcc31990e23c4bd974a65f782d3dbd917;hp=3a908f68ef669b62e6c0cb3d63eeb230a5b86199;hpb=85f69abc9779dd28c0dfdf128aa4c77e08fe3b7f;p=matthijs%2Fupstream%2Fbackupninja.git diff --git a/handlers/mysql.in b/handlers/mysql.in index 3a908f6..a6cd023 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,23 +50,29 @@ ignore='' for i in $ignores $nodata; do ignore="$ignore --ignore-table=$i" done - -# 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 -[ -d $vroot$backupdir ] || fatal "Backup directory '$vroot$backupdir'" -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 +if [ $usevserver = yes ]; then + vroot="$VROOTDIR/$vsname" + vexec="$VSERVER $vsname exec" else - [ "$sqldump" == "no" -o -d $dumpdir ] || mkdir -p $dumpdir - [ "$hotcopy" == "no" -o -d $hotdir ] || mkdir -p $hotdir + vroot="" + vexec="" + vsname="" fi +interpolated=`interpolate "$backupdir" "$vsname"` + +# create backup dirs, $vroot will be empty if no vsname was specified +# and we will instead proceed to operate on the host +[ -d $vroot$interpolated ] || mkdir -p $vroot$interpolated +[ -d $vroot$interpolated ] || fatal "Backup directory '$vroot$backupdir'" + +hotdir="$interpolated/hotcopy" +dumpdir="$interpolated/sqldump" + +[ "$sqldump" == "no" -o -d $vroot$dumpdir ] || mkdir -p $vroot$dumpdir +[ "$hotcopy" == "no" -o -d $vroot$hotdir ] || mkdir -p $vroot$hotdir + ####################################################################### ## AUTHENTICATION @@ -89,13 +94,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 +129,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 +140,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 +162,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 --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 +182,8 @@ then else for db in $databases do - if [ $usevserver = yes ] - then - execstr="$VSERVER $vsname exec $MYSQLHOTCOPY --allowold $db $hotdir" - else - execstr="$MYSQLHOTCOPY --allowold $db $hotdir" - fi + execstr="$vexec $MYSQLHOTCOPY -h $dbhost --allowold $db $hotdir" + debug 'su $user -c \"$execstr\"' if [ ! $test ] then @@ -233,21 +210,13 @@ then info "Initializing SQL dump method" if [ "$databases" == "all" ] then - if [ $usevserver = yes ] - then - debug 'echo show databases | $VSERVER $vsname exec su $user -c \"$MYSQL $defaultsfile\" | grep -v Database' - databases=`echo 'show databases' | $VSERVER $vsname exec su $user -c "$MYSQL $defaultsfile" | grep -v Database` - 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 +238,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