mysql数据备份的脚本分享
短信预约 -IT技能 免费直播动态提醒
本篇内容主要讲解“mysql数据备份的脚本分享”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“mysql数据备份的脚本分享”吧!
#!/usr/bin/perl -w
use warnings;
use strict;
use DBI;
my $backup_dir = "/root/perl/backup";
my $backup_log = "$backup_dir/backup_log";
my $db_name = "mysql";
my $db_host = "localhost";
my $db_user = "root";
my $db_passwd = "redhat";
my $db_port = "3306";
my $mysql_bin_dir = "/usr/local/mysql/bin";
my $ftp_user = "ftp";
my $ftp_passwd = "redhat";
my $ftp_host = "192.168.1.100";
my $ftp_dir ="pub";
my @backup_db_list = qw(mysql test);
my ($sec,$min,$hour,$day,$mon,$year)=localtime;
$year += 1900;
$mon += 1;
my $time_now = "$year-$mon-$day";
my @backup_list;
mkdir "$backup_dir",0755 if !-e "$backup_dir";
open LOG,">>$backup_log";
print LOG "----------------------\nStart: $time_now\n";
my $dsn = "DBI:mysql:$db_name:$db_host:$db_port";
my %conn_attrs = (
PrintError => 1,
RaiseError => 1,
AutoCommit => 1,);
my $dbh=DBI->connect($dsn,$db_user,$db_passwd,\%conn_attrs) or print LOG "Could not connect mysql server:".DBI->errstr."\n";
foreach my $db (@backup_db_list){
mkdir "$backup_dir/$db",0755 if !-e "$backup_dir/$db";
chdir "$backup_dir/$db";
my $backup_db_name = "${db}_${time_now}\.sql";
!system "$mysql_bin_dir/mysqldump -h $db_host -u $db_user -p$db_passwd -P $db_port -B $db > $backup_db_name" or print LOG "mysqldump error:$!\n";
system "(tar -zcvf $backup_db_name\.tar\.gz $backup_db_name) 2>&1 >/dev/null" if -e $backup_db_name;
unlink "$backup_db_name" if -e "$backup_db_name\.tar\.gz";
push (@backup_list,"$backup_dir/$db/$backup_db_name\.tar\.gz");
my @all_backup_db_list = <*.*.tar.gz>;
foreach my $backup_file (@all_backup_db_list){
unlink $backup_file if (time() - (stat($backup_file))[9] > (60*60*24*5));
}
}
$dbh->disconnect();
print "@backup_list\n";
my $ftp = Net::FTP->new($ftp_host,debug => 0) or print LOG "Count not connect ftp:$ftp_host\n";
$ftp->login($ftp_user,$ftp_passwd) or print LOG "Could not login ftp:$ftp_host\n";
$ftp->binary();
$ftp->cwd($ftp_dir);
foreach my $upload_backdb_file (@backup_list){
$ftp->put($upload_backdb_file) or print LOG "put $upload_backdb_file faild!\n";
}
print LOG "------------------------\n---------END----------\n";
$ftp->quit;
close(LOG);
到此,相信大家对“mysql数据备份的脚本分享”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341