+ Reply to Thread
Page 2 of 2 FirstFirst 12
Results 11 to 17 of 17

Thread: Using PHP to backup and load Mysql DB

  1. #11
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: Using PHP to backup and load Mysql DB

    No problem

    If you have anything you'd specifically like to add/change let me know and I (or anyone else here for that matter) can help you out.
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  2. #12
    Gauze is offline x10Hosting Member Gauze is an unknown quantity at this point
    Join Date
    Jun 2008
    Posts
    12

    Re: Using PHP to backup and load Mysql DB

    Alright, I added some echos to the function to see what is happening.

    Code:
    function exportMysql($fileName, $databaseName) {
     mysql_select_db('INFORMATION_SCHEMA');
     $file = ''; // this will hold the information
     $query = "SELECT TABLE_NAME FROM TABLES WHERE TABLE_SCHEMA = '{$databaseName}' AND TABLE_TYPE = 'BASE TABLE'"; // fetch all table names
     
     
     
     
     echo $query."<br>"; //I added this
     
     
     
     
     
     $tableNames = mysql_query($query); // query DB
     
     if (!$tableNames) { // if there's an error
       exit(mysql_error());
     }
     mysql_select_db($databaseName); // select database to back up
     while($row = mysql_fetch_row($tableNames)) { // loop through all tables
       $query = "SELECT * FROM {$row[0]}"; // select everything in the table
       $tableData = mysql_query($query); //query DB
       if (!$tableData) { // if there's an error
         exit(mysql_error());
       }
       while($tableRow = mysql_fetch_row($tableData)) { // loop through all table rows
         $data = implode("', '", $tableRow);
         $data = "'{$data}'";
         $file .= "INSERT INTO {$row[0]} VALUES({$data})\n";
       }
     }
     
     
     
     
     echo $file;// I added this as well.
     
     
     
     
     file_put_contents($fileName, $file); // save file
    }
    I used the function this way:

    Code:
    include 'accessdb.php';//my php file that I use to log into my database using an account with full acccess
    exportMysql(backup_db.sql,robrow_db);
    The result was
    Code:
    SELECT TABLE_NAME FROM TABLES WHERE TABLE_SCHEMA = 'robrow_db' AND TABLE_TYPE = 'BASE TABLE'
    INSERT INTO arms VALUES('10', 'Pinkett', '2600', '13890', '480', '80', '110', '30', '10', 'Standard', '0', '0', '0') INSERT INTO arms VALUES('11', 'Mekhi', '2486', '13298', '450', '77', '102', '39', '14', 'Light', '40000', '0', '0') INSERT INTO arms VALUES('12', 'Frontliner', '3430', '15296', '481', '102', '155', '26', '7', 'Standard', '40000', '0', '0') INSERT INTO arms VALUES('13', 'Rushnik', '2702', '13295', '466', '83', '110', '28', '19', 'Light', '45000', '0', '0')
    ...etc etc...goes on for awhile...
    I guess if I ever lose the database...I'd have to reinitialize the tables?

    Additionally, do I need some break between each INSERT INTO line?

    I think I'll just copy the output (well, not the first line) into a .sql file or something.

    Because it doesn't seem to be saving the file anywhere.

  3. #13
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: Using PHP to backup and load Mysql DB

    You'll need to back up the structure of the tables separately. I'll try to work on this additional functionality in the morning. Also, when you restore the table, it won't check for duplicates, so you should DROP the table first, then create a new one from the definition. The importMysql() function will do the importing for you so you don't need a special break, though traditionally, a .sql file will use semicolons. It should save the file to wherever you specify in relation to where the script is. I verified that it worked on my computer, make sure your directory is writable.
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  4. #14
    Gauze is offline x10Hosting Member Gauze is an unknown quantity at this point
    Join Date
    Jun 2008
    Posts
    12

    Re: Using PHP to backup and load Mysql DB

    Alright, it works very conveniently.

  5. #15
    gomarc's Avatar
    gomarc is offline x10 Elder gomarc is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    USA
    Posts
    511

    Re: Using PHP to backup and load Mysql DB

    Gauze,

    Hopefully you got your database backup. But just in case you are still facing some problems, I would like to recommend using phpMyBackupPro, a very easy to use, free, web-based MySQL backup application, licensed under the GNU GPL.

    I just finish testing it on my free account and it works really great. (Email and scheduled backups not tested)

    This application does all the hard work, and I believe it’s exactly what you were looking for.

    Link to application: phpMyBackupPro

  6. #16
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: Using PHP to backup and load Mysql DB

    That's pretty neat. I'd use that instead :P
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  7. #17
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: Using PHP to backup and load Mysql DB

    Quote Originally Posted by garrettroyce View Post
    I'll try to work on this additional functionality in the morning. Also, when you restore the table, it won't check for duplicates, so you should DROP the table first, then create a new one from the definition.
    You can use 'IF NOT EXISTS' to check if the table exists and abort the import if it does.
    Last edited by xav0989; 06-29-2009 at 09:33 AM.
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

+ Reply to Thread
Page 2 of 2 FirstFirst 12

Similar Threads

  1. mysql y "Server Load 19.43"
    By cuatropo in forum Soporte
    Replies: 3
    Last Post: 09-03-2008, 10:30 PM
  2. MySQL Issues Here
    By Corey in forum Service Alerts
    Replies: 304
    Last Post: 01-06-2008, 09:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers