943,558 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 1686
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 10th, 2009
0

PHP script to backup mysql

Expand Post »
Hi,

I am trying to backup one of my Mysql database with a PHP script.
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. $host="localhost";
  3. $database="toy";
  4. $username="root";
  5. $password="";
  6.  
  7. $connection=mysql_connect($host, $username, $password);
  8. if(!$connection) {
  9. die("ERROR: Couldn't connect to the server.");
  10. } else {
  11. $dbSelect=mysql_select_db($database);
  12. if(!$dbSelect) {
  13. die("ERROR: Couldn't select the database.");
  14. } else {
  15. $mysqldumpPath="/usr/bin/mysqldump";
  16. $backupFileName = "Desktop/toy-`date +%d-%m-%Y-%H:%M:%S`.sql";
  17. $createBackup = $mysqldumpPath." -u ".$username." -h ".$host." ".$database." > ".$backupFileName;
  18. exec($createBackup);
  19. }
  20. }
  21. ?>
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
Last edited by veledrom; Sep 10th, 2009 at 12:34 pm. Reason: .
Reputation Points: 38
Solved Threads: 0
Master Poster
veledrom is offline Offline
724 posts
since Apr 2008
Sep 10th, 2009
0

Re: PHP script to backup mysql

On this line:
PHP Syntax (Toggle Plain Text)
  1. $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.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Sep 11th, 2009
0

Re: PHP script to backup mysql

No error. That's because you didn't echo the exec function. Line 18 should be as follows:
php Syntax (Toggle Plain Text)
  1. echo exec($createBackup);
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
Sep 11th, 2009
0

Re: PHP script to backup mysql

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
Reputation Points: 38
Solved Threads: 0
Master Poster
veledrom is offline Offline
724 posts
since Apr 2008
Sep 11th, 2009
0

Re: PHP script to backup mysql

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.
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
Sep 11th, 2009
0

Re: PHP script to backup mysql

Click to Expand / Collapse  Quote originally posted by veledrom ...
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...
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Sep 15th, 2009
0

Re: PHP script to backup mysql

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
Reputation Points: 38
Solved Threads: 0
Master Poster
veledrom is offline Offline
724 posts
since Apr 2008
Sep 15th, 2009
0

Re: PHP script to backup mysql

I use the class on the attachment to backup my CMS!
Attached Files
File Type: zip DB-Backup-Class.zip (3.6 KB, 59 views)
Reputation Points: 7
Solved Threads: 14
Junior Poster
smartness is offline Offline
103 posts
since Aug 2007
Sep 15th, 2009
0

Re: PHP script to backup mysql

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

PHP Syntax (Toggle Plain Text)
  1. $result = file_put_contents('/var/www/toy/test.txt', 'hi');
  2. var_dump($result);
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Sep 16th, 2009
0

Re: PHP script to backup mysql

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
Reputation Points: 38
Solved Threads: 0
Master Poster
veledrom is offline Offline
724 posts
since Apr 2008

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