J2TEAM Security: A must-have extension for Chrome users. Install now!

Share code Backup dữ liệu MySQL trên PHP

Share code Backup dữ liệu MySQL trên PHP | Juno_okyo's Blog
4 min read
Chào mọi người, để sao lưu database MySQL chúng ta có rất nhiều cách: Bạn có thể dùng PHPMyAdmin, một mã nguồn mở để sao lưu phục hồi, hoặc bạn có thể sử dụng tool của MySQL... Tuy nhiên tôi đang viết một website và tôi muốn tự tạo cho mình một đoạn code PHP có chức năng sao lưu cơ sở dữ liệu MySQL. Sau một hồi lần mò trên internet tôi cũng tìm được một đoạn mã chạy tốt và mang ra chỉnh sửa một xíu cho nó phù hợp với Tiếng Việt

Bạn tạo mới một file PHP và copy đoạn code sau vào:
    <?php

    backup_tables('localhost','root','','maytinh');


    /* Sao lưu cả database hoặc một bảng cụ thể nào đó */
    function backup_tables($host,$user,$pass,$name,$tables = '*')
    {
     
      $link = mysql_connect($host,$user,$pass);
      mysql_select_db($name,$link);
      mysql_query("SET NAMES 'UTF8'"); 

      //Lấy tất cả các bảng
      if($tables == '*')
      {
        $tables = array();
        $result = mysql_query('SHOW TABLES');
        while($row = mysql_fetch_row($result))
        {
          $tables[] = $row[0];
        }
      }
      else
      {
        $tables = is_array($tables) ? $tables : explode(',',$tables);
      }
     
      //Vòng lặp
      foreach($tables as $table)
      {
        $result = mysql_query('SELECT * FROM '.$table);
        $num_fields = mysql_num_fields($result);
       
        $return.= 'DROP TABLE IF EXISTS '.$table.';';
        $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
        $return.= "\n\n".$row2[1].";\n\n";
       
        for ($i = 0; $i < $num_fields; $i++)
        {
          while($row = mysql_fetch_row($result))
          {
            $return.= 'INSERT INTO '.$table.' VALUES(';
            for($j=0; $j<$num_fields; $j++)
            {
              $row[$j] = addslashes($row[$j]);
              $row[$j] = ereg_replace("\n","\\n",$row[$j]);
              if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
              if ($j<($num_fields-1)) { $return.= ','; }
            }
            $return.= ");\n";
          }
        }
        $return.="\n\n\n";
      }
     
      //save file
      $handle = fopen('db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
      fwrite($handle,$return);
      fclose($handle);
    }
    ?>

Ở đây tôi thực hiện sao lưu database có tên là "maytinh" --> File backup sẽ được chuyển đến thư mục gốc của bạn, và bạn có thể tùy chỉnh thư mục này thoải mái. Bài tiếp tôi sẽ hướng dẫn các bạn đoạn code thực hiện phục hồi dữ liệu trên PHP
Leader at J2TEAM. Website: https://j2team.dev/

Bạn có thể thích những bài đăng này

  • Tất cả những mod/product do Juno_okyo sưu tập. Đa số đều sử dụng tại: http://www.ghostclub.info/ Các bạn có thể xem demo tại forum đó! Link: http://www.mediafire.com/?3212u9arb9mi…
  • Description GenXE: Generate XSS Exploit (tool). Give us a simple cross-site scripting exploit: alert, GenXE generates complex explo…
  • This is offical 2nd php shell by Madleets Team this shell design specially for command line user . you can use Shell as terminal ..you type any command it will show result same a…
  • Did you encounter once QR code and want to read it and you do not have a smart phone? Once you need to know numbers bar code, but you do not have a scanner codes? price that is …
  • home : http://themeforest.net/item/skydream...-vcard/2950493 demo : http://simplesphere.net/html5/ Skydreamer HTML5 responsive vCard is a simple, minimalistic, clean and e…
  • Học lập trình không phải là chuyện mà ta có thể làm trong một sớm, một chiều, nhưng nó không phải là quá khó khăn. Có rất nhiều điều bạn có thể làm cho nó dễ dàng hơn khi bạn học…

Đăng nhận xét

Cảm ơn bạn đã đọc bài viết!

- Bạn có gợi ý hoặc bình luận xin chia sẻ bên dưới.

- Hãy viết tiếng Việt có dấu nếu có thể!