您好,欢迎访问一九零五行业门户网

备份mysql数据的php代码

0 ) set_time_limit(0) ;}########################### //end of configurations ###########################// check if directory is already created and has the proper permissionsif (!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) . \n\n\n;$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 arraywhile ($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].; . \n\n ; // cycle through each table-rowwhile($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{$return .= ''.$rowdata[$i] . \, ; } // let's remove the last comma $return = substr($return, 0, -1) ; $return .= ); .\n ; } $return .= \n\n ; }// 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 = backup completed ,
the archive has the name of : $filename and it's file-size is : $filesize .
this zip archive can't be accessed via a web browser , as it's stored into a protected directory .
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 return intval($file_size ) . bytes ; break; case ($file_size/1024 >= 1 && $file_size/(1024*1024) return intval($file_size/1024) . kb ; break; default: return intval($file_size/(1024*1024)) . mb ;}}
复制代码
mysql, php
其它类似信息

推荐信息