PHP script to backup mysql

Thread Solved

Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: PHP script to backup mysql

 
0
  #11
Sep 16th, 2009
Originally Posted by veledrom View Post
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:

  1. 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:

  1. <?php
  2. var_dump(posix_getpwuid(posix_geteuid()));
  3. ?>

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

  1. chmod 777 /var/www/toy

You can change it back to a safer setting when you're done testing.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 495
Reputation: veledrom is an unknown quantity at this point 
Solved Threads: 0
veledrom veledrom is offline Offline
Posting Pro in Training

Re: PHP script to backup mysql

 
0
  #12
Sep 17th, 2009
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

  1. <?php
  2. $result = file_put_contents('/var/www/toy/test.txt', 'hi');
  3. var_dump($result);
  4. ?>
gives this error
  1. 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
  2. bool(false)

  1. ls -l /var/www/toy/
prints info given below
  1. -rw-r--r-- 1 ubuntu ubuntu 731 2009-09-16 15:56 index.php
  2. -rw-r--r-- 1 ubuntu ubuntu 731 2009-09-16 15:56 index.php~
  3. -rw-r--r-- 1 ubuntu ubuntu 20576 2009-09-11 11:23 toy-11-09-2009_11:10:46.sql

  1. var_dump(posix_getpwuid(posix_geteuid()));
prints info given below
  1. array(7) {
  2. ["name"]=>
  3. string(8) "www-data"
  4. ["passwd"]=>
  5. string(1) "x"
  6. ["uid"]=>
  7. int(33)
  8. ["gid"]=>
  9. int(33)
  10. ["gecos"]=>
  11. string(8) "www-data"
  12. ["dir"]=>
  13. string(8) "/var/www"
  14. ["shell"]=>
  15. string(7) "/bin/sh"
  16. }
Last edited by veledrom; Sep 17th, 2009 at 5:39 am. Reason: .
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: PHP script to backup mysql

 
0
  #13
Sep 17th, 2009
Originally Posted by veledrom View Post
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

  1. <?php
  2. $result = file_put_contents('/var/www/toy/test.txt', 'hi');
  3. var_dump($result);
  4. ?>
gives this error
  1. 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
  2. bool(false)

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

Originally Posted by veledrom View Post

  1. ls -l /var/www/toy/
prints info given below
  1. -rw-r--r-- 1 ubuntu ubuntu 731 2009-09-16 15:56 index.php
  2. -rw-r--r-- 1 ubuntu ubuntu 731 2009-09-16 15:56 index.php~
  3. -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--

Originally Posted by veledrom View Post

  1. var_dump(posix_getpwuid(posix_geteuid()));
prints info given below
  1. array(7) {
  2. ["name"]=>
  3. string(8) "www-data"
  4. ["passwd"]=>
  5. string(1) "x"
  6. ["uid"]=>
  7. int(33)
  8. ["gid"]=>
  9. int(33)
  10. ["gecos"]=>
  11. string(8) "www-data"
  12. ["dir"]=>
  13. string(8) "/var/www"
  14. ["shell"]=>
  15. string(7) "/bin/sh"
  16. }
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:

  1. 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:

  1. #!/usr/bin/php
  2. <?php
  3.  
  4. exec("/usr/bin/mysqldump -u root -h localhost toy > /var/www/toy/toy-`date +%d-%m-%Y-%H:%M:%S`.sql", $stdout $exit_status);
  5.  
  6. // just echo the output
  7. echo implode("\n", $stdout);
  8.  
  9. if (!$exit_status) {
  10. // something went wrong
  11. exit($exit_status);
  12. }
  13.  
  14. // all ok
  15. exit(0);
  16.  
  17. ?>

Note the shebang is needed:

  1. #!/usr/bin/php

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

Say you save the file as:

  1. db-backup.php

You must then invoke it as:

  1. ./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:

  1. su ubuntu
  2. 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:

  1. <?php
  2.  
  3. exec("./db-backup.php");
  4.  
  5. ?>
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 495
Reputation: veledrom is an unknown quantity at this point 
Solved Threads: 0
veledrom veledrom is offline Offline
Posting Pro in Training

Re: PHP script to backup mysql

 
0
  #14
Sep 17th, 2009
Hi digital,

  1. 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
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: PHP script to backup mysql

 
0
  #15
Sep 17th, 2009
Originally Posted by veledrom View Post
Hi digital,

  1. 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?

  1. sudo ls /var/www/toy/
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 495
Reputation: veledrom is an unknown quantity at this point 
Solved Threads: 0
veledrom veledrom is offline Offline
Posting Pro in Training

Re: PHP script to backup mysql

 
0
  #16
Sep 18th, 2009
OK. Now i can see them all as ubuntu with this one.
  1. 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.
Last edited by veledrom; Sep 18th, 2009 at 3:57 pm. Reason: .
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: PHP script to backup mysql

 
0
  #17
Sep 19th, 2009
Originally Posted by veledrom View Post
OK. Now i can see them all as ubuntu with this one.
  1. 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.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC