Web Hosting Talk







View Full Version : PHP script to create tgz - look at the code for me


tmax100
06-06-2007, 06:45 PM
Hi, this is a cron job in php.
Currently this script emails me SQL file. (works fine)
Can anyone help me changing this code to compress SQL to tgz and email me the tgz file?

Thank you very much.
Look below
===========================================
$to = "email@address.com";
$from = "email@address.com";
$subject = "db - backup";
$sqlFile = $tmpDir.$prefix.date('Y_m_d').".sql";
$attachment = $tmpDir.$prefix.date('Y_m_d').".sql";

$creatBackup = "mysqldump -u ".$user." --password=".$password." -h ".$host." ".$dbName." > ".$sqlFile;
exec($creatBackup);

$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($attachment);

?>

sasha
06-06-2007, 07:07 PM
$sqlFile = $tmpDir.$prefix.date('Y_m_d').".sql.gz";
...
$creatBackup = .....$dbName." |gzip > ".$sqlFile;

There is no need for tar there, this is only single file

bilalk
06-06-2007, 07:10 PM
Since you have exec() privileges, why not just run GNU tar?


// Replace current $attachment = line
$attachment = "{$sqlFile}.tgz";

// Place after exec($creatBackup) line
exec("tar czvf {$attachment} {$sqlFile}");


EDIT: Sasha is right: One file, no need to make it into a tarball...


// Replace current $attachment = line
$attachment = "{$sqlFile}.gz";

// Place after exec($creatBackup) line
exec("gzip {$sqlFile}");

tmax100
06-06-2007, 07:22 PM
I don't know what tarball is..
All I know is you guys must be really good.

It's working.
8 hours of working... solved in 1 minute.
Impressive.

Thank you very much.