Copy Code code as follows:
<?php
$cfg _dbhost = ' localhost ';//mysql host
$cfg _dbname = ' sq_test ';//Database name
$cfg _dbuser = ' root ';//database user name
$cfg _dbpwd = ' jb51.net ';//database user password
$cfg _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 (' Unable to connect to server! ') Self::mysqlerror); exit ("Unable to connect to server!");;
}
if (!mysql_select_db (self:: $dbname)) {
Self::sqlerror (' Cannot connect to database ('. Self:: $dbname. ')! Self::mysqlerror); exit ("Unable to connect to 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) {
$q 3=mysql_query ($sql, Self:: $DB); $ra =array ();
while ($data =mysql_fetch_assoc ($q 3)) {
$ra []= $data;
}
Mysql_free_result ($q 3);
return $ra;
}
Private Function sqlError ($message = ', $info = ', $sql = ') {//Save error message to 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 ("Create directory [". $mdir. "] Failure. ");
}
}
}
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 deletes the 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) {
$s 1= $db->fetch_array ("Show create Table ' $t ')";
$mysql = "/*time:". Date ("Y-m-d h:i:s"). "*/\r\ndrop table IF EXISTS ' $t '; \ r \ n '. $s 1[' Create TABLE '."; \ R\n\r\n ";
$a 1= $db->FETCH_ASSOC ("select * from ' $t '");
foreach ($a 1 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 "Data backup succeeded, generate backup file". GETCWD (). /". $filename." /<br/> program automatically cleans up 5 days prior to backup ";
?>