Database; // dump db unset( $output ); exec( "$MYSQL_PATH/mysqldump $db_auth --opt $db 2>&1 >$BACKUP_TEMP/$db.sql", $output, $res); if( $res > 0 ) { error( true, "DUMP FAILED\n".implode( "\n", $output) ); } else { writeLog( "Dumped DB: " . $db ); if( $OPTIMIZE ) { unset( $output ); exec( "$MYSQL_PATH/mysqlcheck $db_auth --optimize $db 2>&1", $output, $res); if( $res > 0 ) { error( true, "OPTIMIZATION FAILED\n".implode( "\n", $output) ); } else { writeLog( "Optimized DB: " . $db ); } } // if } // if // compress db unset( $output ); if( $os == 'unix' ) { $BACKUP_NAME = $db . '-'. date('Y-m-d'). '.sql.bz2'; exec( "$USE_NICE $COMPRESSOR $BACKUP_TEMP/$db.sql 2>&1" , $output, $res ); } else { $BACKUP_NAME = $db . '-'. date('Y-m-d'). '.sql.zip'; exec( "zip -mj $BACKUP_TEMP/$db.sql.zip $BACKUP_TEMP/$db.sql 2>&1" , $output, $res ); } if( $res > 0 ) { error( true, "COMPRESSION FAILED\n".implode( "\n", $output) ); } else { writeLog( "Compressed DB: " . $db ); } if( $FLUSH ) { unset( $output ); exec("$MYSQL_PATH/mysqladmin $db_auth flush-tables 2>&1", $output, $res ); if( $res > 0 ) { error( true, "Flushing tables failed\n".implode( "\n", $output) ); } else { writeLog( "Flushed Tables" ); } } // if ################################ # processing ################################ writeLog( "Emailing " .$db. " backup to " . $EMAIL_ADDR . " .. " ); $headers = "From: " . $EMAIL_FROM . " "; // Generate a boundary string $rnd_str = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$rnd_str}x"; // Add headers for file attachment $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; // Add a multipart boundary above the plain message $body = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n"; // make Base64 encoding for file data $data = chunk_split(base64_encode(file_get_contents($BACKUP_TEMP.'/'.$db.'.sql.'.$ext))); // Add file attachment to the message $body .= "--{$mime_boundary}\n" . "Content-Type: {$backup_mime};\n" . " name=\"{$BACKUP_NAME}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$BACKUP_NAME}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; $res = mail( $EMAIL_ADDR, $db . $EMAIL_SUBJECT, $body, $headers ); if ( !$res ) { error( true, 'FAILED to email mysql dumps.' ); } } // while mysql_free_result($db_result); mysql_close($db_conn); mail ( $EMAIL_ADDR, 'MySQL Backup Log File for ' . date('Y-m-d'), file_get_contents($log_path), "From: " . $EMAIL_FROM . " "); // Email Log // see if there were any errors to email if ( ($ERROR_EMAIL) && ($error) ) { writeLog( "\nThere were errors!" ); writeLog( "Emailing error log to " . $ERROR_EMAIL . " .. " ); $headers = "From: " . $EMAIL_FROM . " "; $headers .= "MIME-Version: 1.0\n"; $headers .= "Content-Type: text/plain; charset=\"iso-8859-1\";\n"; $body = "\n".file_get_contents($err_path)."\n"; $res = mail( $ERROR_EMAIL, $ERROR_SUBJECT, $body, $headers ); if( !$res ) { error( true, 'FAILED to email error log.' ); } } ################################ # cleanup / mr proper ################################ // close log files fclose($f_log); fclose($f_err); // if error log is empty, delete it if( !$error ) { unlink( $err_path ); } // delete the log files if they have been emailed (and del_after is on) if ( $DEL_AFTER ) { if ( file_exists( $log_path ) ) { $success = unlink( $log_path ); error( !$success, "FAILED\nUnable to delete log file: ".$log_path ); } if ( file_exists( $err_path ) ) { $success = unlink( $err_path ); error( !$success, "FAILED\nUnable to delete error log file: ".$err_path ); } } // remove files in temp dir if ($dir = @opendir($BACKUP_TEMP)) { while (($file = readdir($dir)) !== false) { if (!is_dir($file)) { unlink($BACKUP_TEMP.'/'.$file); } } } closedir($dir); // remove temp dir rmdir($BACKUP_TEMP); ?>