X-Git-Url: https://git.stderr.nl/gitweb?a=blobdiff_plain;f=handlers%2Fmysql.in;h=f79a0b2b9b235c3bfeb4fd2e83bdc9757f1ff544;hb=4215d06b05dbfef0adf053f67c7839b6ae343810;hp=de4e4c3d2f920d8117c2e960e9c1f5e1619ccf22;hpb=2bca513c38c70cd7a56bef23512183629e91bade;p=matthijs%2Fupstream%2Fbackupninja-vserver.git diff --git a/handlers/mysql.in b/handlers/mysql.in index de4e4c3..f79a0b2 100644 --- a/handlers/mysql.in +++ b/handlers/mysql.in @@ -6,9 +6,11 @@ getconf backupdir /var/backups/mysql getconf databases all getconf ignores +getconf nodata getconf dbhost localhost getconf hotcopy no getconf sqldump no +getconf sqldumpoptions "--lock-tables --complete-insert --add-drop-table --quick --quote-names" getconf compress yes getconf vsname @@ -30,27 +32,33 @@ if [ $vservers_are_available = yes ]; then fatal "The vserver given in vsname ($vsname) does not exist." fi # is it running ? - $VSERVERINFO -q $vsname RUNNING - if [ $? -ne 0 ]; then - fatal "The vserver $vsname is not running." - fi + vservers_running $vsname || fatal "The vserver $vsname is not running." # 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 +else + [ -z "$vsname" ] || warning 'vservers support disabled in backupninja.conf, vsname configuration line will be ignored' fi ## Prepare ignore part of the command ## This only works for mysqldump at the moment ignore='' -for i in $ignores; do +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 @@ -58,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 @@ -88,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)." @@ -128,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 @@ -144,23 +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" - defaultsfile="--defaults-extra-file=$userhome/.my.cnf" + defaultsfile="--defaults-extra-file=$vuserhome/.my.cnf" debug "using $defaultsfile" fi @@ -172,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 --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 @@ -196,12 +178,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 @@ -228,49 +206,45 @@ 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=`echo 'show databases' | 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 - 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 do - 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 $MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db | $GZIP > $vroot$dumpdir/${db}.sql.gz" - else - execstr="$VSERVER $vsname exec $MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db -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="$MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db | $GZIP > $dumpdir/${db}.sql.gz" - else - execstr="$MYSQLDUMP $defaultsfile --lock-tables --complete-insert --add-drop-table --quick --quote-names $ignore $db -r $dumpdir/${db}.sql" - fi - fi + DUMP_BASE="$MYSQLDUMP $defaultsfile $sqldumpoptions" + + # Dumping structure and data + DUMP="$DUMP_BASE $ignore $db" + + # If requested, dump only the table structure for this database + if echo "$nodata" | grep -E '(^|[[:space:]])'"$db\." >/dev/null + then + # Get the structure of the tables, without data + DUMP_STRUCT="$DUMP_BASE --no-data $db" + for qualified_table in $nodata + do + table=$( expr match "$qualified_table" "$db\.\([^\w]*\)" ) + DUMP_STRUCT="$DUMP_STRUCT $table" + done + DUMP="( $DUMP; $DUMP_STRUCT )" + 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