PHP script to backup mysql

Thread Solved

Join Date: Apr 2008
Posts: 490
Reputation: veledrom is an unknown quantity at this point 
Solved Threads: 0
veledrom veledrom is offline Offline
Posting Pro in Training

PHP script to backup mysql

 
0
  #1
Sep 10th, 2009
Hi,

I am trying to backup one of my Mysql database with a PHP script.
  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: .
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: PHP script to backup mysql

 
0
  #2
Sep 10th, 2009
On this line:
  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.
Google is your friend.

Use [code] tags.

If you have found a solution to your problem, please mark the thread as SOLVED.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,429
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 132
cwarn23's Avatar
cwarn23 cwarn23 is offline Offline
Nearly a Posting Virtuoso

Re: PHP script to backup mysql

 
0
  #3
Sep 11th, 2009
No error. That's because you didn't echo the exec function. Line 18 should be as follows:
  1. echo exec($createBackup);
Try not to bump 10 year old threads as it can be really annoying.
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 490
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
  #4
Sep 11th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,429
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 132
cwarn23's Avatar
cwarn23 cwarn23 is offline Offline
Nearly a Posting Virtuoso

Re: PHP script to backup mysql

 
0
  #5
Sep 11th, 2009
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.
Try not to bump 10 year old threads as it can be really annoying.
Like php then read my website at http://syntax.cwarn23.net/
Star-Trek-Atlantis - now that's what I call a movie ^_^
My favourite PC. - MacGyver Fan
Bad english note: dis-iz-2b4u
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,073
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
  #6
Sep 11th, 2009
Originally Posted by veledrom View Post
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...
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: 490
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
  #7
Sep 15th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 70
Reputation: smartness is an unknown quantity at this point 
Solved Threads: 10
smartness's Avatar
smartness smartness is offline Offline
Junior Poster in Training

Re: PHP script to backup mysql

 
0
  #8
Sep 15th, 2009
I use the class on the attachment to backup my CMS!
Attached Files
File Type: zip DB-Backup-Class.zip (3.6 KB, 3 views)
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,073
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
  #9
Sep 15th, 2009
Originally Posted by veledrom View Post
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:

  1. $result = file_put_contents('/var/www/toy/test.txt', 'hi');
  2. var_dump($result);
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: 490
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
  #10
Sep 16th, 2009
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
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