⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mmm_restore

📁 mysql+ha. 实现高可用性 http://code.google.com/p/mysql-master-master/
💻
📖 第 1 页 / 共 2 页
字号:
    }        my $cnt;    my $pid;    if (-f $pid_file) {        # Read mysql pid        open(PID, $pid_file) || return "ERROR: Can't read mysql pid file ($pid_file)";        chomp($pid = <PID>);        close(PID);            # Check process        $cnt = kill(0, $pid);    }        if ($cnt == 0) {        LogWarn("MySql is not running now, skipping shutdown step...");        return  "OK: Mysql is not running";    }        LogNotice("MySql is running now. Going to stop it...");        #Stop mysql    my $rc_script = $host->{rc_script};    $rc_script = "/etc/init.d/mysql" if ($rc_script eq "");        my $res = system($rc_script, "stop");    if ($res) {        return "ERROR: Can't stop local MySql server!";    }    # Check process    my $wait = 15;    LogDebug("Waiting mysql process with $pid to shutdown: ");    while ($wait--) {        $cnt = kill(0, $pid);        last if ($cnt == 0);	LogDebug(".");	sleep(1);    }    if ($cnt != 0) {        PrintError("MySql Shutdown Failed!");	return "ERROR: MySql is running with PID $pid after shutdown request!";    }        LogDebug("Mysql Shutdown done: OK");    return "OK: MySql server is down now!";}#-----------------------------------------------------------------sub StartLocalMysql() {    # Get pid file location    my $pid_file = $host->{pid_file};    # Check pid file    if ($pid_file eq "") {        LogWarn("Warining: Can't find pid-file option in config file.");	$pid_file = "/var/run/mysqld/mysqld.pid";    }        if (-f $pid_file) {        # Read mysql pid        open(PID, $pid_file) || return "ERROR: Can't read mysql pid file ($pid_file)";        chomp(my $pid = <PID>);        close(PID);            # Check process        my $cnt = kill(0, $pid);	if ($cnt) {	    return "ERROR: Local mysql is running now with pid = $pid!";	}    }        LogNotice("MySql is not running now. Going to start it...");        # Start mysql    my $rc_script = $host->{rc_script};    $rc_script = "/etc/init.d/mysql" if ($rc_script eq "");        my $res = system($rc_script, "start");    if ($res) {        return "ERROR: Can't start local MySql server!";    }        return "OK: MySql server has been started!";}#-----------------------------------------------------------------sub CleanupDataDir() {    LogNotice("Cleaning data dir from master.info and binary logs...");        my $master_log = $status->{master}->{File};    unless ($master_log =~ /^(.*)\.(\d+)$/) {	return "ERROR: Unknown master binary log file name format ($master_log)!";    }        LogDebug("Deleting master binary logs: $1.*");    system("find $config->{dest_dir} -name $1.* | xargs rm -vf");        my $slave_log = $status->{slave}->{Relay_Log_File};    unless ($slave_log =~ /^(.*)\.(\d+)$/) {	return "ERROR: Unknown relay binary log file name format ($slave_log)!";    }        LogDebug("Deleting relay binary logs: $1.*");    system("find $config->{dest_dir} -name $1.* | xargs rm -vf");        LogDebug("Deleting .info and .pid  files...");    system("find $config->{dest_dir} -name master.info | xargs rm -vf");    system("find $config->{dest_dir} -name relay-log.info | xargs rm -vf");    system("find $config->{dest_dir} -name *.pid | xargs rm -vf");        LogDebug("Changing permissions on mysql data dir...");    system("chown -R mysql:mysql $config->{dest_dir}");        return "OK";}#-----------------------------------------------------------------sub RestoreDataFiles() {    if ($copy_method->{incremental} eq 'yes') {        LogError("This is incremental backup dir! Use --version option to restore specific version");	exit(0);    }    my $restore_command = $copy_method->{restore_command};    unless ($restore_command) {        LogError("Copy method without restore command! Please, check your config file!");	exit(0);    }        LogDebug("Performing template substitutions on command...");    $restore_command =~ s/%DATA_DIR%/$config->{dest_dir}/ig;    $restore_command =~ s/%BACKUP_DIR%/$backup_dir/ig;    LogDebug("Final command: '$restore_command'");        my $res = system($restore_command);    return "ERROR: Restoration error: $!" if ($res);    return "OK";}#-----------------------------------------------------------------sub RestoreDataFilesFromIncremental($version) {    if (!$copy_method->{incremental} || $copy_method->{incremental} eq 'no') {        LogError("Error: This is not incremental backup dir ($backup_dir backup method is $backup_method)!");	exit(0);    }    my $restore_command = $copy_method->{restore_command};    unless ($restore_command) {        LogError("Copy method without restore command! Please, check your config file!");	exit(0);    }        LogDebug("Performing template substitutions on command...");    $restore_command =~ s/%DATA_DIR%/$config->{dest_dir}/ig;    $restore_command =~ s/%BACKUP_DIR%/$backup_dir/ig;    $restore_command =~ s/%VERSION%/$version/ig;    LogDebug("Final command: '$restore_command'");        my $res = system($restore_command);    return "ERROR: Restoration error: $!" if ($res);    return "OK";}#-----------------------------------------------------------------sub SetupReplication() {    $restore_mode =~ /(\w+)\-(\w+)/;    my $src_mode = $1;    my $dst_mode = $2;        if ($dst_mode eq "single") {        LogNotice("Skipping replication setup because destination configiration is 'simple'");	return "OK: Skipped";    }        # Get remote host info    my $src_host = $config->{host}->{$status->{host}};        # Prepare params for local host    my $my_master_info = {};    $my_master_info->{'master_user'} = $config->{'replication_user'};    $my_master_info->{'master_pass'} = $config->{'replication_password'};    # Prepare params for remote host    my $peer_master_info = {};    $peer_master_info->{'master_user'} = $config->{'replication_user'};    $peer_master_info->{'master_pass'} = $config->{'replication_password'};        # Restore slave from slave - use the same replication params    if ($src_mode eq 'slave' && $dst_mode eq 'slave') {        $my_master_info->{'master_host'} = $status->{slave}->{'Master_Host'};        $my_master_info->{'master_port'} = $status->{slave}->{'Master_Port'};        $my_master_info->{'master_log'} = $status->{slave}->{'Master_Log_File'};        $my_master_info->{'master_pos'} = $status->{slave}->{'Read_Master_Log_Pos'};    }            # Restore slave from master - use master as replication source    if ($src_mode eq 'master' && $dst_mode eq 'slave') {        $my_master_info->{'master_host'} = $src_host->{'mysql_host'};        $my_master_info->{'master_port'} = $src_host->{'mysql_port'};        $my_master_info->{'master_log'} = $status->{master}->{'File'};        $my_master_info->{'master_pos'} = $status->{master}->{'Position'};    }    # Restore master from slave - not implemented    if ($src_mode eq 'slave' && $dst_mode eq 'master') {        LogWarn("FIXME: Don't know how to restore master from slave");	return "OK: Not implemented!";    }    # Restore master from master - not implemented    if ($src_mode eq 'master' && $dst_mode eq 'master') {        LogWarn("FIXME: Don't know how to restore master from master");	return "OK: Not implemented!";    }    LogDebug("My Master info: " . Dumper($my_master_info));    # Setup replication on local host    my $res = ChangeMasterTo($config->{this}, $my_master_info);    return $res unless ($res =~ /^OK/);        # Setup replication on remote host    if ($dst_mode eq 'master') {#        my $res = ChangePeerMasterTo();#        unless ($res =~ /^OK/) {#            LogError("Error: Can't set master for peer server: $res");#            exit(1);#        }    }        return "OK: Done";}#-----------------------------------------------------------------sub ChangeMasterTo($$) {    my $host_name = shift;    my $master = shift;    my $host = $config->{host}->{$host_name};        LogNotice("Changing master info for $host_name server...");    # get connection info    my $my_host = $host->{mysql_host};    my $my_port = $host->{mysql_port};    my $my_user = $host->{mysql_user};    my $my_pass = $host->{mysql_password};    my $dbh = MysqlConnect($my_host, $my_port, $my_user, $my_pass);    if (!$dbh) {        return "ERROR: Can't connect to $host_name mysql (host = $my_host:$my_port, user = $my_user)!";    }    # Stop slave    my $res = ExecuteQuery($dbh, "STOP SLAVE");    return "ERROR: SQL Query Error: " . $dbh->errstr unless($res);    # Change master    my $sql = "CHANGE MASTER TO " .              "  MASTER_HOST='$master->{master_host}'," .              "  MASTER_PORT=$master->{master_port}," .              "  MASTER_USER='$master->{master_user}'," .              "  MASTER_PASSWORD='$master->{master_pass}'";    if ($master->{master_log} && $master->{master_pos}) {        $sql .= ", MASTER_LOG_FILE='$master->{master_log}'," .                "  MASTER_LOG_POS=$master->{master_pos}";    }        my $res = ExecuteQuery($dbh, $sql);    return "ERROR: SQL Query Error: " . $dbh->errstr unless($res);        # Start slave    $res = ExecuteQuery($dbh, "START SLAVE");    return "ERROR: SQL Query Error: " . $dbh->errstr unless($res);    # disconnect    $dbh->disconnect;    return "OK";}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -