943,529 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 1686
  • PHP RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Sep 16th, 2009
0

Re: PHP script to backup mysql

Click to Expand / Collapse  Quote originally posted by veledrom ...
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:

PHP Syntax (Toggle Plain Text)
  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:

php Syntax (Toggle Plain Text)
  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.

PHP Syntax (Toggle Plain Text)
  1. chmod 777 /var/www/toy

You can change it back to a safer setting when you're done testing.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Sep 17th, 2009
0

Re: PHP script to backup mysql

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

PHP Syntax (Toggle Plain Text)
  1. ls -l /var/www/toy/
prints info given below
PHP Syntax (Toggle Plain Text)
  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

PHP Syntax (Toggle Plain Text)
  1. var_dump(posix_getpwuid(posix_geteuid()));
prints info given below
PHP Syntax (Toggle Plain Text)
  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: .
Reputation Points: 38
Solved Threads: 0
Master Poster
veledrom is offline Offline
724 posts
since Apr 2008
Sep 17th, 2009
0

Re: PHP script to backup mysql

Click to Expand / Collapse  Quote originally posted by veledrom ...
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 Syntax (Toggle Plain Text)
  1. <?php
  2. $result = file_put_contents('/var/www/toy/test.txt', 'hi');
  3. var_dump($result);
  4. ?>
gives this error
PHP Syntax (Toggle Plain Text)
  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.

Click to Expand / Collapse  Quote originally posted by veledrom ...

PHP Syntax (Toggle Plain Text)
  1. ls -l /var/www/toy/
prints info given below
PHP Syntax (Toggle Plain Text)
  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--

Click to Expand / Collapse  Quote originally posted by veledrom ...

PHP Syntax (Toggle Plain Text)
  1. var_dump(posix_getpwuid(posix_geteuid()));
prints info given below
PHP Syntax (Toggle Plain Text)
  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:

PHP Syntax (Toggle Plain Text)
  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:

php Syntax (Toggle Plain Text)
  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:

PHP Syntax (Toggle Plain Text)
  1. #!/usr/bin/php

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

Say you save the file as:

PHP Syntax (Toggle Plain Text)
  1. db-backup.php

You must then invoke it as:

PHP Syntax (Toggle Plain Text)
  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:

PHP Syntax (Toggle Plain Text)
  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:

php Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. exec("./db-backup.php");
  4.  
  5. ?>
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Sep 17th, 2009
0

Re: PHP script to backup mysql

Hi digital,

PHP Syntax (Toggle Plain Text)
  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
Reputation Points: 38
Solved Threads: 0
Master Poster
veledrom is offline Offline
724 posts
since Apr 2008
Sep 17th, 2009
0

Re: PHP script to backup mysql

Click to Expand / Collapse  Quote originally posted by veledrom ...
Hi digital,

PHP Syntax (Toggle Plain Text)
  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?

PHP Syntax (Toggle Plain Text)
  1. sudo ls /var/www/toy/
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Sep 18th, 2009
0

Re: PHP script to backup mysql

OK. Now i can see them all as ubuntu with this one.
PHP Syntax (Toggle Plain Text)
  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: .
Reputation Points: 38
Solved Threads: 0
Master Poster
veledrom is offline Offline
724 posts
since Apr 2008
Sep 19th, 2009
0

Re: PHP script to backup mysql

Click to Expand / Collapse  Quote originally posted by veledrom ...
OK. Now i can see them all as ubuntu with this one.
PHP Syntax (Toggle Plain Text)
  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.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: uploading files with php
Next Thread in PHP Forum Timeline: PHP Editor Like Wordpress





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC