Advertisement
Guest User

Non incremental MySQL backup script

a guest
Jul 1st, 2014
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.47 KB | None | 0 0
  1. <?php
  2. /***************************************************************************
  3.  *   copyright: (C) 2008 Sawyerr Ken
  4.  *   developer site: http://www.sawyerrken.net/
  5.  *   description: non-incremental mysql database backup script
  6.  *   written: Feb 2008
  7.  ***************************************************************************/
  8.  
  9. /***************************************************************************
  10.  *   This program is free software; you can redistribute it and/or modify
  11.  *   it under the terms of the GNU General Public License as published by
  12.  *   the Free Software Foundation; either version 2 of the License, or
  13.  *   (at your option) any later version. Although none of the code may be
  14.  *   sold. If you have been sold this script, get a refund.
  15.  ***************************************************************************/
  16. //change the  variables here to fit your connection settings
  17. $host = "";
  18. $username = "";
  19. $password = "";
  20. $database = "";
  21.  
  22.  
  23. //do not change anything from here
  24. function backup_tables($host,$user,$pass,$name,$tables = '*')
  25. {
  26.  
  27.   $link = mysqli_connect($host,$user,$pass);
  28.   mysqli_select_db($link,$name);
  29.   if($tables == '*')
  30.   {
  31.     $tables = array();
  32.     $result = mysqli_query($link,'SHOW TABLES');
  33.     while($row = mysqli_fetch_row($result))
  34.     {
  35.       $tables[] = $row[0];
  36.     }
  37.   }
  38.   else
  39.   {
  40.     $tables = is_array($tables) ? $tables : explode(',',$tables);
  41.   }
  42.   foreach($tables as $table)
  43.   {
  44.     $result = mysqli_query($link,'SELECT * FROM '.$table);
  45.     $num_fields = @mysqli_num_fields($result);
  46.    
  47.     $return.= 'DROP TABLE '.$table.';';
  48.     $row2 = @mysqli_fetch_row(mysqli_query($link,'SHOW CREATE TABLE '.$table));
  49.     $return.= "\n\n".$row2[1].";\n\n";
  50.    
  51.     for ($i = 0; $i < $num_fields; $i++)
  52.     {
  53.       while($row = mysqli_fetch_row($result))
  54.       {
  55.         $return.= 'INSERT INTO '.$table.' VALUES(';
  56.         for($j=0; $j<$num_fields; $j++)
  57.         {
  58.           $row[$j] = addslashes($row[$j]);
  59.           $row[$j] = ereg_replace("\n","\\n",$row[$j]);
  60.           if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
  61.           if ($j<($num_fields-1)) { $return.= ','; }
  62.         }
  63.         $return.= ");\n";
  64.       }
  65.     }
  66.     $return.="\n\n\n";
  67.   }
  68. $time=date("Y-m-d:H:i:s");
  69.   $handle = fopen('../../hourly_backups/db-backup-'.$time.'-'.$name.'.sql','w+');
  70.   fwrite($handle,$return);
  71.   fclose($handle);
  72. }
  73.  
  74.  
  75. backup_tables($host,$username,$password,$database);
  76. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement