MySQL Data backup class code

Source: Internet
Author: User

 /***
  * Description, this class is suitable for small Web site database tutorial backup, built-in MySQL connection, simply configure the data connection
  * and storage backup location. The Show_dir_file () method in the
  * class can return directly to all files in the backup directory, and returns an array form
  * Method Expord_sql () directly generate SQL files
  * This kind of production is simple and can be spread arbitrarily , how do you propose to this class, please send a message to the shrimp
  * Producer: Tour Day Shrimp
  * emial:328742379@qq.com
  * **/

Class Data {
 public   $data _dir    = "class/";      // The path to which the backup file is stored
 public   $data _name   = "111cnnet.sql";    //backup file name
  private  $mysql _host  = "localhost"     //database address
 private  $mysql _user   = "root";      //user name
 private  $mysql _pwd   = "lpl19881129";     //password
 private  $mysql _db    = "Date";      //database name
 private  $mysql _code  = "GBK";       //encoding method

/***
* 1. Connecting to the database
* **/
function __construct () {
$conn = mysql_connect ($this->mysql_host, $this->mysql_user, $this->mysql_pwd);
mysql_select_db ($this->mysql_db);
mysql_query ("Set names $this->mysql_code");
}

/***
* 2. Generate SQL statements
* **/
Private Function Set_sql ($table) {
$tabledump = "DROP table if exists $table;";
$createtable = mysql_query ("Show create Table $table");
$create = Mysql_fetch_row ($createtable);
$tabledump. = $create [1]. ";";

$rows = mysql_query ("SELECT * from $table");
$numfields = Mysql_num_fields ($rows);
$numrows = mysql_num_rows ($rows);
while ($row = Mysql_fetch_row ($rows)) {
$comma = "";
$tabledump. = "INSERT into $table values (";
for ($i = 0; $i < $numfields; $i + +)
{
$tabledump. = $comma. "'". Mysql_escape_string ($row [$i]). "'";
$comma = ",";
}
$tabledump. = ");";
}
$tabledump. = "";

return $tabledump;
}

/***
* 3. Display files that have been backed up under the storage directory
* **/
Public Function Show_dir_file () {
$dir = $this->data_dir;
if (!is_dir ($dir)) {
if (!mkdir ($dir)) {
Echo folder does not exist, try to create a folder, create a failure, maybe you do not have relevant permissions ";
Exit ();
}else{
chmod ($dir, 755);
}
}

if (!is_writable ($dir)) {
echo "File not writable";
Exit ();
}

$link = Opendir ($dir);
if (! $link) {
echo "Create link Failed";
Exit ();
}
Return Scandir ($dir);
}

/***
* 4. Generate SQL file
* **/
Public Function Expord_sql () {
$this->show_dir_file ();
$result = Mysql_list_tables ($this->mysql_db);
while ($arr = Mysql_fetch_row ($result)) {
$tables. = $this->set_sql ($arr [0]);
}
$file = $this->data_dir. $this->data_name;
$link = fopen ($file, "w+");
if (!is_writable ($file)) {
echo "File not writable";
Exit ();
}
Fwrite ($link, $tables);
Fclose ($link);
echo "Backup succeeded";
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.