$dbhost) { // Initialize/reset log message variable and backup path $message = ''; $directory = dirname(__FILE__) . "/$dbhost"; // If archive directory doesn't exist for the dbhost, make one if (!is_dir($directory)) { mkdir($directory, 0755); } // If an archive directory exists, backup the db if (is_dir($directory)) { // The dump archive to save $archive = "$directory/$dbhost" . date("Ymd-Hi") . '.sql.gz'; // The mysqldump command for this archive, 2>&1 captures error output $command = "$mysqldump --all-databases --opt --quick -h $dbhost " . "-u $dbuser -p$dbpass 2>&1 | gzip > $archive"; // Execute the command $output = shell_exec($command); // Write to the log file if (preg_match('/Got error/', $output)) { $message .= date(YmdHis) . " - $output\n"; } else { $message .= date(YmdHis) . " - Successfully dumped and saved $archive\n"; } } else { $message .= date(YmdHis) . " - ERROR: $directory does not exist, " . "mysqldump aborted\n"; } // Append messages to log $fh = fopen($logfile, 'a') or die("Unable to open $logfile"); fwrite($fh, $message); fclose($fh); // Add to an email message $email .= $message; } // Send session output to the DBA mail($to, $subject, $email); ?>