Here may be a solution to no files / empty files when trying to do mysqldump from a php script for backup purposes.
/*
I downloaded this original script from: http://www.sematopia.com/?p=61
I had visited many different websites on google -
I had what appears to be a very common problem among users... They attempted
to have a php script that backed up their mysql database via mysqldump ...
The common problem was that the script generated an empty file....
I had spent many hours and finally, made it to this script, which is WORKING
for me.....
For your convenience, I included the original script, as well as my implementation
below. My actual implementation is a version without email functionality.
It is dropping the files into my webhosting home directory......
The database server is different than my webhosting home directory which makes
things more difficult but that is addressed in my implementation of the script.
I am also using a YmdHis implementation of the date which generates file names
as follows: cwphpbackup20060913195214 - and allows me to backup once per second,
but the reason I did this is so that if I am attacked, I can find the appropriate
file to restore to..... This sure beats the daily cron jobs allowed by my web host...
Now when I want to backup, I just open up my browser, and click the backup bookmark
which is http://www.example.com/backup.php .....
*/
<?php
/*
ORIGINAL SCRIPT
Quickly and easily backup your MySQL database and have the
tgz emailed to you.
You need PEAR installed with the Mail and Mail_Mime packages
installed. Read more about PEAR here: [url]http://pear.php.net[/url]
This will work in any *nix enviornment. Make sure you have
write access to your /tmp directory.
*/
require_once('Mail.php');
require_once('Mail/mime.php');
// location of your temp directory
$tmpDir = "/tmp/";
// username for MySQL
$user = "root";
// password for MySQl
$password = "pass";
// database name to backup
$dbName = "db";
// the zip file emailed to you will have this prefixed
$prefix = "db_";
// email settings...
$to = "someone@gmail.com";
$from = "another@gmail.com";
$subject = "db - backup";
$sqlFile = $tmpDir.$prefix.date('Y_m_d').".sql";
$attachment = $tmpDir.$prefix.date('Y_m_d').".tgz";
$creatBackup = "mysqldump -u ".$user." --password=".$password." ".$dbName." > ".$sqlFile;
$createZip = "tar cvzf $attachment $sqlFile";
exec($creatBackup);
exec($createZip);
$headers = array('From' => $from, 'Subject' => $subject);
$textMessage = $attachment;
$htmlMessage = "";
$mime = new Mail_Mime("\n");
$mime->setTxtBody($textMessage);
$mime->setHtmlBody($htmlMessage);
$mime->addAttachment($attachment, 'text/plain');
$body = $mime->get();
$hdrs = $mime->headers($headers);
$mail = &Mail::factory('mail');
$mail->send($to, $hdrs, $body);
unlink($sqlFile);
unlink($attachment);
?>
=====================
<?php
/
MY IMPLEMENTATION
/
// location of your temp directory
$tmpDir = "/usr/home/username/";
// username for MySQL
$user = "username";
// password for MySQl
$password = "password";
// database name to backup
$dbName = "databasename not table name";
// the zip file emailed to you will have this prefixed
$prefix = "cwphpbackup";
// email settings...
$to = "email@example.com";
$from = "email@example.com";
$subject = "db - backup";
$sqlFile = $tmpDir.$prefix.date('YmdHis').".sql";
$attachment = $tmpDir.$prefix.date('YmdHis').".tgz";
$creatBackup = "/usr/local/bin/mysqldump -u ".$user." --password=".$password." -hdatabaseserver.example.com ".$dbName." > ".$sqlFile;
$createZip = "tar cvzf $attachment $sqlFile";
exec($creatBackup);
exec($createZip);
echo "backed up";
?>