Скрипт конвертации в InnoDB

Материал из BiTel WiKi

Перейти к: навигация, поиск
#! /bin/usr/perl

my $mysqluser="repl";
my $database="bgbilling_51";
my $dir="/var/log/mysql/";
my $file="alter_table.sql";

print "Script started!\n";
print "Enter password for mysql:\n";
chomp($pass = <STDIN>);

print "Create sql file?(yes/no)\n";
chomp($begin = <STDIN>);
if($begin eq "yes")
{
	$create_file=qx(mysql -u $mysqluser --password=$pass -e "SELECT table_name FROM information_schema.tables WHERE table_schema='$database' AND engine='MYISAM';"
             | tail -n +2 | grep -v -E '_[0-9]{6}\$' | xargs -I '{}' echo "ALTER TABLE {} ENGINE=INNODB;" > $dir$file);
	print $create_file;
}

print "Start converting base?(yes/no)\n";
chomp($begin = <STDIN>);
if($begin ne "yes"){
	print "By!\n";
	exit 0;
}

open(MYFILE, "$dir$file") || die "Ошибка при открытии $dir$file. $!\n";

$inc = 1;
$force = 0;
while(<MYFILE>)
{
	chomp;# убрать \n в последнем поле
#	$command=`echo "$_" > temp`;
#	$command=`mysql -u $mysqluser $database < temp`;
	print "$inc.command:\n   $_\n";
	if($force==0)
	{
		print "Continue? (press 'Enter' to continue or press 'q' to abort or 'f' to force mode)\n";
		chomp($begin = <STDIN>);
		if($begin eq "q")
		{
			print "By!\n";
			exit 0;
		}	
		if($begin eq "f")
		{
			$force=1;
		}	
	}

	$command=`mysql -u $mysqluser --password=$pass $database -e "$_"`;
	print "$inc.result:\n$command";
	$inc++;
}

close(MYFILE);
print "Script finished successfully!\n";
Личные инструменты