PHP-MySQL Database Backup

Source: Internet
Author: User

Usage:

Require_once ("backdata. class. php ");
$ Link = @ mysql_connect ("localhost", "Database Name", "password") or die ('could not connect to server .');
Mysql_query ("use cms", $ link );
Mysql_query ("set names utf8", $ link );

$ Dbbck = new backupData ($ link); // instantiate it. You only need a link ID.

// If you want to back up all the tables in a database during data backup, write as follows:
$ Dbbck-> backupTables ("cms", "./", array ('*'));

// When backing up data, if you want to back up only one table in a database, you can write as follows:
$ Dbbck-> backupTables ("cms", "./", array ('user '));

// When backing up data, if you want to back up multiple tables in a database, you can write as follows:
$ Dbbck-> backupTables ("cms", "./", array ('user', 'acl', 'in oin '));

// Annotation: $ dbbck-> backupTables ("parameter 1", "parameter 2", array,
Parameter 1: Database Name,
Parameter 2: Location of the backup data to be stored (that is, the directory address)
Third: the tables you want to save

Backdata. class. php

<? Php
/*
*
* A simple Mysql backup data class
*
*/
Class backupData {
Private $ mysql_link; // link ID
Private $ dbName; // Database Name
Private $ dataDir; // directory where data is stored
Private $ tableNames; // table name

Public function _ construct ($ mysql_link ){
$ This-> mysql_link = $ mysql_link;
}
Public function backupTables ($ dbName, $ dataDir, $ tableNames) {// start backup
$ This-> dbName = $ dbName;
$ This-> dataDir = $ dataDir;
$ This-> tableNames = $ tableNames;
$ Tables = $ this-> delarray ($ this-> tableNames );
$ Sqls = '';
Foreach ($ tables as $ tablename ){
If ($ tablename = '') {// The table does not exist
Continue;
}

****** ********
// If a table exists, delete it first.
$ Sqls. = "drop table if exists $ tablename; \ n ";
// Read the table structure
$ Rs = mysql_query ("show create table $ tablename", $ this-> mysql_link );
$ Row = mysql_fetch_row ($ rs );
// Obtain the table structure as an SQL statement
$ Sqls. = $ row ['1']. "; \ n ";
Unset ($ rs );
Unset ($ row );

// ************************ The second half of the SQL statement is formed ******* *******
// Query all the data in the table
$ Rs = mysql_query ("select * from $ tablename", $ this-> mysql_link );
// Number of fields in the table
$ Field = mysql_num_fields ($ rs );
// Form this SQL statement: "INSERT INTO 'groups' VALUES ('1499e0ca25988d', 'directory','', '0 ');"
While ($ rows = mysql_fetch_row ($ rs )){
$ Comma = ''; // comma
$ Sqls. = "insert into '$ tablename' VALUES (";
For ($ I = 0; $ I <$ field; $ I ++ ){
$ Sqls. = $ comma. "'". $ rows [$ I]. "'";
$ Comma = ',';
}
$ Sqls. = "); \ n ";
}
}
$ Backfilepath = $ this-> dataDir. date ("Ymdhis", time (). '. SQL ';

// Write a file
$ Filehandle = fopen ($ backfilepath, "w ");
Fwrite ($ filehandle, $ sqls );
Fclose ($ filehandle );
}
Private function delarray ($ array) {// process the incoming array
Foreach ($ array as $ tables ){
If ($ tables = '*') {// All tables (the table name cannot be an array in the conventional way)
$ Newtables = mysql_list_tables ($ this-> dbName, $ this-> mysql_link );
$ TableList = array ();
For ($ I = 0; $ I <mysql_numrows ($ newtables); $ I ++ ){
Array_push ($ tableList, mysql_tablename ($ newtables, $ I ));
}
$ TableList = $ tableList;
} Else {
$ TableList = $ array;
Break;
}
}
Return $ tableList;
}
}
?>

Recommended reading:

Using mysqldump in Linux to back up a MySQL database as an SQL File

Use mysqldump in Linux to regularly back up MySQL Databases

MySQL backup and restoration Parameters

Comparison of backup efficiency between MySQL backup tools mysqldump and mydumper

MySQL Backup Recovery "fault" Summary

Summary of three methods for MySQL backup and recovery

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.