Mysql Database Backup

Source: Internet
Author: User
Tags mysql tutorial


<? Php tutorial
$ Cfg_dbhost = 'localhost'; // mysql tutorial host
$ Pai_dbname = 'sq _ test'; // database tutorial name
$ User_dbuser = 'root'; // database username
$ Export _dbpwd = 'jb51. net'; // Database User Password
$ Pai_db_language = 'utf8'; // database Encoding

Class dbmysql {
Public static $ dbhost = 'localhost ';
Public static $ dbname;
Public static $ dbuser = 'root ';
Public static $ dbpass;
Public static $ charset = 'utf8 ';
Public static $ DB = null;
Public $ querycount = 0;

Public function _ construct ()
{
Self: $ dbhost = $ GLOBALS ['cfg _ dbhost'];
Self: $ dbname = $ GLOBALS ['cfg _ dbname'];
Self: $ dbuser = $ GLOBALS ['cfg _ dbuser'];
Self: $ dbpass = $ GLOBALS ['cfg _ dbpwd'];
Self: $ charset = $ GLOBALS ['cfg _ db_language '];
Self: connect ();
}
Public function connect (){
Self: $ DB = mysql_connect (self ::$ dbhost, self ::$ dbuser, self ::$ dbpass );
If (! Self: $ DB ){
Self: sqlError ('cannot connect to the server! '. Self: mysqlerror); exit ("unable to connect to the server! ");;
}
If (! Mysql_select_db (self: $ dbname )){
Self: sqlError ('unable to connect to the database ('. self: $ dbname .')! '. Self: mysqlerror); exit ("unable to connect to the database! ");
}
Mysql_query ("set names '". self: $ charset. "', character_set_client = binary, SQL _mode =''; ", self: $ DB );
}

Private function mysqlerror (){
Return mysql_error ();
}

Public function getTablesName (){
$ Res = mysql_query ('show tables from '. self: $ dbname, self: $ DB );
$ Tables = array ();
While ($ row = mysql_fetch_row ($ res) $ tables [] = $ row [0];
Mysql_free_result ($ res );
Return $ tables;
}
Public function getFields ($ table ){
$ Res = mysql_query ('describe'. $ table, self: $ DB );
$ Tables = array ();
While ($ row = mysql_fetch_row ($ res) $ tables [] = $ row [0];
Mysql_free_result ($ res );
Return $ tables;
}

Public function fetch_array ($ SQL ){
$ Res = mysql_query ($ SQL, self: $ DB );
$ R = mysql_fetch_array ($ res );
Mysql_free_result ($ res );
Return $ r;
}

Public function fetch_assoc ($ SQL ){
$ Q3 = mysql_query ($ SQL, self: $ DB); $ ra = array ();
While ($ data = mysql_fetch_assoc ($ q3 )){
$ Ra [] = $ data;
}
Mysql_free_result ($ q3 );
Return $ ra;
}
Private function sqlError ($ message = '', $ info ='', $ SQL = '') {// Save the error message to the file
Echo "{". $ message. "<br/> DATE :". date ('Y-n-j H: I: s '). "<br/> ERROR :". $ info. "<br/> SQL :". $ SQL. "<br/>}< br/> ";
}
Public function close (){
Self: $ DB = null;
}
Public function _ destruct ()
{
Self: close ();
}
}

/* --- Class end */

Function makedir ($ dirpath ){
If (! $ Dirpath) return 0;
$ Dirpath = str_replace ("\", "/", $ dirpath); $ mdir = "";
Foreach (explode ("/", $ dirpath) as $ val ){
$ Mdir. = $ val ."/";
If ($ val = "..." | $ val = ".") continue;
If (! Is_dir ($ mdir )&&! File_exists ($ mdir )){
If (! @ Mkdir ($ mdir, 0755 )){
Exit ("creating directory [". $ mdir. "] failed .");
}
}
}
Return true;
}

Function delDirAndFile ($ dirName ){
If ($ handle = opendir ($ dirName )){
While (false! ==( $ Item = readdir ($ handle ))){
If ($ item! = "." & $ Item! = ".."){
If (is_dir ("$ dirName/$ item ")){
DelDirAndFile ("$ dirName/$ item ");
} Else {unlink ("$ dirName/$ item ");}
}
}
Closedir ($ handle );
If (rmdir ($ dirName) echo "successfully deleted Directory: $ dirName <br/> n ";
}
}

Function filein ($ filename = "databak/", $ table = '', $ mysql = ''){
$ Fp = fopen ($ filename. '/'. $ table. '. SQL', 'w ');
Fputs ($ fp, $ mysql );
Fclose ($ fp );
}

Header ("Content-Type: text/html; charset = UTF-8 ");

$ Db = new dbmysql ();

$ Table = $ db-> getTablesName ();

$ Filename = "databak/". date ("Ymd ");
$ Url = getcwd (). "/databak /";
$ Handle = opendir ($ url );
While (false! ==( $ File = readdir ($ handle ))){
If ($ file! = "." & $ File! = ".." & Is_dir ($ url. "/". $ file )){
If (date ("Ymd")-$ file> 5) {delDirAndFile ($ url. "/". $ file );};
}
}

Makedir ($ filename );
Foreach ($ table as $ t ){
$ S1 = $ db-> fetch_array ("show create table '$ t '");
$ Mysql = "/* Time :". date ("Y-m-d H: I: s "). "*/rnDROP table if exists '$ T'; rn ". $ s1 ['create table']. "; rnrn ";
$ A1 = $ db-> fetch_assoc ("select * from '$ t '");
Foreach ($ a1 as $ data ){
$ Vals = array_values ($ data );
$ Vals = array_map ('addslashes', $ vals );
$ Vals = join ("','", $ vals );
$ Vals = "'". $ vals ."'";
$ Mysql. = "insert into '$ t' VALUES ($ vals); rn ";
}
$ Mysql. = "rn ";
Filein ($ filename, $ t, $ mysql );
}

Echo "the data is backed up successfully, and the backup file is generated". getcwd (). "/". $ filename. "/<br/> the program automatically cleans up the backup five days ago ";
?>

Related Article

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.