webcodebase.com Snippets database & Pastebin

MySQL Backup Class

Submitted on 04/11/2008
Authors Comment: Take backup of the MySQL Database using a quite advanced method, but it is proved to work on all linux and windows systems. Unlike mysqldump which only works on *NIX systems.
How to use:
*********** @ How to use this tool @ ***********
At the start of your script add this line:
* $dump = new sqlbackup;

Config part:
* $dump->config("Database Server", "Database Username" , "Database Password", "Database");

Example of config:
* $dump->config("localhost", "root", "pass", "db");

To take the backup add this line:
* $dump->backup("filename","sql_backup_dir");

Change filename to the name you want the file to be named.
* Example:
* $dump->backup("backup_102.sql"); // Default: backup_dbname_Year_Month_Day
Download Snippet:
Snippet:

  1. <?
  2. /*
  3. * Description: MySQL Backup tool
  4. * Author: Arne C. Blystad
  5. * Copyright: 2008 WebCodebase
  6. * License: LICENSE FREE - Use as you want too
  7.  
  8.  
  9. *********** @ How to use this tool @ ***********
  10. * At the start of your script add this line:
  11. * $dump = new sqlbackup;
  12.  
  13. * Config part:
  14. * $dump->config("Database Server", "Database Username" , "Database Password", "Database");
  15.  
  16. * Example of config:
  17. * $dump->config("localhost", "root", "xfx", "cds");
  18.  
  19. * To take the backup add this line:
  20. * $dump->backup("filename","sql_backup_dir");
  21.  
  22. * Change filename to the name you want the file to be,
  23. * Example:
  24. * $dump->backup("backup_102.sql","dir");
  25.  
  26. * Warning: This file must be placed before any text, because it modifies your header info,
  27. * To download the SQL file
  28.  
  29. */
  30. // ====================
  31. ## Class sqlbackup // Create SQL Backup and give the user a chance to download it
  32. // ====================
  33. class sqlbackup
  34. {
  35.         //=============================//
  36.         // Start Webcodebase Backup Tool
  37.         //=============================//
  38.        
  39.         // Define some variables.
  40.         var $nocomments;
  41.         var $db_serv;  
  42.         var $db;                       
  43.         var $filename;  
  44.         //================================================================================//
  45.         // Function: Config($db_serv, $user, $password, $db)                                           
  46.         // Description: Do all the basic configuration for the connection to the database
  47.         //================================================================================//
  48.         function config( $db_serv, $user, $password, $db)
  49.         {      
  50.                 mysql_connect($db_serv, $user, $password);             
  51.                 mysql_select_db($db);
  52.                 $this->db=$db;
  53.                 $this->db_serv=$db_serv;
  54.         }
  55.         //======================================//
  56.         // Function: db_write($fp,$val)
  57.         // Description: Write text to textfile
  58.         //======================================//
  59.         function db_write($fp, $val){
  60.         echo $val;
  61.         }
  62.         //======================================//
  63.         // Function: write_header($filename)  
  64.         // Description: Sets the content header
  65.         //======================================//
  66.         function write_header($filename){
  67.                 header( "Content-type: application/force-download");  
  68.         header( "Content-Disposition: inline; filename=\"" . $filename . "\"");
  69.                 header( "Expires: Mon, 1 Jul 2010 01:00:00 GMT");
  70.         header( "Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0");
  71.         }
  72.         //==========================================================================//
  73.         // Function: backup($b_file)                                                             
  74.         // Description: Gets the SQL information and writes the backup to text     
  75.         //                              Then gets the user to download the text file, if $b_file
  76.         //                              is empty it will write to a file called        
  77.         //                              backup_DBNAME_TODAYSDATE.sql                   
  78.         //==========================================================================//
  79.         function backup($b_file="")
  80.         {
  81.                         # Not working at this moment # $this->nocomment=$nocomment;
  82.                         if($b_file){                           
  83.                                 $this->filename=$this->bdir.$b_file;
  84.                         }else{                 
  85.                                 $this->filename = $this->bdir."backup_".$this->db."_".date("Y_m_d__G_i").".sql";
  86.                         }
  87.                         $this->write_header($this->filename);
  88.                         $this->db_write($fp,"-- WebCodebase.com SQL Backup Tool \n");
  89.                         $this->db_write($fm,"-- Copyright: 2008 Webcodebase \n");
  90.                         $this->db_write($fp,"--\n");
  91.                         $this->db_write($fp,"-- Host : $this->db_serv     Database :  $this->db\n");
  92.                         $this->db_write($fp,"-- ---------------------------------------------\n");
  93.                         $ltable = mysql_list_tables($this->db);
  94.                         $nb_row = mysql_num_rows($ltable);
  95.                                        
  96.                         $i = 0;
  97.                         while ($i < $nb_row)
  98.                         {       $tablename = mysql_tablename($ltable, $i);
  99.                                         if($this->nocomment!=1){
  100.                                         $this->db_write($fp,"\n");
  101.                                         $this->db_write($fp,"\n");
  102.                                         $this->db_write($fp,"--\n");
  103.                                         $this->db_write($fp,"-- Table structure for table '$tablename' \n");
  104.                                         $this->db_write($fp,"--\n");
  105.                                         $this->db_write($fp,"\n");
  106.                                 }
  107.                                 $this->db_write($fp,"DROP TABLE IF EXISTS `$tablename`;\n");
  108.                          
  109.                                 $query = "SHOW CREATE TABLE $tablename";
  110.                                 $tbcreate = mysql_query($query);
  111.                                 $row = mysql_fetch_array($tbcreate);
  112.                                 $create = $row[1].";";
  113.                                 $this->db_write($fp,"$create\n\n");
  114.                                         if($this->nocomment!=1){
  115.                                         $this->db_write($fp,"--\n");
  116.                                         $this->db_write($fp,"-- Dumping data for table '$tablename' \n");
  117.                                         $this->db_write($fp,"--\n");
  118.                                         $this->db_write($fp,"\n");
  119.                                 }
  120.                                 $query = "SELECT * FROM $tablename";
  121.                                 $datacreate = mysql_query($query);
  122.                                 if (mysql_num_rows($datacreate) > 0)
  123.                                 {
  124.                                         $qinsert = "LOCK TABLES $tablename WRITE; \n";
  125.                                         $qinsert .= "INSERT INTO `$tablename` values \n  ";
  126.                                        
  127.                                         while($row12 = mysql_fetch_assoc($datacreate))
  128.                                         {          //set_time_limit(30);                                                // In case your server is in "SAFE MODE" uncomment this line to set excute time limit //
  129.                                                    $row12 = array_map(array($this, 'sep_sql'), $row12);
  130.                                                    $data = implode(",",$row12);                
  131.                                                    $data = "$qinsert($data)";                          
  132.                                                    $this->db_write($fp,"$data\n");
  133.                                                    $qinsert=", ";                                                      
  134.                                         }
  135.                                         $this->db_write($fp,";\n");
  136.                                         $this->db_write($fp,"UNLOCK TABLES; \n");
  137.                                         $this->db_write($fp,"\n");
  138.                                 }else{                                                         
  139.                                         if($this->nocomment!=1){
  140.                                                 $this->db_write($fp,"--\n");
  141.                                                 $this->db_write($fp,"-- table '$tablename' empty \n");
  142.                                                 $this->db_write($fp,"--\n");
  143.                                                 $this->db_write($fp,"\n");
  144.                                         }
  145.  
  146.                         }
  147.                   $i++;
  148.                   }  
  149.         }
  150.         //==================================================================//
  151.         // Function: sql_sql($tbl)                                             
  152.         // Description: Adds ' to start and to end of each value in table.
  153.         //==================================================================//
  154.         function sep_sql($tbl)
  155.         {
  156.                 $tbl=mysql_escape_string($tbl);        
  157.                 if(is_numeric($tbl)){ return $tbl;}    
  158.                 if(!$tbl){return "NULL";}                      
  159.                 return "'".$tbl."'";
  160.         }
  161.         //===============================//
  162.         //                  EOS
  163.         //===============================//
  164. }
  165. ?>

 

Comments:

  1. .izkkeug | December 11, 2008 at 18:47

    z6tcah <a href="http://qjqwbmaibcli.com/">qjqwbmaibcli</a>, [url=http://pqmpfpebyzmc.com/]pqmpfpebyzmc[/url], [link=http://tfzrwjlvfcft.com/]tfzrwjlvfcft[/link], http://bfwqodchyqym.com/

  2. .yggdehrqhmh | December 11, 2008 at 18:47

    5vDd2b <a href="http://glfqscitwkts.com/">glfqscitwkts</a>, [url=http://ktsbkpienfps.com/]ktsbkpienfps[/url], [link=http://xwebhclqoxsa.com/]xwebhclqoxsa[/link], http://gdfyuttgocox.com/

  3. .hedxiwwvkca | December 11, 2008 at 18:47

    dfjqOX <a href="http://hlgzxswhplcn.com/">hlgzxswhplcn</a>, [url=http://mqxpqrxknujq.com/]mqxpqrxknujq[/url], [link=http://httzkjoulxif.com/]httzkjoulxif[/link], http://llahtusqxrde.com/

  4. .mkonrfj | December 11, 2008 at 18:47

    ZlEY16 <a href="http://opbftqxwuqgz.com/">opbftqxwuqgz</a>, [url=http://rxyehagyvdku.com/]rxyehagyvdku[/url], [link=http://lifuahfkrdlb.com/]lifuahfkrdlb[/link], http://wcepswzfqzsg.com/

  5. .gkpjsxbhq | December 11, 2008 at 18:47

    wH2hxO <a href="http://szigyrcgecet.com/">szigyrcgecet</a>, [url=http://mdpnfvwobbhx.com/]mdpnfvwobbhx[/url], [link=http://qvbrvimkmtbg.com/]qvbrvimkmtbg[/link], http://uwnftlbwizbi.com/

  6. .cngbkcpbjv | December 11, 2008 at 18:47

    w35tHd <a href="http://ocnuoljoymij.com/">ocnuoljoymij</a>, [url=http://zgqwzmmgdsfq.com/]zgqwzmmgdsfq[/url], [link=http://ksemczxlvygo.com/]ksemczxlvygo[/link], http://sxetwdxuygge.com/


New Comment:
Name:
Comment:








Latest News

  • 30/12/09
    Webcodebase is BACK & New updates.
    Hello, Because of a jackass of a friend the main files for Webcodebase was deleted. I Thought everything was lost until i found a backup folder on my old linux box, so i restored WCB Back to its original state. The staff will continue posting snippets and fighting spam. We will also start releasing tools that web masters can use to check their scripts for vulnerabilities. These scripts will have special stars next to them and the "STAFF" tag. Please use these on your own systems only, we are NOT responsible for any damage they may do. We release them purely for educational purposes only! Thanks, Head of WCB, Arne AKA Cypher....
    [Read More]