## go through each database from the list and dump the database
script_logline( "Backup MySQL databases." );
$failed = "";
foreach my $database (@databases) {
## all databases with _test / _old are not backed up
if ( $database =~ /_test/ || $database =~ /_old/ || $database =~ /lost\+found/ ) {
next;
}
## start mysqldump utility for this database
script_logline( "Dumping $database." );
# Keep track of errors
my $error = 0;
# We need to capture STDERR
# Save the original STDERR
my( $ORIGSTDERR );
open( $ORIGSTDERR, ">&", STDERR);
# Create a temporary filehandle to catch STDERR
my( $CATCHERR);
if( open( $CATCHERR, "+>", undef ) ) {
open( STDERR, ">&", $CATCHERR );
# From here STDERR is captured
# Execute mysqldump
if( open( MYSQLDUMP, "$mysqldump --defaults-extra-file=/var/mysql/my.pwd --skip-lock-tables -q $database |" ) ) {
# Execute gzip
if( open( GZIP, "| $gzip -c > $backupdir/$database-$date.sql.gz" ) ) {
while( <MYSQLDUMP> ) {
# Pipe the data from mysqldump to gzip
print GZIP;
}
close( GZIP );
} else {
script_logline( "gzip failed for $database" );
$failed .= "- gzip failed for $database\n";
$error++;
}
close( MYSQLDUMP );
} else {
script_logline( "mysqldump failed for $database" );
$failed .= "- mysqldump failed for $database\n";
$error++;
}
# Restore STDERR
open( STDERR, ">&", $ORIGSTDERR );
# Lets see if it produced any errors
seek( $CATCHERR, 0, SEEK_SET );
while( <$CATCHERR> ) {
my( $output ) = $_;
script_logline( "Error during mysqldump($database): $output" );
$failed .= "- Errors during mysqldump($database): $output";
$error++;
}
close( $CATCHERR );
} else {
my( $output ) = $!;
script_logline( "Unable to open temp file: $output" );
$failed .= "- Unable to open temp file: $output\n";
$error++;
}
if( $error == 0 ) { # Only clean up old backups if the backup succeeds
cleanupdirectory($backupdir, $database);
} else {
script_logline( "Backup of $database produced errors, not cleaning old backups" );
$failed .= "- Backup of $database produced errors, not cleaning old backups\n";
}
}
if ( $failed ne "" ) {
$failed = "Failure in backing up the following databases:\n\n$failed\n";
$failed .= "Check the log file /local/backup/mysqlbackup.log.\n";
script_logline( "Sending email to SysMan mailbox" );
mail_support($failed);
}
script_logline( "Ready dumping databases" );