Skip to content

Instantly share code, notes, and snippets.

@michabbb
Created January 14, 2015 14:55
Show Gist options
  • Save michabbb/06c21cbebee4552acbc2 to your computer and use it in GitHub Desktop.
Save michabbb/06c21cbebee4552acbc2 to your computer and use it in GitHub Desktop.
Backup Mysql Script Perl
#!/usr/bin/perl
use strict;
use DBI;
my $rootpwd_from = '';
my $rootpwd_to = '';
my $dbh = DBI->connect('DBI:mysql:mysql;host=localhost', 'root', $rootpwd_from, { RaiseError => 1,mysql_socket=> '/var/run/mysqld/mysqld.sock' } );
my $sth = $dbh->prepare("SELECT NOW()+1");
$sth->execute();
my $date = $sth->fetchrow_array();
system("mkdir -p /home/tmp/mysql/$date");
$sth = $dbh->prepare('SHOW DATABASES');
$sth->execute();
my @dbs = ();
while (my $db = $sth->fetchrow_array()) {
if (
($db eq 'information_schema') ||
($db eq 'abc') ||
($db eq 'def') ||
($db eq 'test') ||
($db eq 'test2')
) {
print "ignore: $db\n";
} else {
push(@dbs,$db);
}
}
$sth->finish;
$dbh->disconnect();
foreach my $db (@dbs) {
print "backing up: $db -> ";
eval { `nice -n 19 ionice -c3 mysqldump $db --opt --skip-lock-tables -R --triggers > /home/tmp/mysql/$date/mysqldump.$db.sql`; };
if ($@) { print "ERROR !!!!\n"; } else {
print "OK !!!!\n";
eval { `nice -n 19 ionice -c3 tar -cjvf /home/tmp/mysql/$date/$db.tar.bz2 /home/tmp/mysql/$date/mysqldump.$db.sql` };
if ($@) { print "ERROR !!!!\n"; } else {
}
}
}
eval {`nice -n 19 ionice -c3 tar -cjvf /home/backup/mysql/$date.tar.bz2 /home/tmp/mysql/$date/*.bz2 `; };
if ($@) {
print "ERROR !!!!\n";
} else {
system('touch /tmp/backup_done_mysql');
}
system("rm -f /home/tmp/mysql/$date/ -r");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment