Forum Moderators: coopster

Message Too Old, No Replies

Exporting MySQL database to file using PHP

         

designaweb

8:35 pm on Nov 16, 2005 (gmt 0)

10+ Year Member



I want to export my database to a file that is hosted on the same server. I got this script from somewhere, but it doesnt work. Most likely because I cannot access mysql command thru the shell_exec() command, however, I am able to use the wget command thru the shell...

Here's the code I was trying to use:

$User = "username"; // Put New user -- CPanel user or MySQL user with All permissions is fine.

$Password = "password"; // Put New Password

$DatabaseName = "easyterr_main"; // Put Database name

$File = $_SERVER['DOCUMENT_ROOT']."/somefile.sql"; // Put the complete path here -- /home/user/database.sql for example

$Results = shell_exec("mysqldump -u$User -p$Password $DatabaseName > $File");

Is there any alternative way to export a mysql database to a local file? Or do you have any idea why the above script isn't working? somefile.sql has been CHMOD-ed to 777, so that's no prob...

Anyango

4:41 am on Nov 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you have phpMyAdmin installed on that server? that ll take less then a minute to do that.

designaweb

7:33 am on Nov 17, 2005 (gmt 0)

10+ Year Member



Yes I have, but I want to run this PHP script from the cron on a daily basis, so I can wget this file from an external server (again, cronjob) to update the remote DB...

jackvull

12:00 pm on Nov 17, 2005 (gmt 0)

10+ Year Member



First, check if your server or host has PHP's safe_mode turned on. I had a similar problem with my hosting company and they had to turn safe_mode off for this to work.
Be aware that turning it off has some security implications but I don't know of another way around it to get a daily DB backup working properly without direct access to the machine and some knowledge in other forms of scripting (bash, perl, etc.)

designaweb

11:54 pm on Nov 19, 2005 (gmt 0)

10+ Year Member



i got it to work, but the only export i get is this:

-- MySQL dump 10.9
--
-- Host: localhost Database: dbase_name
-- ------------------------------------------------------
-- Server version4.1.14-standard

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

The database HAS content though...

designaweb

12:07 am on Nov 20, 2005 (gmt 0)

10+ Year Member



Sorry, nevermind, the mysql user had limited access. Fully works now :) Hope this helps others with the same issue some day...

Jaunty Edward

12:54 pm on Nov 20, 2005 (gmt 0)

10+ Year Member



hi,

trying to do something similar.. can anyone tell me if I can find out if shell_exec() is blocked on my server using php.

I damn sure that it has been blocked on my server.

Thanks
Bye

designaweb

3:11 pm on Nov 20, 2005 (gmt 0)

10+ Year Member



Try this:

echo exec("wget");

this should return an echo on your screen that says: "Try `wget --help' for more options."