Hi,
I hope this question isn't too far off topic....I'm almost at my wits
end trying to figure this out.
I have a Mysql database and I wish to automate the backup of the
database because I don't wish to rely on the web host doing so. I am
using the "dbsender.p hp" script, which is written for this exact
purpose (available at hotscripts.com) . If I execute the script via my
browser, the database is emailed to me in a 'gzip' format, which is
perfect.....the script works just great. However, when I set a cron
job to automate this task, I get an empty '.gz' file attached to the
email instead of a ".gz" file containing my database.
Seeing as the scipt works properly when executed via the browser, the
script is fine. The cron job does it's task on time, and the empty .gz
attachment even has the correct name to it so I'm sure the command
path is correct.....but the attached .gz file is empty.
What on earth could be wrong??? I have tried other scripts with
exactly the same result.
I'll past the code below in case some kind soul can shed some light on
this frustrating problem.
Many thanks for looking.
Neil
xxxxxxxxxxxxxxx xxxxxxxxxxxxxxx xxxxxxxxxxxxxxx xxxxxxxxxxxxxxx xxxxx
dbsender.php script below
#!/usr/local/bin/php
<?php
// configure your database variables below:
$dbhost = 'localhost'; // Server address of your MySQL Server
$dbuser = 'cookie_neil'; // Username to access MySQL database
$dbpass = 'airbourne'; // Password to access MySQL database
$dbname = 'cookie_shop'; // Database Name
// Optional Options You May Optionally Configure
$use_gzip = "yes"; // Set to No if you don't want the files sent in
..gz format
$remove_sql_fil e = "yes"; // Set this to yes if you want to remove the
..sql file after gzipping. Yes is recommended.
$remove_gzip_fi le = "no"; // Set this to yes if you want to delete the
gzip file also. I recommend leaving it to "no"
// Configure the path that this script resides on your server.
$savepath =
"/home/cookie/domains/mydomain.co.nz/public_html/dbsender"; // Full
path to this directory. Do not use trailing slash!
$send_email = "yes"; // Do you want this database backup sent to your
email? Fill out the next 2 lines
$to = "my@email.co.nz "; // Who to send the emails to
$from = "database@mydom ain.co.nz"; // Who should the emails be sent
from?
$senddate = date("j F Y");
$subject = "MySQL Database Backup - $senddate"; // Subject in the
email to be sent.
$message = "Your MySQL database has been backed up and is attached to
this email"; // Brief Message.
$use_ftp = "no"; // Do you want this database backup uploaded to an
ftp server? Fill out the next 4 lines
$ftp_server = "localhost" ; // FTP hostname
$ftp_user_name = "ftp_userna me"; // FTP username
$ftp_user_pass = "ftp_passwo rd"; // FTP password
$ftp_path = "/"; // This is the path to upload on your ftp server!
// Do not Modify below this line! It will void your warranty!
$date = date("mdy-hia");
$filename = "$savepath/$dbname-$date.sql";
passthru("mysql dump --opt -h$dbhost -u$dbuser -p$dbpass
$dbname >$filename");
if($use_gzip==" yes"){
$zipline = "tar -czf ".$dbname."-".$date."_sql.t ar.gz
$dbname-$date.sql";
shell_exec($zip line);
}
if($remove_sql_ file=="yes"){
exec("rm -r -f $filename");
}
if($use_gzip==" yes"){
$filename2 =
"$savepath/".$dbname."-".$date."_sql.t ar.gz";
} else {
$filename2 = "$savepath/$dbname-$date.sql";
}
if($send_email == "yes" ){
$fileatt_type = filetype($filen ame2);
$fileatt_name = "".$dbname. "-".$date."_sql.t ar.gz";
$headers = "From: $from";
// Read the file to be attached ('rb' = read binary)
$file = fopen($filename 2,'rb');
$data = fread($file,fil esize($filename 2));
fclose($file);
// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary =
"==Multipart_Bo undary_x{$semi_ rand}x";
// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" ."Content-Type:
multipart/mixed;\n" ." boundary=\"{$mi me_boundary}\"" ;
// Add a multipart boundary above the plain message
$message = "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" .
$message . "\n\n";
// Base64 encode the file data
$data = chunk_split(bas e64_encode($dat a));
// Add file attachment to the message
$message .= "--{$mime_boundary }\n" ."Content-Type:
{$fileatt_type} ;\n" ." name=\"{$fileat t_name}\"\n"
.."Content-Disposition: attachment;\n" ."
filename=\"{$fi leatt_name}\"\n " ."Content-Transfer-Encoding:
base64\n\n" .
$data . "\n\n" ."--{$mime_boundary }--\n";
// Send the message
$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
echo "<h4><center>Da tabase backup created and
sent! File name $filename2</center></h4>";
} else {
echo "<h4><center>Ma il could not be sent.
Sorry!</center></h4>";
}
}
if($use_ftp == "yes"){
$ftpconnect = "ncftpput -u $ftp_user_name -p
$ftp_user_pass -d debsender_ftplo g.log -e dbsender_ftplog 2.log -a -E
-V $ftp_server $ftp_path $filename2";
shell_exec($ftp connect);
echo "<h4><center>$f ilename2 Was created and uploaded
to your FTP server!</center></h4>";
}
if($remove_gzip _file=="yes"){
exec("rm -r -f $filename2");
}
?>
xxxxxxxxxxxxxxx xxxxxxxxxxxxxxx xxxxxxxxxxxxxxx xxxxxxxxxxxxxxx xxxxxxxxxx
Cron Command Path below
/home/cookie/domains/mydomain.co.nz/public_html/dbsender/dbsender.php
I hope this question isn't too far off topic....I'm almost at my wits
end trying to figure this out.
I have a Mysql database and I wish to automate the backup of the
database because I don't wish to rely on the web host doing so. I am
using the "dbsender.p hp" script, which is written for this exact
purpose (available at hotscripts.com) . If I execute the script via my
browser, the database is emailed to me in a 'gzip' format, which is
perfect.....the script works just great. However, when I set a cron
job to automate this task, I get an empty '.gz' file attached to the
email instead of a ".gz" file containing my database.
Seeing as the scipt works properly when executed via the browser, the
script is fine. The cron job does it's task on time, and the empty .gz
attachment even has the correct name to it so I'm sure the command
path is correct.....but the attached .gz file is empty.
What on earth could be wrong??? I have tried other scripts with
exactly the same result.
I'll past the code below in case some kind soul can shed some light on
this frustrating problem.
Many thanks for looking.
Neil
xxxxxxxxxxxxxxx xxxxxxxxxxxxxxx xxxxxxxxxxxxxxx xxxxxxxxxxxxxxx xxxxx
dbsender.php script below
#!/usr/local/bin/php
<?php
// configure your database variables below:
$dbhost = 'localhost'; // Server address of your MySQL Server
$dbuser = 'cookie_neil'; // Username to access MySQL database
$dbpass = 'airbourne'; // Password to access MySQL database
$dbname = 'cookie_shop'; // Database Name
// Optional Options You May Optionally Configure
$use_gzip = "yes"; // Set to No if you don't want the files sent in
..gz format
$remove_sql_fil e = "yes"; // Set this to yes if you want to remove the
..sql file after gzipping. Yes is recommended.
$remove_gzip_fi le = "no"; // Set this to yes if you want to delete the
gzip file also. I recommend leaving it to "no"
// Configure the path that this script resides on your server.
$savepath =
"/home/cookie/domains/mydomain.co.nz/public_html/dbsender"; // Full
path to this directory. Do not use trailing slash!
$send_email = "yes"; // Do you want this database backup sent to your
email? Fill out the next 2 lines
$to = "my@email.co.nz "; // Who to send the emails to
$from = "database@mydom ain.co.nz"; // Who should the emails be sent
from?
$senddate = date("j F Y");
$subject = "MySQL Database Backup - $senddate"; // Subject in the
email to be sent.
$message = "Your MySQL database has been backed up and is attached to
this email"; // Brief Message.
$use_ftp = "no"; // Do you want this database backup uploaded to an
ftp server? Fill out the next 4 lines
$ftp_server = "localhost" ; // FTP hostname
$ftp_user_name = "ftp_userna me"; // FTP username
$ftp_user_pass = "ftp_passwo rd"; // FTP password
$ftp_path = "/"; // This is the path to upload on your ftp server!
// Do not Modify below this line! It will void your warranty!
$date = date("mdy-hia");
$filename = "$savepath/$dbname-$date.sql";
passthru("mysql dump --opt -h$dbhost -u$dbuser -p$dbpass
$dbname >$filename");
if($use_gzip==" yes"){
$zipline = "tar -czf ".$dbname."-".$date."_sql.t ar.gz
$dbname-$date.sql";
shell_exec($zip line);
}
if($remove_sql_ file=="yes"){
exec("rm -r -f $filename");
}
if($use_gzip==" yes"){
$filename2 =
"$savepath/".$dbname."-".$date."_sql.t ar.gz";
} else {
$filename2 = "$savepath/$dbname-$date.sql";
}
if($send_email == "yes" ){
$fileatt_type = filetype($filen ame2);
$fileatt_name = "".$dbname. "-".$date."_sql.t ar.gz";
$headers = "From: $from";
// Read the file to be attached ('rb' = read binary)
$file = fopen($filename 2,'rb');
$data = fread($file,fil esize($filename 2));
fclose($file);
// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary =
"==Multipart_Bo undary_x{$semi_ rand}x";
// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" ."Content-Type:
multipart/mixed;\n" ." boundary=\"{$mi me_boundary}\"" ;
// Add a multipart boundary above the plain message
$message = "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" .
$message . "\n\n";
// Base64 encode the file data
$data = chunk_split(bas e64_encode($dat a));
// Add file attachment to the message
$message .= "--{$mime_boundary }\n" ."Content-Type:
{$fileatt_type} ;\n" ." name=\"{$fileat t_name}\"\n"
.."Content-Disposition: attachment;\n" ."
filename=\"{$fi leatt_name}\"\n " ."Content-Transfer-Encoding:
base64\n\n" .
$data . "\n\n" ."--{$mime_boundary }--\n";
// Send the message
$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
echo "<h4><center>Da tabase backup created and
sent! File name $filename2</center></h4>";
} else {
echo "<h4><center>Ma il could not be sent.
Sorry!</center></h4>";
}
}
if($use_ftp == "yes"){
$ftpconnect = "ncftpput -u $ftp_user_name -p
$ftp_user_pass -d debsender_ftplo g.log -e dbsender_ftplog 2.log -a -E
-V $ftp_server $ftp_path $filename2";
shell_exec($ftp connect);
echo "<h4><center>$f ilename2 Was created and uploaded
to your FTP server!</center></h4>";
}
if($remove_gzip _file=="yes"){
exec("rm -r -f $filename2");
}
?>
xxxxxxxxxxxxxxx xxxxxxxxxxxxxxx xxxxxxxxxxxxxxx xxxxxxxxxxxxxxx xxxxxxxxxx
Cron Command Path below
/home/cookie/domains/mydomain.co.nz/public_html/dbsender/dbsender.php
Comment