Search the Community
Showing results for tags 'mysqldump php cronjob'.
-
Thanks for taking a look. Please let me know if there is more info I can provide you. MySQL server version: 5.5.28-cll PHP version: 5.3.18 Apache version: 2.2.23 Raw MySQL statement: mysqldump --user=$dbuser --password=$dbpswd --host=$host $mysqldb | gzip > $filename_with_path" Description of purpose: This script is used to create a backup of the entire database every night. The script then emails me a message if there is an error. Errors MySQL returns: using Cron job, so no errors are returned, except that I'm receiving the email, which says that the size of the backup is 0. What's wrong: It was working fine on a PHP4 server. I moved it to a new server running PHP5 at the same host. The back up is no longer being created. To help isolate the problem, I've marked the complete script (below) with this: // IT SEEMS TO WORK UNTIL THIS POINT Things I've attempted: I've made sure the backup file path is correct, and tried to add the mysql path (I'm not entirely sure that's correct, but I've tried several options). I've changed to single quotes in many places where double quotes were not needed, I've also tried rewriting the system(mysqldump...) command line. I'm stumped! Complete script: <?php $email_recipient = 'XXX'; $email_sender = 'XXX'; $website = 'XXX'; $host = 'localhost'; // database host // Must CHANGE all of these to use for a different DB $dbuser = 'XXX'; // database user name $dbpswd = 'XXXl'; // database password $mysqldb = 'XXX'; // name of database $db_used_with = 'XXX'; $script_name = 'backupMysql.php'; $file_path = '/home/XXX/backups/dbname/'; // Create the backup file name if ( date(w) == 0 ) // Sunday = 0 { $week = intval (( date(j) + 6 ) / 7 ); // integer_value((numeric_day_of_month + 6) / 7) = week of month: 1-5 $filename_with_path = $file_path . 'backupWeek' . $week . '.sql.gz'; } else { // date("D"): 3-letter day of week (MON, TUE) $filename_with_path = $file_path . 'backup' . date("D") . '.sql.gz'; } // IT SEEMS TO WORK UNTIL THIS POINT if ( file_exists($filename_with_path) ) unlink($filename_with_path); system("mysqldump --user=$dbuser --password=$dbpswd --host=$host $mysqldb | gzip > $filename_with_path", $result); $size = filesize($filename_path); switch ($size) { case ($size>=1048576): $size = round($size/1048576) . ' MB'; break; case ($size>=1024): $size = round($size/1024) . ' KB'; break; default: $size = $size . ' bytes'; break; } // $Result returns the last line of the command output on success (0) or failure (FALSE, usually?). // or send a report weekly on Sundays (w = 0) if (( $result !== 0 ) || ( date(w) == 0 )) { // prepare success or failure/error email notice if ( $result === 0 ) { $result_text = 'SUCCESS'; } else { $result_text = 'ERROR'; } $title = "Database Backup Report: $result_text"; $subject = "$title -- Website: $website -- Database for: $db_used_with -- Database: $mysqldb"; $msg = "$title\n\n\n"; $msg .= 'The database backup for ' . $mysqldb . " has been run.\n\n"; $msg .= "Website: $website\n"; $msg .= 'The return code was: ' . $result . " (typically FALSE on failure, 0 on success).\n"; $msg .= "Script: $script_name\n"; $msg .= 'The file & path is: ' . $filename_with_path . "\n"; $msg .= 'Size of the backup: ' . $size . "\n\n"; $msg .= 'Server time of the backup: ' . date("F d h:ia") . "\n\n"; $msg .= "Sender: $email_sender\n"; $msg .= "Recipient: $email_recipient\n\n"; $msg = wordwrap($msg, 70); mail($email_recipient, $subject, $msg, 'From ' . $email_sender); } ?>