php mysql backup on linux using exec - php

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.

Related

Accessing mySQL using EASYPHP DEVSERVER on command line

The book I'm reading asked me to type
"\Program Files\EasyPHP 3.0\mysql\bin\mysql" -u root
in my command line, but my mysql executable directory, which I installed using with EASYPHP DEVSERVER, is in
D:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\binaries\mysql\bin
So I tried making sense of it (or at least that's what I thought) and tried typing
"\Program Files (x86)\EasyPHP-DevServer-14.1VC11\binaries\mysql\bin" -u root
in the command line. I pressed enter and I got an error that that's unrecognizable.
So how do I access my database through the command line? Did I make any installation errors? Are there misconceptions I have?
Change "\Program Files (x86)\EasyPHP-DevServer-14.1VC11\binaries\mysql\bin" -u root to "D:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\binaries\mysql\bin" -u root
The writer assumed everything was on the same windows partition.
Also I advise you to add mysql to your PATH for convenience: https://stackoverflow.com/a/23841529/982075
C:\EasyPHP-Devserver-17\eds-binaries\dbserver\mysql5717x86x200727173959\bin>mysql -u root
And Enter
\bin>mysql -u root

shell_exec with MySQL not working even though safe mode is off

I have a string of script which working in terminal but does not work when I use it in PHP with shell_exec().I know a lot of questions similar to this question has been asked already but in my case the problem I am facing is that I have already tried the proposed solutions I found. Below is my simple code.
<?php
$output = shell_exec('mysql -u root -pmypass -h 127.0.0.1 mydatabase< db.sql');
echo "<pre>$output</pre>";
?>
So far this is the best solution I have found.
Does anyone knows what might be wrong?
Your shell_exec is this:
$output = shell_exec('mysql -u root -pmypass -h 127.0.0.1 mydatabase< db.sql');
And your command is this:
mysql -u root -pmypass -h 127.0.0.1 mydatabase< db.sql
The reason that command works when you are in the shell is the binary path to mysql is part of your user login profile.
To see what I mean, login to the shell as yourself and then type echo $PATH and what you will see is a list of search paths the shell uses to figure out where binaries you are attempting to run are located.
But when you attempt to run a script via shell_exec() the Apache server user running PHP is making the sell call. And that user typically does not have binary paths set. So you need to provide the full path to mysql which might be:
/usr/bin/mysql
Or:
/usr/local/bin/mysql
The best solution is from the shell use the which command like so:
which mysql
And then take the full path provided and adjust your shell_exec() command as follows; using /usr/bin/ for example:
$output = shell_exec('/usr/bin/mysql -u root -pmypass -h 127.0.0.1 mydatabase < db.sql');
Also, where is db.sql actually located? You would have to prepend the full path to that MySQL script like this as well; using /full/path/to/this/ for example:
$output = shell_exec('/usr/bin/mysql -u root -pmypass -h 127.0.0.1 my database < /full/path/to/this/db.sql');

getting "db: SQLSTATE[HY000] [2002] Connection refused" error on Mac with MAMP

I am trying to run the CLI version of this PHP databse Search and Replace Script, but I think this a more general MySQL problem relating to Mac OS X and MAMP. I receive the following error whenever I attempt to run the CLI script locally:
db: SQLSTATE[HY000] [2002] Connection refused
Here is the command I'm running:
./srdb.cli.php -h 127.0.0.1 -u root -n mydbname -proot -c utf\-8 -s mywebsite.com -r dev.mywebsite.com
What I've tried
I am able to connect to mysql using these settings, no problem, using mysql -u root -proot etc...
Swapping 127.0.0.1 for localhost gives the same error.
All my my.cnf files are blank.
Apache and MySQL are running fine.
I have succeeded in replicating this problem on another Mac running MAMP
I am using this mysql: /Applications/MAMP/Library/bin/mysql
And this php: /Applications/MAMP/bin/php/php5.3.28/bin/php
Anybody any ideas? Thanks!
Edit
Here is the source code showing how the script connects to MySQL:
https://github.com/interconnectit/Search-Replace-DB/blob/master/srdb.cli.php
which in turn imports this:
https://github.com/interconnectit/Search-Replace-DB/blob/master/srdb.class.php
As stated in my comment already, chances are that you're not running the PHP binary you thought you were running. Even if the MAMP php binary is in your path, the shebang line in srdb.cli.php reads #!/usr/bin/php and that points to the Apple-provided php binary.
So if you invoke the script with the full path to your MAMP php binary, the problem should be avoided:
/Applications/MAMP/bin/php/php5.3.28/bin/php srdb.cli.php -h 127.0.0.1 -u root -n mydbname -proot -c utf\-8 -s mywebsite.com -r dev.mywebsite.com
Another solution might be to replace the shebang line with:
#!/usr/bin/env php
This works only if the MAMP binary is in your $PATH in front of /usr/bin. Using #!/usr/bin/env phpensures however, that you're always using the same binary no matter if you're invoking the script via ./srdb.cli.php or with php srdb.cli.php.
Stop mysql :
sudo service mysql stop
And then start it :
sudo service mysql start
It resolved the problem
To add onto z80crew's brilliant solution, for anyone else unfamiliar/uncomfortable with altering path variables, specifying the full location paths for both the MAMP php binary and the search-replace-db script in the cli script provided by interconnect solved the problem for me. I put the strings to search for and replace with in quotes. I also increased the php timeout limit in wp-config.php with: set_time_limit(3000);
I was consistent with the server name between the options passed to the script and what's in my wp-config.php file (using localhost in wp-config, using localhost in the script as well)
/Applications/MAMP/bin/php/php7.4.2/bin/php /Applications/MAMP/htdocs/test/Search-Replace-DB-master/srdb.cli.php -h localhost -u root -proot --port 8889 -n test -s "http://olddomain.com" -r "http://localhost:8888/test" -v true

an alternative to mysqldump?

First of all, I am having serious problems with MYSQLDump, We have a dedicated server here for our main domain and I am running the following command:
mysqldump --opt -h localhost -u root -p ***** --all-databases > ~/var/www/vhosts/mydomain/httpdocs/db.sql
and I get nothing :(
But more importantly, I don't have root access to every server I have access to. But I do have database username and passwords. Surely there is a PHP only way of dumping the entire contents of a SQL database?
then why don't you use your user/password for the databases to do a per database dump as described i.e. here:
http://dev.mysql.com/doc/refman/5.1/de/mysqldump.html
mysqldump [options] --databases db_name1 [db_name2 db_name3...]
i just know two options to backup mysql-databases. One is to use mysqldump, the other one is to stop the mysql-server and backup the databasefiles. Doing dumps using PHP or whatever will last longer and cause much more trouble then just using mysqldump!
I was not aware there was a MySQL root.
Well, then that's the most likely cause of your problems, since you have this:
mysqldump --opt -h localhost -u root -p *****
^^^^^^^
The -u parameter expects a MySQL user and you are probably feeding it with systems' root user, which is something entirely different.
If you have a separate user for each database, I'm afraid you'll have to issue separate dumps.
Additionally, try to fetch error messages. You can redirect stderr to stdout by appending the 2>&1 operator to your command and you can grab output from shell_exec()'s return.
In the mysqldump command there is no space after -p and the password so your line should look like:
mysqldump --opt -h localhost -u root -p***** --all-databases > /var/www/vhosts/mydomain/httpdocs/db.sql

Why do I get this error when trying to use mysqldump command?

I want to back up and roll back the back up on a database, and I'm trying to see what's wrong:
'mysqldump' is not recognized as an internal or external command, operable program or batch file.
The above is what I get in the file that is exported. And this is what I run:
$database = 'logindb';
$backup = $location.'/'.$database.'_backup_'.date('Y').'_'.date('m').'_'.date('d').'.sql';
exec("mysqldump --opt -h localhost -u root logindb > $backup 2>&1", $output);
print_r($output);
As $location being my backup folder. So why do I get that error ?
And as additional info, I'm testing on localhost with XAAMP, the Apache server is running and MySQL is running as wel.
Because mysqldump is not in httpd user path.
I asume you work in Linux (or UNIX).
To know witch directory contains mysqldump you can execute whereis mysqldump.
Then you have two options:
Check for mysqldump directory is in PATH environment variable of httpd user (usualy www-data user for apache server). And include it in PATH is it not present.
Also you can execute mysqldump with full path:
Like this:
exec("/path_to_mysql_bins/mysqldump ...
The error is not a mysqldump error, it's a Windows error that says that it does not understand what mysqldump relates to. To fix that you can either include the path to mysqldump executable to the environment variables or run the exec command with a full path to the mysqldump executable (which you can find within XAMPP mysql folder).

Categories