| | |
PHP script to backup mysql
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
•
•
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
The permissions on the file can be viewed with the "ls" command.
eg:
PHP Syntax (Toggle Plain Text)
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)
<?php var_dump(posix_getpwuid(posix_geteuid())); ?>
To test it temporarily, chmod the directory to 777 and see if you get the SQL dump.
PHP Syntax (Toggle Plain Text)
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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
•
•
Join Date: Apr 2008
Posts: 495
Reputation:
Solved Threads: 0
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
gives this error
prints info given below
prints info given below
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)
<?php $result = file_put_contents('/var/www/toy/test.txt', 'hi'); var_dump($result); ?>
PHP Syntax (Toggle Plain Text)
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)
PHP Syntax (Toggle Plain Text)
ls -l /var/www/toy/
PHP Syntax (Toggle Plain Text)
-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
PHP Syntax (Toggle Plain Text)
var_dump(posix_getpwuid(posix_geteuid()));
PHP Syntax (Toggle Plain Text)
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" }
Last edited by veledrom; Sep 17th, 2009 at 5:39 am. Reason: .
•
•
•
•
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
gives this errorPHP Syntax (Toggle Plain Text)
<?php $result = file_put_contents('/var/www/toy/test.txt', 'hi'); var_dump($result); ?>
PHP Syntax (Toggle Plain Text)
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)
•
•
•
•
prints info given belowPHP Syntax (Toggle Plain Text)
ls -l /var/www/toy/
PHP Syntax (Toggle Plain Text)
-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
-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--
•
•
•
•
prints info given belowPHP Syntax (Toggle Plain Text)
var_dump(posix_getpwuid(posix_geteuid()));
PHP Syntax (Toggle Plain Text)
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 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)
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)
#!/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:
PHP Syntax (Toggle Plain Text)
#!/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)
db-backup.php
You must then invoke it as:
PHP Syntax (Toggle Plain Text)
./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)
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 Syntax (Toggle Plain Text)
<?php exec("./db-backup.php"); ?>
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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
•
•
Join Date: Apr 2008
Posts: 495
Reputation:
Solved Threads: 0
Hi digital,
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
PHP Syntax (Toggle Plain Text)
sudo chown www-data /var/www/toy/
Thanks in advance
•
•
•
•
Hi digital,
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.PHP Syntax (Toggle Plain Text)
sudo chown www-data /var/www/toy/
Thanks in advance
PHP Syntax (Toggle Plain Text)
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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
•
•
Join Date: Apr 2008
Posts: 495
Reputation:
Solved Threads: 0
OK. Now i can see them all as ubuntu with this one.
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.
PHP Syntax (Toggle Plain Text)
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: .
•
•
•
•
OK. Now i can see them all as ubuntu with this one.
PHP Syntax (Toggle Plain Text)
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.
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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
![]() |
Similar Threads
- Help with php script to export mysql data and import data (PHP)
- question about connecting odbc to sql through php script (PHP)
- Please school me in PHP script installation. (MySQL)
- how to make member expire after so many days with php script (PHP)
- How many clients can access a PHP script at a time from a MySQL server? (MySQL)
- How many clients can access a PHP script at a time? (PHP)
Other Threads in the PHP Forum
- Previous Thread: uploading files with php
- Next Thread: PHP Editor Like Wordpress
| Thread Tools | Search this Thread |
apache api array beginner binary body broken buttons cakephp checkbox class cms code cron curl database date date/time display dynamic ebooks echo email error file files folder form forms function functions global google href htaccess html image include insert ip javascript joomla limit link list login mail mediawiki menu mlm msqli_multi_query multiple mycodeisbad mysql number oop parameter paypal pdf php phpincludeissue problem query radio random recourse recursion regex remote script search seo server sessions sms source sp space speed sql static subdomain syntax system table tag tutorial update upload url validator variable vbulletin video web webdesign white wordpress xml youtube






