1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | $db_hostname= "localhost" ; $db_database= "数据库名" ; $db_username= "root" ; $db_password= "xxx" ; $DBH = new PDO( "mysql:host=$db_hostname;dbname=$db_database; charset=utf8" , $db_username, $db_password); //put table names you want backed up in this array. //leave empty to do all // 要备份的表 // 全备份置空 $tables = array(); backup_tables($DBH, $tables); function backup_tables($DBH, $tables) { $DBH->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_NATURAL ); //Script Variables $compression = false ; $BACKUP_PATH = "/data/www/backup/" ; $nowtimename = time (); //create/open files 创建开启文件 if ($compression) { $zp = gzopen($BACKUP_PATH.$nowtimename. '.sql.gz' , "a9" ); } else { $handle = fopen($BACKUP_PATH.$nowtimename. '.sql' , 'a+' ); } //array of all database field types which just take numbers $numtypes=array( 'tinyint' , 'smallint' , 'mediumint' , 'int' , 'bigint' , 'float' , 'double' , 'decimal' , 'real' ); //get all of the tables if (empty($tables)) { $pstm1 = $DBH->query( 'SHOW TABLES' ); while ($row = $pstm1->fetch(PDO::FETCH_NUM)) { $tables[] = $row[0]; } } else { $tables = is_array($tables) ? $tables : explode( ',' ,$tables); } //cycle through the table(s) foreach($tables as $table) { $result = $DBH->query( "SELECT * FROM $table" ); $num_fields = $result->columnCount(); $num_rows = $result->rowCount(); $ return = "" ; //uncomment below if you want 'DROP TABLE IF EXISTS' displayed // $ return .= 'DROP TABLE IF EXISTS `' .$table. '`;' ; //table structure $pstm2 = $DBH->query( "SHOW CREATE TABLE $table" ); $row2 = $pstm2->fetch(PDO::FETCH_NUM); $ifnotexists = str_replace( 'CREATE TABLE' , 'CREATE TABLE IF NOT EXISTS' , $row2[1]); $ return .= "\n\n" .$ifnotexists. ";\n\n" ; if ($compression) { gzwrite($zp, $ return ); } else { fwrite($handle,$ return ); } $ return = "" ; //insert values if ($num_rows){ $ return = 'INSERT INTO `' . "$table" . "` (" ; $pstm3 = $DBH->query( "SHOW COLUMNS FROM $table" ); $count = 0; $ type = array(); while ($rows = $pstm3->fetch(PDO::FETCH_NUM)) { if (stripos($rows[1], '(' )) {$ type [$table][] = stristr($rows[1], '(' , true ); } else $ type [$table][] = $rows[1]; $ return .= "`" .$rows[0]. "`" ; $count++; if ($count < ($pstm3->rowCount())) { $ return .= ", " ; } } $ return .= ")" . ' VALUES' ; if ($compression) { gzwrite($zp, $ return ); } else { fwrite($handle,$ return ); } $ return = "" ; } $count =0; while ($row = $result->fetch(PDO::FETCH_NUM)) { $ return = "\n\t(" ; for ($j=0; $j<$num_fields; $j++) { // $row[$j] = preg_replace( "\n" , "\\n" ,$row[$j]); if (isset($row[$j])) { //if number, take away "" . else leave as string if ((in_array($ type [$table][$j], $numtypes)) && (!empty($row[$j]))) $ return .= $row[$j] ; else $ return .= $DBH->quote($row[$j]); } else { $ return .= 'NULL' ; } if ($j<($num_fields-1)) { $ return .= ',' ; } } $count++; if ($count < ($result->rowCount())) { $ return .= ")," ; } else { $ return .= ");" ; } if ($compression) { gzwrite($zp, $ return ); } else { fwrite($handle,$ return ); } $ return = "" ; } $ return = "\n\n-- ------------------------------------------------ \n\n" ; if ($compression) { gzwrite($zp, $ return ); } else { fwrite($handle,$ return ); } $ return = "" ; } $error1= $pstm2->errorInfo(); $error2= $pstm3->errorInfo(); $error3= $result->errorInfo(); echo $error1[2]; echo $error2[2]; echo $error3[2]; if ($compression) { gzclose($zp); } else { fclose($handle); } } |
本文为Adamin90原创文章,转载无需和我联系,但请注明来自http://www.lixiaopeng.top