selimppc

Database Backup in PHP

Dec 14th, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.46 KB | None | 0 0
  1. public function db_back_up(){
  2.         $query = $this->database_backup('localhost', 'root', 'root1234', 'affifact');
  3.         exit("Success!");
  4.     }
  5.  
  6.  
  7.     protected static function database_backup($host, $user, $pass, $name, $tables = '*')
  8.     {
  9.             $link = mysqli_connect($host, $user, $pass);
  10.             mysqli_select_db($link, $name);
  11.             if ($tables == '*')
  12.             {
  13.                 $tables = [];
  14.                 $result = mysqli_query($link, 'SHOW TABLES');
  15.                 while ($row = mysqli_fetch_row($result))
  16.                 {
  17.                     $tables[] = $row[0];
  18.                 }
  19.             } else
  20.             {
  21.                 $tables = is_array($tables) ? $tables : explode(',', $tables);
  22.             }
  23.  
  24.             $return = null;
  25.             foreach ($tables as $table)
  26.             {
  27.                 $result = mysqli_query($link, 'SELECT * FROM ' . $table);
  28.                 $num_fields = mysqli_num_fields($result);
  29.  
  30.                 $return .= 'DROP TABLE ' . $table . ';';
  31.                 $row2 = mysqli_fetch_row(mysqli_query($link, 'SHOW CREATE TABLE ' . $table));
  32.                 $return .= "\r\n" . $row2[1] . ";\r\n";
  33.  
  34.                 for ($i = 0; $i < $num_fields; $i ++)
  35.                 {
  36.                     while ($row = mysqli_fetch_row($result))
  37.                     {
  38.                         $return .= 'INSERT INTO ' . $table . ' VALUES(';
  39.                         for ($j = 0; $j < $num_fields; $j ++)
  40.                         {
  41.                             $row[$j] = addslashes($row[$j]);
  42.                             $row[$j] = mb_ereg_replace("n", "n", $row[$j]);
  43.                             if (isset($row[$j]))
  44.                             {
  45.                                 $return .= '"' . $row[$j] . '"';
  46.                             } else
  47.                             {
  48.                                 $return .= '""';
  49.                             }
  50.                             if ($j < ($num_fields - 1))
  51.                             {
  52.                                 $return .= ',';
  53.                             }
  54.                         }
  55.                         $return .= ");\r\n";
  56.                     }
  57.                 }
  58.                 $return .= "\r\n";
  59.  
  60.  
  61.  
  62.             }
  63.  
  64.             $file_name = 'db-backup-' . date("d-m-y-h-i-s-A") . '.sql';
  65.             $handle = fopen("/tmp/db_backup/" . $file_name, 'w+');
  66.             fwrite($handle, $return);
  67.             fclose($handle);
  68.     }
Advertisement
Add Comment
Please, Sign In to add comment