Hi,

I am trying to backup one of my Mysql database with a PHP script.

<?php
$host="localhost";
$database="toy";
$username="root";
$password="";

$connection=mysql_connect($host, $username, $password);
if(!$connection) {
	die("ERROR: Couldn't connect to the server.");
} else {
	$dbSelect=mysql_select_db($database);
	if(!$dbSelect) {
		die("ERROR: Couldn't select the database.");
	} else {
		$mysqldumpPath="/usr/bin/mysqldump";
		$backupFileName = "Desktop/toy-`date +%d-%m-%Y-%H:%M:%S`.sql";
		$createBackup = $mysqldumpPath." -u ".$username." -h ".$host." ".$database." > ".$backupFileName;
		exec($createBackup);
	}
}
?>

Executed code works fine in Linux terminal (toy-10-09-2009-16:15:49.sql) but when i run this PHP script it doesn't work. No error too.

What do you think i miss?

Thanks

On this line:

$backupFileName = "Desktop/toy-`date +%d-%m-%Y-%H:%M:%S`.sql";

You probably are going to need the absolute path to where the file needs to be stored.

No error. That's because you didn't echo the exec function. Line 18 should be as follows:

echo exec($createBackup);

Still doesn't work. Also no error for echo exec($createBackup); .

I changed the path to /usr/bin/mysqldump -u root -h localhost toy > /var/www/toy/toy-11-09-2009_10:03:41.sql but, still doesn't work in the script.
As i said before, it works fine in Ubuntu terminal. I also, tar it in the terminal without any problem with same path.

Thanks

In Ubuntu. ok. I found this problem when I first got a Virtual Private Server. The server had a different file structure than what my Ubuntu did. So try to browse around your server to make sure those are the locations for your servers Operating System. There seems to be two mains sorts of systems of sorting files from my experience.

Still doesn't work. Also no error for echo exec($createBackup); .

I changed the path to /usr/bin/mysqldump -u root -h localhost toy > /var/www/toy/toy-11-09-2009_10:03:41.sql but, still doesn't work in the script.
As i said before, it works fine in Ubuntu terminal. I also, tar it in the terminal without any problem with same path.

Thanks

Does PHP have write privileges to: /var/www/toy/

Does your root account on mysql have no password? Just wondering...

Hi digital,

I don't know how to check if PHP has R+W privilages !!!
No password for MySQL. I didn't set it.

Thanks

I use the class on the attachment to backup my CMS!

Hi digital,

I don't know how to check if PHP has R+W privilages !!!
No password for MySQL. I didn't set it.

Thanks

A simple check is to have PHP write a file to where ever you want to check.

eg:

$result = file_put_contents('/var/www/toy/test.txt', 'hi');
var_dump($result);

I have this info in 'User Settings'

Name-------Login name--------Home directory
veledrom ubuntu /home/ubuntu (this is active-bold and black coloured)
root root /root (this looks inactive-grey coloured)

This is what i did before.
ubuntu@ubuntu:~$ sudo chown -hR root /var/www/toy

I realise that i use root to login to mysql but in the terminal it shown ubuntu. Is ubuntu still root?

What can i do now? I bet it is something to do with users and permitions as you said, digital

I have this info in 'User Settings'

Name-------Login name--------Home directory
veledrom ubuntu /home/ubuntu (this is active-bold and black coloured)
root root /root (this looks inactive-grey coloured)

This is what i did before.
ubuntu@ubuntu:~$ sudo chown -hR root /var/www/toy

I realise that i use root to login to mysql but in the terminal it shown ubuntu. Is ubuntu still root?

What can i do now? I bet it is something to do with users and permitions as you said, digital

Did you try and see if PHP can write to /var/www/toy/?

The permissions on the file can be viewed with the "ls" command.

eg:

ls -l /var/www/toy/

You probably want to chown the /var/www/toy/ to the PHP user.

You can get the user PHP runs as, using:

<?php
var_dump(posix_getpwuid(posix_geteuid()));
?>

To test it temporarily, chmod the directory to 777 and see if you get the SQL dump.

chmod 777 /var/www/toy

You can change it back to a safer setting when you're done testing.

Hi digital,

I have done the tests. I give you the result i got. It may be useful info for you to direct me what to do.

Thanks

<?php
$result = file_put_contents('/var/www/toy/test.txt', 'hi');
var_dump($result);
?>

gives this error

Warning: file_put_contents(/var/www/toy/test.txt) [function.file-put-contents]: failed to open stream: Permission denied in /var/www/in.php on line 2
bool(false)
ls -l /var/www/toy/

prints info given below

-rw-r--r-- 1 ubuntu ubuntu   731 2009-09-16 15:56 index.php
-rw-r--r-- 1 ubuntu ubuntu   731 2009-09-16 15:56 index.php~
-rw-r--r-- 1 ubuntu ubuntu 20576 2009-09-11 11:23 toy-11-09-2009_11:10:46.sql
var_dump(posix_getpwuid(posix_geteuid()));

prints info given below

array(7) {
  ["name"]=>
  string(8) "www-data"
  ["passwd"]=>
  string(1) "x"
  ["uid"]=>
  int(33)
  ["gid"]=>
  int(33)
  ["gecos"]=>
  string(8) "www-data"
  ["dir"]=>
  string(8) "/var/www"
  ["shell"]=>
  string(7) "/bin/sh"
}

Hi digital,

I have done the tests. I give you the result i got. It may be useful info for you to direct me what to do.

Thanks

<?php
$result = file_put_contents('/var/www/toy/test.txt', 'hi');
var_dump($result);
?>

gives this error

Warning: file_put_contents(/var/www/toy/test.txt) [function.file-put-contents]: failed to open stream: Permission denied in /var/www/in.php on line 2
bool(false)

You can see here that PHP cannot write to that folder.

ls -l /var/www/toy/

prints info given below

-rw-r--r-- 1 ubuntu ubuntu   731 2009-09-16 15:56 index.php
-rw-r--r-- 1 ubuntu ubuntu   731 2009-09-16 15:56 index.php~
-rw-r--r-- 1 ubuntu ubuntu 20576 2009-09-11 11:23 toy-11-09-2009_11:10:46.sql

The files in the folder are owned by ubuntu and group ubuntu.

-rw-r--r-- means user can read and write, group can read, world (anyone else) can read.

ie:
the groups are: user, group and world.
the access flags are: (r)ead, (w)rite, e(x)ecute

User Group World
- rw- r-- r--

var_dump(posix_getpwuid(posix_geteuid()));

prints info given below

array(7) {
  ["name"]=>
  string(8) "www-data"
  ["passwd"]=>
  string(1) "x"
  ["uid"]=>
  int(33)
  ["gid"]=>
  int(33)
  ["gecos"]=>
  string(8) "www-data"
  ["dir"]=>
  string(8) "/var/www"
  ["shell"]=>
  string(7) "/bin/sh"
}

The PHP process belongs to the user "www-data".

The user "www-data" cannot write to the folder /var/www/toy/, since only the user "ubuntu" has write privileges to it.

You'll need to give www-data privileges to write to /var/www/toy/. The simplest way is it log in as "root" or "ubuntu", and chown /var/www/toy/ to the user www-data.

eg:

sudo chown www-data /var/www/toy/

Then you should be able to have PHP write to it.

Note:

The reason you can write to that folder from the shell is because you're logged in as "ubuntu". Thus when you start mysqladmin, the process belongs to "ubuntu". However, when mysqladmin is started from PHP by Apache, the process belongs to the Apache user "www-data".

Another method:

What you can also do, to overcome problems like this is to create a script that will do the backups for you, and have the setuid flag set for that script. You can even write it in PHP.

eg:

#!/usr/bin/php
<?php

exec("/usr/bin/mysqldump -u root -h localhost toy > /var/www/toy/toy-`date +%d-%m-%Y-%H:%M:%S`.sql", $stdout $exit_status);

// just echo the output
echo implode("\n", $stdout);

if (!$exit_status) { 
// something went wrong 
exit($exit_status);
}

// all ok
exit(0);

?>

Note the shebang is needed:

#!/usr/bin/php

This tells the shell to run it though the PHP interpreter.

Say you save the file as:

db-backup.php

You must then invoke it as:

./db-backup.php

Now you have to set the setuid flag on the script, so that when it is executed, the process belongs to the file owner, instead of the parent process that started it.

eg:

su ubuntu
chmod ug+s db-backup.php

This means when db-backup.php is executed, the process will be owned by ubuntu.

Now to run your script from PHP started by Apache:

<?php

exec("./db-backup.php");

?>

Hi digital,

sudo chown www-data /var/www/toy/

solved the problem. I can backup with php. However, i also want to be able to see the backup files stored in that folder. Now i cannot see because ubuntu has no permission.

Thanks in advance

Hi digital,

sudo chown www-data /var/www/toy/

solved the problem. I can backup with php. However, i also want to be able to see the backup files stored in that folder. Now i cannot see because ubuntu has no permission.

Thanks in advance

You cannot see the files when you do `ls` as ubuntu? What if you do it as root?

sudo ls /var/www/toy/

OK. Now i can see them all as ubuntu with this one.

ubuntu@ubuntu:~$ sudo chmod 777 /var/www/toy

Before i mark this as solved i just want to make sure that this command doesn't expose security issues!!!
What do you think digital?

Sorry i am new in linux but geting there slowly.

OK. Now i can see them all as ubuntu with this one.

ubuntu@ubuntu:~$ sudo chmod 777 /var/www/toy

Before i mark this as solved i just want to make sure that this command doesn't expose security issues!!!
What do you think digital?

Sorry i am new in linux but geting there slowly.

If you're on a shared system it is not a good idea to have world writable directories or files.

Best is to use the least privileges that does the job.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.