Automatic database backup using mysqldump

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
amplifire
Forum Newbie
Posts: 23
Joined: Thu Jul 22, 2010 1:09 am
Location: India
Contact:

Automatic database backup using mysqldump

Post by amplifire »

Hello everyone
I referred to you for database backup using mysqldump ans system() function to execute it.
So far iam able to create backup file, but my backup file is empty. When i use mysqldump from command line, it works.
But from php it doesnt work what could be the problem?

Here's my code:

Code: Select all

if($_POST['backup'])
        {      
                 $command="mysqldump -h localhost -u root myDatabase > database/backup.sql";
                 system($command);
               
                 echo "<script>alert('table has been copied');</script>";
        }
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Automatic database backup using mysqldump

Post by VladSun »

You need to supply password:

Code: Select all

mysqldump -h localhost -uroot  -pXXXXXX myDatabase > database/backup.sql
also use absolute paths:

Code: Select all

/path/to/mysqldump -h localhost -uroot  -pXXXXXX myDatabase > /path/database/backup.sql
There are 10 types of people in this world, those who understand binary and those who don't
amplifire
Forum Newbie
Posts: 23
Joined: Thu Jul 22, 2010 1:09 am
Location: India
Contact:

Re: Automatic database backup using mysqldump

Post by amplifire »

My password is blank, so i did not supply the password. Moreover i have globaly registered the environment variable for mysqldump and i can use it from any directory. And also the .sql is being created but its blank. but when i create it from command line it gets created successfully.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Automatic database backup using mysqldump

Post by VladSun »

Try

Code: Select all

$result = `mysqldump -h localhost -uroot  myDatabase  2>&1`;
echo $result;
There are 10 types of people in this world, those who understand binary and those who don't
amplifire
Forum Newbie
Posts: 23
Joined: Thu Jul 22, 2010 1:09 am
Location: India
Contact:

Re: Automatic database backup using mysqldump

Post by amplifire »

Thanx for sharing different ideas. but now i will give you some more information.
1) I am using Windows XP.
2) I am using WAMP server.
3) I registered environment variable for mysqldump in Windows, so that i can run mysqldump from any directory.
4) today when i opened my Apache error log (apache_error), i noticed this
'[Sat Dec 25 18:21:08 2010] [error] [client 127.0.0.1] File does not exist: C:/wamp/www/favicon.ico
'mysqldump' is not recognized as an internal or external command,
operable program or batch file.'.
5) It means Apache is giving this error, but when i use the Command line , it works fine.
6) What could Apache possibly have to do with judging whether mysqldump is a command or not. Windows should judge.
7) I tried running 'cmd' and 'ping localhost' from php and they worked.

This is the situation. Please send some ideas on what could be happening.
Regards
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Automatic database backup using mysqldump

Post by VladSun »

Well, first try using the absolute path to mysqldump as I suggested. Looks like it's not in the PATH.
There are 10 types of people in this world, those who understand binary and those who don't
amplifire
Forum Newbie
Posts: 23
Joined: Thu Jul 22, 2010 1:09 am
Location: India
Contact:

Re: Automatic database backup using mysqldump

Post by amplifire »

Yeh ive even tried actual path. EX.
E:/wamp/www/file/database/backup.sql

The file is getting created but its blank, empty
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Automatic database backup using mysqldump

Post by VladSun »

I meant to add the absolute path to "mysqldump" executable.
E.g.:
C:/Program Files/MySQL/bin/mysqldump
There are 10 types of people in this world, those who understand binary and those who don't
amplifire
Forum Newbie
Posts: 23
Joined: Thu Jul 22, 2010 1:09 am
Location: India
Contact:

Re: Automatic database backup using mysqldump

Post by amplifire »

Yes. In Wamp server the absolute path is
C:\wamp\bin\mysql\mysql5.0.51b\bin
I have added it in User variables as well as System Variables under the name PATH.
And im successful in running mysqldump from any directory from command prompt. but not using PHP.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Automatic database backup using mysqldump

Post by VladSun »

VladSun wrote:Try

Code: Select all

$result = `mysqldump -h localhost -uroot  myDatabase  2>&1`;
echo $result;
Did you try it?
There are 10 types of people in this world, those who understand binary and those who don't
amplifire
Forum Newbie
Posts: 23
Joined: Thu Jul 22, 2010 1:09 am
Location: India
Contact:

Re: Automatic database backup using mysqldump

Post by amplifire »

I tried it
it is now showing this on my browser
"Invalid File'mysqldump' is not recognized as an internal or external command, operable program or batch file."
amplifire
Forum Newbie
Posts: 23
Joined: Thu Jul 22, 2010 1:09 am
Location: India
Contact:

Resolved: Automatic database backup using mysqldump

Post by amplifire »

I did it. Special thanx to VladSun.
I simply did this
$command="C:\wamp\bin\mysql\mysql5.0.51b\bin\mysqldump -h localhost -u root nigamkota > database/backup.sql";
system($command);

It means there is some problem in PATH environment variable. I will see it.
Thanx
Post Reply