Results 1 to 11 of 11

Thread: Backup a Mysql database (php 5.3.0)

Threaded View

  1. #1

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Backup a Mysql database (php 5.3.0)

    Hi,

    This is a php script I made because I was curious how the automated backup of the mysql databases was achieved.

    PHP Code:
    <?php
    //Name of host you want to use
    $db_host "localhost";

    //Name of database you want to use
    $db_name "students";
    //User
    $db_user "root";
    //Password
    $db_pass "";
    //Name of table or tables
    $table "students";

    mysql_connect($db_host,$db_user,$db_pass);

    mysql_select_db($db_name) or die("Unable to select database.");

    function 
    datadump ($table) {
         
    $result ="";
    $row="";

        
    $result .= "# Dump of $table \n";

        
    $result .= "# Dump DATE : " date("d-M-Y") ."\n\n";
     
        
    $query mysql_query("select * from $table");

        
    $num_fields = @mysql_num_fields($query);
    //Retrieve the data from the original database 
        
    for ($i =0$i <$num_fields$i++) {
          
    $result .= "INSERT INTO ".$table." VALUES(";
          
    $data mysql_query("SELECT * FROM $table");
    while(
    $info mysql_fetch_array$data,MYSQL_ASSOC )){
          
    $comma_separated implode(",",  $info);
          
    //Insert the data from the original array in to sql  statement
               
    if ($i<($num_fields)) $result .= "$comma_separated";
             
          
    $result .= ");\n";
         }
         return 
    $result "\n\n\n";
         }
    }


    $table1 datadump ($table); //Add more datadumps if needed


    $content $table1// Add additional datadumps to the string 

    //Creates an .sql file for download to your computer 
    $file_name "MySQL_Database_Backup.sql";

    Header("Content-type: application/octet-stream");

    Header("Content-Disposition: attachment; filename=$file_name");

    echo 
    $content;

    exit;

    ?>
    If you have any comments or suggestions on how I can improve the code please post them. By the way, I'm hoping to create a totally automated back script sometime.

    Edit:

    Forgot to mention the php code version which is 5.3.0.



    Nightwalker
    Last edited by Nightwalker83; Jan 14th, 2010 at 11:48 PM. Reason: Fixed spelling
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width