473,434 Members | 1,438 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

mysql backup help please

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.php" 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

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxx
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_file = "yes"; // Set this to yes if you want to remove the
..sql file after gzipping. Yes is recommended.
$remove_gzip_file = "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 = "da******@mydomain.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_username"; // FTP username
$ftp_user_pass = "ftp_password"; // 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("mysqldump --opt -h$dbhost -u$dbuser -p$dbpass
$dbname >$filename");

if($use_gzip=="yes"){
$zipline = "tar -czf ".$dbname."-".$date."_sql.tar.gz
$dbname-$date.sql";
shell_exec($zipline);
}
if($remove_sql_file=="yes"){
exec("rm -r -f $filename");
}

if($use_gzip=="yes"){
$filename2 =
"$savepath/".$dbname."-".$date."_sql.tar.gz";
} else {
$filename2 = "$savepath/$dbname-$date.sql";
}
if($send_email == "yes" ){
$fileatt_type = filetype($filename2);
$fileatt_name = "".$dbname."-".$date."_sql.tar.gz";

$headers = "From: $from";

// Read the file to be attached ('rb' = read binary)
$file = fopen($filename2,'rb');
$data = fread($file,filesize($filename2));
fclose($file);

// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary =
"==Multipart_Boundary_x{$semi_rand}x";

// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" ."Content-Type:
multipart/mixed;\n" ." boundary=\"{$mime_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(base64_encode($data));

// Add file attachment to the message
$message .= "--{$mime_boundary}\n" ."Content-Type:
{$fileatt_type};\n" ." name=\"{$fileatt_name}\"\n"
.."Content-Disposition: attachment;\n" ."
filename=\"{$fileatt_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>Database backup created and
sent! File name $filename2</center></h4>";
} else {
echo "<h4><center>Mail could not be sent.
Sorry!</center></h4>";
}
}

if($use_ftp == "yes"){
$ftpconnect = "ncftpput -u $ftp_user_name -p
$ftp_user_pass -d debsender_ftplog.log -e dbsender_ftplog2.log -a -E
-V $ftp_server $ftp_path $filename2";
shell_exec($ftpconnect);
echo "<h4><center>$filename2 Was created and uploaded
to your FTP server!</center></h4>";

}

if($remove_gzip_file=="yes"){
exec("rm -r -f $filename2");
}

?>

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxx
Cron Command Path below

/home/cookie/domains/mydomain.co.nz/public_html/dbsender/dbsender.php
Jul 17 '05 #1
4 4397
Neil <me@nospam.net> wrote in
news:2a********************************@4ax.com:
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.php" 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.


When a cron job fails to do something that works fine when you do it
yourself, the problem is usually that the something is making assumptions
about the current working directory or the environment or permissions.
After taking a cursory look at your code, I'm going to guess that cron
isn't setting the current working directory to your own directory, so your
script will have to do that itself.
Jul 17 '05 #2
On 25 Jan 2004 06:49:43 GMT, Eric Bohlman <eb******@earthlink.net>
wrote:

When a cron job fails to do something that works fine when you do it
yourself, the problem is usually that the something is making assumptions
about the current working directory or the environment or permissions.
After taking a cursory look at your code, I'm going to guess that cron
isn't setting the current working directory to your own directory, so your
script will have to do that itself.


Thanks for the reply Eric.

I'm pretty certain that I have the permissions set correctly.....I
have even gone to chmod777 out of despiration, but still no luck. I
have also done a (php info) check on the server, and I have entered
the Command path as indicated in the server settings. I'm doing this
for a friend and the host he has chosen can't/won't help, so I'm
floundering a bit here as ColdFusion is really my 'thing' although I
have a pretty reasonable understanding of php.

The result I have so far....ie...the empty gz file, is the closest I
have come to sorting this out, but obviously, there's no cigar yet!!

Cheers,
Neil
Jul 17 '05 #3
On 2004-01-25, Neil <me@nospam.net> wrote:
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. Cron Command Path below
/home/cookie/domains/mydomain.co.nz/public_html/dbsender/dbsender.php


As you say it is working when you retrieve run the script from your
webbrowser, you could consider adding an entry to your crontab as:

wget http://mydomain/dbsender/dbsender.php

--
http://home.mysth.be/~timvw
Jul 17 '05 #4
On 25 Jan 2004 07:19:44 GMT, Tim Van Wassenhove <eu**@pi.be> wrote:
As you say it is working when you retrieve run the script from your
webbrowser, you could consider adding an entry to your crontab as:

wget http://mydomain/dbsender/dbsender.php


Thank you Tim, that works perfectly and now cron is executing my
script beautifully.

I didn't realise that wget could be used in this way, and I didn't
know that it was installed on the Apache server. Is this a default
part of the Apache Server installation?

This is interesting so I'll read what I can find on line.

Thanks for your help.

Cheers,
Neil

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: James | last post by:
HI, I'm looking for a script that will allow users/admins to have a one click backup solution for a MYSQL Database.. 'BACK DATABASE' button, click and its done... The a restore option, that...
1
by: Konrad | last post by:
I have an internet portal based on MySql database and what I need is to make a database backup , after each actualization of data. I know how should PHP code look like but I have no idea how to...
0
by: Duane Winner | last post by:
Hello all - I'm having a small problem with the mysql startup script that ships with MySQL-3.23.56-1. I'm running on RedHat Linux. It works fine, but I have a backup server that runs a script...
4
by: Ka | last post by:
I install a mysql server in default installation with latin charset, but I want to use GBK(a chinese charset), so that I can store and search chinese words directly. so, I download, unpack and...
4
by: news | last post by:
Our production database in an exported textfil runs about 60 MB. Compressed that's about 9 MB. I'm trying to import the export into another machine running FC3 and mySQL 11.18, and it appears as...
39
by: Mairhtin O'Feannag | last post by:
Hello, I have a client (customer) who asked the question : "Why would I buy and use UDB, when MySql is free?" I had to say I was stunned. I have no experience with MySql, so I was left sort...
0
by: newman | last post by:
Dear all, I have mysql 4.1.11 on my current server, i need my database restore another server.. (another server mysql version is 4.1.11 same.) And now... I just created new my database to new...
2
by: GS | last post by:
Implemented web application using MySQL to maintain inventory of the office PC's/laptops/Routers/hubs and other stuff, here we are using MySQL as back-end Database on Linux Operating system. I need...
4
by: Bootstrap Bill | last post by:
I'm looking for a PHP program to backup and restore a mysql database. I'm using Godaddy to host a forum. Their mysql control panel will only restore a database of two megabytes or less. My...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.