Generating a dump file from PHP - php

I'm trying to generate a mysql dump file from PHP using the following:
exec('mysqldump -u root -ppassword maindb > c:\DB_Dump.sql');
However the file being generated is blank. Anyone knows what's wrong?
From the cmd, this is working:
cd C:\Program Files\MySQL\MySQL Server 5.5\bin to change the path and then
mysqldump -u root -ppassword maindb > c:\DB_Dump.sql
But I'm trying to do it within PHP.

To discard most common issues:
Use full paths
Send output to a publicly writable directory
The path to mysqldump in your computer appears to have white spaces. Make sure you quote it properly:
exec('"C:\\Program Files\\MySQL\\MySQL Server 5.5\\bin\\mysqldump" -u root -ppassword maindb > c:\\DB_Dump.sql 2> c:\file.err.txt')

Related

php exec mysqldump doesn't work

I'm trying to run a mysqldump from my php file using exec(). File is located in my website's www directory and I'm accessing it from browser. But, no matter what I try, I'm simply not getting ANY results and no sql file is created.
Here's my command:
$export = exec("/usr/bin/mysqldump -u MY_USERNAME -pMY_PASS DATABASE_NAME products > /path/to/www/directory/sync/products.sql");
exec() is enabled. sync directory permissions are 777.
If I use the same command, but add $output, $return_var like this:
$export = exec("/usr/bin/mysqldump -u MY_USERNAME -pMY_PASS DATABASE_NAME products > /path/to/www/directory/sync/products.sql", $output, $return_var);
$output is an empty array and $return_var echoes 127.
Path to mysqldump is correct:
root#server [~]# which mysqldump
/usr/bin/mysqldump
root#server [~]#
If I run the command from console, it works just fine and the new file gets created in my sync directory.
Can someone please tell me what I'm doing wrong and what I'm missing here? The script used to work just fine until we moved to a new server...
I've been searching all over the place and have stumbled upon something that turned out to solve my problem.
Instead of using
$export = exec("/usr/bin/mysqldump -u MY_USERNAME -pMY_PASS DATABASE_NAME products > /path/to/www/directory/sync/products.sql");
I am now using this and THIS WORKS:
$export = exec('mysqldump -u MY_USERNAME -pMY_PASS DATABASE_NAME products -r "/path/to/www/directory/sync/products.sql"');
Using path to mysqldump doesn't make a difference. Using options (in this case -r) solved it for me, although I don't really understand why the original solution didn't work.
Maybe worth mentioning - I noticed that I can't import a database using the standard mysql -u -p db < table.sql - running that command just shows me help instructions, as if I ran mysql -?. I'm not sure if this is specific to me and my server, but maybe the solution above helps someone in a similar situation.

Execute mysqldump to back up database in sql format

I'm trying to use PHP's exec() function to run mysqldump to back up a database named projectdata from Amazon Web Service. But I can only create an empty sql file.
I'm running the php file with xampp, under Windows 7 where mysqldump is in C:\xampp\mysql\mysqldump
exec('C:\xampp\mysql\bin\mysqldump --user=user --password=password --host=cannotTellyou.amazonaws.com:3306 projectdata > backup.sql');
What you should do is: do a ssh login to your AWS machine. Run the mysqldump in command line and start debugging from there.
ssh <your remote AWS using your private_key>
then run
mysqldump -u <username> -p<password> yourDB | gzip > backupfilename.sql.tar.gz
use gzip if you want to zip your backup file, otherwise, it's not necessary.
Then refer to this post:
how to mysqldump remote db from local machine
I would try to explicitly specify the file name instead of redirecting the output. Like this:
exec('C:\xampp\mysql\mysqldump --user=user --password=password --host=cannotTellyou.amazonaws.com:3306 projectdata -r backup.sql');
The -r option should be used also because:
Direct output to a given file. This option should be used in MSDOS, because it prevents new line '\n' from being converted to '\r\n' (carriage return + line feed).
It works after removing the port number
C:\xampp\mysql\bin\mysqldump --user=user --password=password --host=cannotTellyou.amazonaws.com projectdata > backup.sql

How to restore mysql from backup?

I have got alldatabase.sql file
How I can restore data with it?
My console comand is not working:
mysqldump -u root -p < alldatabase.sql
I must to create database first, but all database in my one file. What I must to do?
You use mysqldump to take a snapshot, but you restore using the mysql command
mysql -u root -p < alldatabase.sql

php mysql backup on linux using exec

I am trying to use the exec() function to execute mysqldump file and store the backup file in a particular folder, but it doesn't appear in that folder. Any ideas on what is wrong with code?
exec('/usr/bin/mysqldump -h hostname -u root -psomepassword dbname > somepath/file.sql');
Thanks!
If you have root access on the server try that:
sudo su www-data // Ubuntu, for other systems find out the user on which apache is running
/usr/bin/mysqldump -h hostname -u root -psomepassword dbname > somepath/file.sql
If that works, the problem is in php. If not, you know the problem because of the error message. But as already pointed out, crontab is a better solution for this.

MySQLdump in php, I need it to dump in a specific directory

I run a script in one of my php files that dumps a specific database which works fine, but it puts the created file in the directory where the scripts is called. How can I make it to store the file in a specific directory? (e.g /blahblah/backups)
Here's the code (the connection code is not included but you get the idea)
system("mysqldump -h $pdb_server -u $pdb_user -p$pdb_pwd $pdb_name > $backupfile");
could I just add
/path/to/output/$backupfile
Put the Path before the command:
system("cd /path/to/output/$backupfile; mysqldump -h $pdb_server -u $pdb_user -p$pdb_pwd $pdb_name > $backupfile");
or
system("mysqldump -h $pdb_server -u $pdb_user -p$pdb_pwd $pdb_name > /path/to/output/$backupfile");

Categories