Jump to content

Backup your MySQL db through php?


skaterdav85
 Share

Recommended Posts

ya i tried that but it didnt work. This is my error:Can't create/write to file 'C:\Documents and Settings\All Users\Application Data\MySQL\MySQL Server 5.1\Data\backup\employees.sql' (Errcode: 2)Anyone know what this means? How does phpMyAdmin do it with the export database function?

Link to comment
Share on other sites

So I tried this since this seemed like it would export the whole db into a .gz file, but when i load the page, its a blank screen and i see nothing in my folder:

$backupFile = $databasename . date("Y-m-d-H-i-s") . '.gz';$command = "mysqldump --opt -h $hostname -u $dbuser -p $dbpassword $databasename | gzip > $backupFile";system($command);

Any possible reasons why this is not working?

Link to comment
Share on other sites

Maybe your host has disabled shell execution? Make sure you have all error messages enabled. Also try echoing $command to make sure it is formed as you think it is.

Link to comment
Share on other sites

You should either print the return value from system, or use passthru instead. The system function returns the output, but you're not doing anything with it. If there was an error message from mysqldump you wouldn't see it.

Link to comment
Share on other sites

i tried using passthru like the following, and it echoed 255 out to the page. What does this mean?

passthru($command, $passThruResult);echo $passThruResult;

Anything wrong with this command? I echoed it out to the page and it says:

mysqldump --opt -h virtualserver1 -u root -p thePW testdb | gzip > testdb2010-05-12-12-29-11.gz

Link to comment
Share on other sites

i tried using passthru like the following, and it echoed 255 out to the page. What does this mean?
That means the return status of the command was 255, for whatever that's worth. You don't need to echo anything with passthru, as the name implies the output from the command goes straight to the browser.
Anything wrong with this command?
You'll probably want to check the documentation for mysqldump to see about the command line options. With mysql, for example, the -p option doesn't have a space after it. I'm not sure if that's different for mysqldump, but it would be worth looking up.
Link to comment
Share on other sites

ok i checked out the mysqldump command line documentation, and i have slight improvement. I tried the following:

$command = 'mysqldump --opt -h '.$hostname.' -u '.$dbuser.' -p'.$dbpassword.' '.$databasename.' > backup/backup3.sql';$command = 'mysqldump --opt -h '.$hostname.' -u '.$dbuser.' -p'.$dbpassword.' '.$databasename.' | gzip > backup/GZbackup.gz';

The first one at least creates a new file, but the 2nd one doesnt. The only difference is that the 2nd one has this gzip thing in it. Why would I not be getting anything in the file? Am I failing to connect to the db even though i get a return value from passthru of 255?

Link to comment
Share on other sites

I haven't looked at the Linux documentation, but I assume that a status of 255 just means the process quit successfully. That doesn't indicate what it did, just that it stopped in a normal way. I'm not very familiar with the pipe syntax for Linux commands though, so I'm not real sure specifically what that second command is telling the OS to do.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...