韦德国际_韦德国际1946官方网站_韦德国际1946手机版
做最好的网站

备份mysql数据库的php代码,通过php备份mysql数据的

日期:2019-05-22编辑作者:韦德国际1946手机版
<?php

##################### 
//CONFIGURATIONS  
#####################
// Define the name of the backup directory
define('BACKUP_DIR', './myBackups' ) ; 
// Define  Database Credentials
define('HOST', 'localhost' ) ; 
define('USER', 'testd!b' ) ; 
define('PASSWORD', 'k^$2y4n9@#VV' ) ; 
define('DB_NAME', 'test123' ) ; 
/*
Define the filename for the sql file
If you plan to upload the  file to Amazon's S3 service , use only lower-case letters 
*/
$fileName = 'mysqlbackup--' . date('d-m-Y') . '@'.date('h.i.s').'.sql' ; 
// Set execution time limit
if(function_exists('max_execution_time')) {
if( ini_get('max_execution_time') > 0 )     set_time_limit(0) ;
}

###########################  

//END  OF  CONFIGURATIONS  

###########################

// Check if directory is already created and has the proper permissions
if (!file_exists(BACKUP_DIR)) mkdir(BACKUP_DIR , 0700) ;
if (!is_writable(BACKUP_DIR)) chmod(BACKUP_DIR , 0700) ; 

// Create an ".htaccess" file , it will restrict direct accss to the backup-directory . 
$content = 'deny from all' ; 
$file = new SplFileObject(BACKUP_DIR . '/.htaccess', "w") ;
$file->fwrite($content) ;

$mysqli = new mysqli(HOST , USER , PASSWORD , DB_NAME) ;
if (mysqli_connect_errno())
{
   printf("Connect failed: %s", mysqli_connect_error());
   exit();
}
 // Introduction information
 $return .= "--n";
$return .= "-- A Mysql Backup System n";
$return .= "--n";
$return .= '-- Export created: ' . date("Y/m/d") . ' on ' . date("h:i") . "nnn";
$return = "--n";
$return .= "-- Database : " . DB_NAME . "n";
$return .= "--n";
$return .= "-- --------------------------------------------------n";
$return .= "-- ---------------------------------------------------n";
$return .= 'SET AUTOCOMMIT = 0 ;' ."n" ;
$return .= 'SET FOREIGN_KEY_CHECKS=0 ;' ."n" ;
$tables = array() ; 
// Exploring what tables this database has
$result = $mysqli->query('SHOW TABLES' ) ; 
// Cycle through "$result" and put content into an array
while ($row = $result->fetch_row()) 
{
$tables[] = $row[0] ;
}
// Cycle through each  table
 foreach($tables as $table)
 { 
// Get content of each table
$result = $mysqli->query('SELECT * FROM '. $table) ; 
// Get number of fields (columns) of each table
$num_fields = $mysqli->field_count  ;
// Add table information
$return .= "--n" ;
$return .= '-- Tabel structure for table `' . $table . '`' . "n" ;
$return .= "--n" ;
$return.= 'DROP TABLE  IF EXISTS `'.$table.'`;' . "n" ; 
// Get the table-shema
$shema = $mysqli->query('SHOW CREATE TABLE '.$table) ;
// Extract table shema 
$tableshema = $shema->fetch_row() ; 
// Append table-shema into code
$return.= $tableshema[1].";" . "nn" ; 
// Cycle through each table-row
while($rowdata = $result->fetch_row()) 
{ 
// Prepare code that will insert data into table 
$return .= 'INSERT INTO `'.$table .'`  VALUES ( '  ;
// Extract data of each row 
for($i=0; $i<$num_fields; $i  )
{
$return .= '"'.$rowdata[$i] . ""," ;
 }
 // Let's remove the last comma 
 $return = substr("$return", 0, -1) ; 
 $return .= ");" ."n" ;
 } 
 $return .= "nn" ; 
}
// Close the connection
$mysqli->close() ;
$return .= 'SET FOREIGN_KEY_CHECKS = 1 ; '  . "n" ; 
$return .= 'COMMIT ; '  . "n" ;
$return .= 'SET AUTOCOMMIT = 1 ; ' . "n"  ; 
//$file = file_put_contents($fileName , $return) ; 
$zip = new ZipArchive() ;
$resOpen = $zip->open(BACKUP_DIR . '/' .$fileName.".zip" , ZIPARCHIVE::CREATE) ;
if( $resOpen ){
$zip->addFromString( $fileName , "$return" ) ;
    }
$zip->close() ;
$fileSize = get_file_size_unit(filesize(BACKUP_DIR . "/". $fileName . '.zip')) ; 
$message = <<<msg
  <h2>BACKUP  completed ,</h2><br> 
  the archive has the name of  : <b>  $fileName  </b> and it's file-size is :   $fileSize  .<br>
 This zip archive can't be accessed via a web browser , as it's stored into a protected directory . <br>
  It's highly recomended to transfer this backup to another filesystem , use your favorite FTP client to download the archieve . 
msg;
echo $message ; 

// Function to append proper Unit after file-size . 
function get_file_size_unit($file_size){
switch (true) {
    case ($file_size/1024 < 1) :
        return intval($file_size ) ." Bytes" ;
        break;
    case ($file_size/1024 >= 1 && $file_size/(1024*1024) < 1)  :
        return intval($file_size/1024) ." KB" ;
        break;
    default:
    return intval($file_size/(1024*1024)) ." MB" ;
}
}
<?php

##################### 
//CONFIGURATIONS  
#####################
// Define the name of the backup directory
define('BACKUP_DIR', './myBackups' ) ; 
// Define  Database Credentials
define('HOST', 'localhost' ) ; 
define('USER', 'testd!b' ) ; 
define('PASSWORD', 'k^$2y4n9@#VV' ) ; 
define('DB_NAME', 'test123' ) ; 
/*
Define the filename for the sql file
If you plan to upload the  file to Amazon's S3 service , use only lower-case letters 
*/
$fileName = 'mysqlbackup--' . date('d-m-Y') . '@'.date('h.i.s').'.sql' ; 
// Set execution time limit
if(function_exists('max_execution_time')) {
if( ini_get('max_execution_time') > 0 )     set_time_limit(0) ;
}

###########################  

//END  OF  CONFIGURATIONS  

###########################

// Check if directory is already created and has the proper permissions
if (!file_exists(BACKUP_DIR)) mkdir(BACKUP_DIR , 0700) ;
if (!is_writable(BACKUP_DIR)) chmod(BACKUP_DIR , 0700) ; 

// Create an ".htaccess" file , it will restrict direct accss to the backup-directory . 
$content = 'deny from all' ; 
$file = new SplFileObject(BACKUP_DIR . '/.htaccess', "w") ;
$file->fwrite($content) ;

$mysqli = new mysqli(HOST , USER , PASSWORD , DB_NAME) ;
if (mysqli_connect_errno())
{
   printf("Connect failed: %s", mysqli_connect_error());
   exit();
}
 // Introduction information
 $return .= "--n";
$return .= "-- A Mysql Backup System n";
$return .= "--n";
$return .= '-- Export created: ' . date("Y/m/d") . ' on ' . date("h:i") . "nnn";
$return = "--n";
$return .= "-- Database : " . DB_NAME . "n";
$return .= "--n";
$return .= "-- --------------------------------------------------n";
$return .= "-- ---------------------------------------------------n";
$return .= 'SET AUTOCOMMIT = 0 ;' ."n" ;
$return .= 'SET FOREIGN_KEY_CHECKS=0 ;' ."n" ;
$tables = array() ; 
// Exploring what tables this database has
$result = $mysqli->query('SHOW TABLES' ) ; 
// Cycle through "$result" and put content into an array
while ($row = $result->fetch_row()) 
{
$tables[] = $row[0] ;
}
// Cycle through each  table
 foreach($tables as $table)
 { 
// Get content of each table
$result = $mysqli->query('SELECT * FROM '. $table) ; 
// Get number of fields (columns) of each table
$num_fields = $mysqli->field_count  ;
// Add table information
$return .= "--n" ;
$return .= '-- Tabel structure for table `' . $table . '`' . "n" ;
$return .= "--n" ;
$return.= 'DROP TABLE  IF EXISTS `'.$table.'`;' . "n" ; 
// Get the table-shema
$shema = $mysqli->query('SHOW CREATE TABLE '.$table) ;
// Extract table shema 
$tableshema = $shema->fetch_row() ; 
// Append table-shema into code
$return.= $tableshema[1].";" . "nn" ; 
// Cycle through each table-row
while($rowdata = $result->fetch_row()) 
{ 
// Prepare code that will insert data into table 
$return .= 'INSERT INTO `'.$table .'`  VALUES ( '  ;
// Extract data of each row 
for($i=0; $i<$num_fields; $i  )
{
$return .= '"'.$rowdata[$i] . ""," ;
 }
 // Let's remove the last comma 
 $return = substr("$return", 0, -1) ; 
 $return .= ");" ."n" ;
 } 
 $return .= "nn" ; 
}
// Close the connection
$mysqli->close() ;
$return .= 'SET FOREIGN_KEY_CHECKS = 1 ; '  . "n" ; 
$return .= 'COMMIT ; '  . "n" ;
$return .= 'SET AUTOCOMMIT = 1 ; ' . "n"  ; 
//$file = file_put_contents($fileName , $return) ; 
$zip = new ZipArchive() ;
$resOpen = $zip->open(BACKUP_DIR . '/' .$fileName.".zip" , ZIPARCHIVE::CREATE) ;
if( $resOpen ){
$zip->addFromString( $fileName , "$return" ) ;
    }
$zip->close() ;
$fileSize = get_file_size_unit(filesize(BACKUP_DIR . "/". $fileName . '.zip')) ; 
$message = <<<msg
  <h2>BACKUP  completed ,</h2><br> 
  the archive has the name of  : <b>  $fileName  </b> and it's file-size is :   $fileSize  .<br>
 This zip archive can't be accessed via a web browser , as it's stored into a protected directory . <br>
  It's highly recomended to transfer this backup to another filesystem , use your favorite FTP client to download the archieve . 
msg;
echo $message ; 

// Function to append proper Unit after file-size . 
function get_file_size_unit($file_size){
switch (true) {
    case ($file_size/1024 < 1) :
        return intval($file_size ) ." Bytes" ;
        break;
    case ($file_size/1024 >= 1 &amp;&amp; $file_size/(1024*1024) < 1)  :
        return intval($file_size/1024) ." KB" ;
        break;
    default:
    return intval($file_size/(1024*1024)) ." MB" ;
}
}
<?php
$Database_name = 'somedb';
$table = 'sometable';

     Define ('H', 'localhost'); define ('N', 'root') define ('P','password'); define ('T', $database_name);
     Mysql_connect (H, N, P) or die (mysql_error ()); mysql_select_db (T) or die (mysql_error ());

     function Escape($v){return mysql_real_escape_string($v);}

     Define('FILE', 'C:/Program Files/EasyPHP 2.0b1/www/'. $table .'/'. date ('d').'-'. date (' m').'-' . date ('Y').'.php');
     Define('FP', fopen(FILE,'w'));

     $aa = mysql_list_tables($database_name); while ($a = mysql_fetch_row($aa))
     {

             $bb = mysql_query ('SELECT * FROM'. $a[0].''); $tt = mysql_num_rows($bb); $nb = mysql_num_fields($bb);
             If($tt>0) 
             {
                 $p = 'INSERT INTO'. $a[0]. " VALUES ';

                 $t = 0;
                 while ($b = mysql_fetch_array($bb)) 
                 {
                     $ p .='(';
                     For ($i=0;$i<$nb, $i  ) 

                     {
                            if ($i==0) 
                            {
                                $p .= "'$b[$i]'";
                            }
                            else
                            {
                                $p .=' , ''. Escape($b[$ i]). ''';
                            }
                     }
                     $t  ;

                     if ($t<$tt) 
                     {
                         $ p .='),'."  n ";
                     }
                     else
                     {
                        $ p .=');'."  n";
                     }
                 } 
                 $p .= "nnn";
                 fwrite(FP, $p);
             }
     }

     fclose(FP);
?>
<?php
$Database_name = 'somedb';
$table = 'sometable';

     Define ('H', 'localhost'); define ('N', 'root') define ('P','password'); define ('T', $database_name);
     Mysql_connect (H, N, P) or die (mysql_error ()); mysql_select_db (T) or die (mysql_error ());

     function Escape($v){return mysql_real_escape_string($v);}

     Define('FILE', 'C:/Program Files/EasyPHP 2.0b1/www/'. $table .'/'. date ('d').'-'. date (' m').'-' . date ('Y').'.php');
     Define('FP', fopen(FILE,'w'));

     $aa = mysql_list_tables($database_name); while ($a = mysql_fetch_row($aa))
     {

             $bb = mysql_query ('SELECT * FROM'. $a[0].''); $tt = mysql_num_rows($bb); $nb = mysql_num_fields($bb);
             If($tt>0) 
             {
                 $p = 'INSERT INTO'. $a[0]. " VALUES ';

                 $t = 0;
                 while ($b = mysql_fetch_array($bb)) 
                 {
                     $ p .='(';
                     For ($i=0;$i<$nb, $i  ) 

                     {
                            if ($i==0) 
                            {
                                $p .= "'$b[$i]'";
                            }
                            else
                            {
                                $p .=' , ''. Escape($b[$ i]). ''';
                            }
                     }
                     $t  ;

                     if ($t<$tt) 
                     {
                         $ p .='),'."  n ";
                     }
                     else
                     {
                        $ p .=');'."  n";
                     }
                 } 
                 $p .= "nnn";
                 fwrite(FP, $p);
             }
     }

     fclose(FP);
?>

本文由韦德国际发布于韦德国际1946手机版,转载请注明出处:备份mysql数据库的php代码,通过php备份mysql数据的

关键词: PHP mysql 数据库备份 数据备份 伟德

mysql存款和储蓄进程再次回到resultset,php调用my

在蕴藏进程中选取select语句可以在jdbc中得到此存款和储蓄进度的归来的resultset,即便你的囤积进度中有七个select语句...

详细>>

mysql有时表的创办和选择

mysql中须求经过 create temporary table tablename 来创建有的时候表,不时表常用在复杂查询时临时存放中间表数据。 1 说明...

详细>>

mysql内部存款和储蓄器表和有的时候表的分别,

mysql内部存款和储蓄器表和有的时候表的分别,mysql内部存款和储蓄器表介绍。“内部存款和储蓄器表”从名称想到所...

详细>>