/** * Description: This type is applicable to database backup for small websites and built-in mysql connections. You only need to configure data connections simply. * And the storage backup location. * Perform the following operations after the class is real-listed and connected to the database * Get_db_table ($ database) obtains all data tables. * Export_ SQL ($ table, $ subsection = 0) generates an SQL file. Note that the generated SQL file is saved only to the server directory and is not downloaded. * Import_ SQL ($ dir) only import SQL files in the server directory to restore Data * This type is easy to create and can be spread at will. If you have any suggestions for this type, please send an email to the shrimps. * @ Author Zhao Hongjian [youtian shrimp] * Email: 328742379@qq.com * Qq chat group: 69574955 juyitang-Online page creation */ Class data { Public $ data_dir = "class/"; // path of the backup file Public $ transfer = ""; // temporarily store SQL statements. [do not assign values to this attribute. Otherwise, an incorrect SQL statement is generated.] /** * Database connection * @ Param string $ host Name * @ Param string $ user Username * @ Param string $ pwd Password * @ Param string $ db select the Database Name * @ Param string $ charset encoding method */ Function connect_db ($ host, $ user, $ pwd, $ db, $ charset = 'gbk '){ If (! $ Conn = mysql_connect ($ host, $ user, $ pwd )){ Return false; } Mysql_select_db ($ db ); Mysql_query ("set names $ charset "); Return true; } /** * Generate SQL statements * @ Param $ table the table to be backed up * @ Return $ tabledump: SQL statement generated */ Public function set_ SQL ($ table, $ subsection = 0, & $ tabledom = ''){ $ Tabledom. = "drop table if exists $ tablen "; $ Createtable = mysql_query ("show create table $ table "); $ Create = mysql_fetch_row ($ createtable ); $ Create [1] = str_replace ("n", "", $ create [1]); $ Create [1] = str_replace ("t", "", $ create [1]); $ Tabledom. = $ create [1]. "; n "; $ Rows = mysql_query ("select * from $ table "); $ Numfields = mysql_num_fields ($ rows ); $ Numrows = mysql_num_rows ($ rows ); $ N = 1; $ Sqlarry = array (); While ($ row = mysql_fetch_row ($ rows )){ $ Comma = ""; $ Tabledom. = "insert into $ table values ("; For ($ I = 0; $ I <$ numfields; $ I ++) { $ Tabledom. = $ comma. "'". mysql_escape_string ($ row [$ I]). "'"; $ Comma = ","; } $ Tabledom. = ") n "; If ($ subsection! = 0 & strlen ($ this-> transfer) >=$ subsection * 1000 ){ $ Sqlarry [$ n] = $ tabledom; $ Tabledom = ''; $ n ++; } } Return $ sqlarry; } /** * List tables in a database * @ Param database $ name of the database to be operated on * @ Return array $ database table in the dbarray list */ Public function get_db_table ($ database ){ $ Result = mysql_list_tables ($ database ); While ($ tmparry = mysql_fetch_row ($ result )){ $ Dbarry [] = $ tmparry [0]; } Return $ dbarry; } /** * Verify whether the directory is valid * @ Param diretory $ dir * @ Return booln */ Function check_write_dir ($ dir ){ If (! Is_dir ($ dir) {@ mkdir ($ dir, 0777 );} If (is_dir ($ dir )){ If ($ link = opendir ($ dir )){ $ Filearry = scandir ($ dir ); For ($ I = 0; $ I If ($ filearry [$ I]! = '.' | $ Filearry! = '..'){ @ Unlink ($ dir. $ filearry [$ I]); } } } } Return true; } /** * Write data to a file * @ Param file $ filename file name * @ Param string $ str information to be written * @ Return booln return true if the write is successful; otherwise, false */ Private function write_ SQL ($ filename, $ str ){ $ Re = true; If (! @ $ Fp = fopen ($ filename, "w +") {$ re = false; echo "An error occurred while opening the file. Backup failed! ";} If (! @ Fwrite ($ fp, $ str) {$ re = false; echo "An error occurred while writing information. Backup failed! ";} If (! @ Fclose ($ fp) {$ re = false; echo "An error occurred while closing the file. Backup failed! ";} Return $ re; } /** * Generate an SQL File * @ Param string $ SQL statement * @ Param number $ subsection volume size, in kb, 0 indicates no volume sharding */ Public function export_ SQL ($ table, $ subsection = 0 ){ If (! $ This-> check_write_dir ($ this-> data_dir) {echo 'you do not have permission to operate the directory, backup failed '; return false ;} If ($ subsection = 0 ){ If (! Is_array ($ table )){ $ This-> set_ SQL ($ table, 0, $ this-> transfer ); } Else { For ($ I = 0; $ I $ This-> set_ SQL ($ table [$ I], 0, $ this-> transfer ); } } $ Filename = $ this-> data_dir.date ("ymd", time (). '_ all. SQL '; If (! $ This-> write_ SQL ($ filename, $ this-> transfer) {return false ;} } Else { If (! Is_array ($ table )){ $ Sqlarry = $ this-> set_ SQL ($ table, $ subsection, $ this-> transfer ); $ Sqlarry [] = $ this-> transfer; } Else { $ Sqlarry = array (); For ($ I = 0; $ I $ Tmparry = $ this-> set_ SQL ($ table [$ I], $ subsection, $ this-> transfer ); $ Sqlarry = array_merge ($ sqlarry, $ tmparry ); } $ Sqlarry [] = $ this-> transfer; } For ($ I = 0; $ I $ Filename = $ this-> data_dir.date ("ymd", time (). '_ part'. $ I.'. SQL '; If (! $ This-> write_ SQL ($ filename, $ sqlarry [$ I]) {return false ;} } } Return true; } /** * Load an SQL File * @ Param diretory $ dir * @ Return booln * Note: Do not store other files in the directory or directory. * To save recovery time */ Public function import_ SQL ($ dir ){ If ($ link = opendir ($ dir )){ $ Filearry = scandir ($ dir ); $ Pattern = "_ part [0-9] +. SQL $ | _ all. SQL $ "; For ($ I = 0; $ I If (eregi ($ pattern, $ filearry [$ I]) { $ Sqls = file ($ dir. $ filearry [$ I]); Foreach ($ sqls as $ SQL ){ Str_replace ("r", "", $ SQL ); Str_replace ("n", "", $ SQL ); If (! Mysql_query (trim ($ SQL) return false; } } } Return true; } } } |