Copy codeThe Code is as follows:
<? Php
$ Pai_dbhost = 'localhost'; // mysql host
$ Pai_dbname = 'sq _ test'; // Database 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 "). "*/\ r \ nDROP table if exists '$ T'; \ r \ n ". $ s1 ['create table']. "; \ r \ n ";
$ 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); \ r \ n ";
}
$ Mysql. = "\ r \ n ";
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 ";
?>