Using Perl script to realize MySQL import and export

Source: Internet
Author: User
Tags exit mkdir mysql host import database mysql database mysql import perl script backup

Unit's development colleague needs to import a MySQL library into local MySQL so in order to reduce the manual operation, I wrote the following MySQL import and export script with Perl implementation:

#!/usr/bin/perl-w
#Author: Andylhz
#Date: 20120/08/29
#Purpose: Import database from other MySQL DB host to Local.
My $db _host_local= "localhost";
My $db _username_local= "root";
My $db _password_local= "password";
My $db _dbname_local= "Baby";
My $db _backupdir_local= "/root/dbbackup/local";
if (-e $db _backupdir_local) {
Print "The database backup dir is $db _backupdir_local\n";
}else{
mkdir $db _backupdir_local;
}
My $db _time_local= ' date +%y%m%d%h%m%s ';
#backup localhost databases
Print "$db _dbname_local is backuping.....\n";
System "/usr/local/mysql/bin/mysqldump-h $db _host_local-u$db_username_local-p$db_password_local--databases $db _ dbname_local > $db _backupdir_local/$db _dbname_local$db_time_local ";
if ($?==0) {
Print "Database $db _dbname_local is backup ok!\n";
}else{
Print "Database $db _dbname_local is backup false!\n";
Exit;
}
Print "Compress the dump file\n";
System "gzip $db _backupdir_local/$db _dbname_local$db_time_local";
if ($?==0) {
Print "Dump file is compress ok!\n";
}else{
Print "Dump file is compress false!\n";
Exit
}
####
#!/usr/bin/perl
%iplist= (' 144trunk ' => ' 192.168.100.144 ', ' 114branch ' => ' 192.168.100.114 ', ' 164release ' => ' 192.168.100.164 ') );
@keys = Keys%iplist;
@values = values%iplist;
print "$keys [0]-> $values [0] Trunk MySQL (0\n);
Print "$keys [1]-> $values [1] Branch MySQL (1\n);
Print "$keys [2]-> $values [2] release MySQL (2\n);
Print "Please choose the MySQL host IP which for your want to import:\n";
Chomp ($_=<stdin>);
if (m/0|1|2/) {
Chomp ($dbhost _remote= "$values [$_]");
}else{
Print "The MySQL database isn't found choose one from above list!\n";
Exit
}
####
#sub improt_remote_db {
# print "Please tell me which MySQL DB does you want to import to local:\n";
My $db _host_remote= "$dbhost _remote";
My $db _username_remote= "root";
My $db _password_remote= "password";
My $db _dbname_remote= "Baby";
My $db _backupdir_remote= "/root/dbbackup/remote";
if (-e $db _backupdir_remote) {
Print "The database export dir is $db _backupdir_remote\n";
}else{
mkdir $db _backupdir_remote;
}
My $db _time_remote= ' date +%y%m%d%h%m%s ';
#backup localhost databases
Print "The database $db _dbname_remote is dumping.....\n";
'/usr/local/mysql/bin/mysqldump-h $db _host_remote-u$db_username_remote-p$db_password_remote--databases $db _ Dbname_remote > $db _backupdir_remote/$db _dbname_remote$db_time_remote ';

if ($?==0) {
Print "Database $db _dbname_remote is Export ok!\n";
}else{
Print "Database $db _dbname_remote is Export false!\n";
Exit
}
Print "Import $db _host_remote mysql $db _dbname_remote begin.....\n";
System "/usr/local/mysql/bin/mysql-u$db_username_local-p$db_password_local $db _dbname_local < $db _backupdir_ remote/$db _dbname_remote$db_time_remote ";
if ($?==0) {
Print "Database $db _dbname_remote is import ok!\n";
}else{
Print "Database $db _dbname_remote is import false!\n";
Exit
}
#}
####
Unlink "$db _backupdir_remote/*";
Print "Import $db _host_remote mysql $db _dbname_remote is ok\n";
#END

This article is from the "Shadow Knight" blog, please be sure to keep this source http://andylhz2009.blog.51cto.com/728703/980818

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.