├── README.md ├── dumpdb.php └── DumpDB.jsp /README.md: -------------------------------------------------------------------------------- 1 | ##
MYSQL脱裤脚本
2 | 3 |
4 | 5 | 6 | mysql脱裤脚本,添加好信息,会生成一个以数据库为名的zip。里面是每张表的数据.... 7 | 8 | txt文件, 以逗号分割。 9 | 10 | 11 | 自己实战的时候发现没有好的脱裤脚本。哎,造福大众吧...... 12 | 13 | 14 | 2016-06-03 15 | 16 | + 添加jsp版脱裤脚本 17 | -------------------------------------------------------------------------------- /dumpdb.php: -------------------------------------------------------------------------------- 1 | conn = mysql_connect($host, $username, $password); 9 | if(!$this->conn){ 10 | exit("mysql Connect Error!"); 11 | } 12 | $this->db = $db; 13 | mysql_select_db($db); 14 | $this->Dump(); 15 | $this->addFileToZip($this->db); 16 | } 17 | 18 | public function Dump(){ 19 | $result = mysql_query("show tables"); 20 | $table = array(); 21 | while (!!$rs=mysql_fetch_array($result)) { 22 | $sql = "select * from " . $rs[0]; 23 | $content = mysql_query($sql); 24 | while (!!$ns=mysql_fetch_assoc($content)) { 25 | $this->dataformat($rs[0], $ns); 26 | } 27 | } 28 | } 29 | 30 | public function dataformat($table, $ns){ 31 | $split = ", "; 32 | $dir = $this->db; 33 | $str = implode($split, $ns); 34 | if(!is_dir($dir)){ 35 | mkdir($dir); 36 | } 37 | $filename = $dir . '/' . $table . '.txt'; 38 | if (!file_exists($filename)){ 39 | $columns = ""; 40 | foreach ($ns as $key => $value) { 41 | $columns .= $key . $split; 42 | } 43 | $columns = substr($columns, 0, strrpos($columns, $split)) . "\r\n"; 44 | file_put_contents($filename, $columns, FILE_APPEND); 45 | return 1; 46 | } 47 | file_put_contents($filename, $str. "\r\n", FILE_APPEND); 48 | } 49 | 50 | public function addFileToZip($dbname){ 51 | $zip = new ZipArchive; 52 | $zipfile = $dbname . '.zip'; 53 | $res = $zip->open($zipfile, ZipArchive::CREATE); 54 | if($res===TRUE){ 55 | $handler = opendir($dbname); 56 | while (($filename = readdir($handler)) !== false) { 57 | if($filename != '.' && $filename != '..'){ 58 | if(is_dir($dbname.'/'.$filename)){ 59 | addFileToZip($dbname.'/'.$filename, $zip); 60 | }else{ 61 | $zip->addFile($dbname. '/' . $filename); 62 | } 63 | } 64 | } 65 | @closedir($handler); 66 | $zip->close(); 67 | } 68 | } 69 | 70 | public function __destruct(){ 71 | mysql_close($this->conn); 72 | echo "

Dump Database successful

"; 73 | } 74 | 75 | } 76 | if (isset($_POST['submit']) && $_POST['submit'] == 'dump'){ 77 | new DumpDB($_POST['host'], $_POST['username'], $_POST['password'], $_POST['database']); 78 | } 79 | 80 | 81 | ?> 82 | 83 | 84 | 85 | 86 | mysql dump database 87 | 88 | 89 |

blog: http://www.evalshell.com

90 |
91 | 92 | 93 | 96 | 99 | 102 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 |
94 | HOST 95 | 97 | User 98 | 100 | Password 101 | 103 | Database 104 |
113 | 114 |
115 | 116 | 117 | -------------------------------------------------------------------------------- /DumpDB.jsp: -------------------------------------------------------------------------------- 1 | <%@page import="java.util.zip.ZipEntry"%> 2 | <%@page import="java.io.FileOutputStream"%> 3 | <%@page import="java.util.zip.ZipOutputStream"%> 4 | <%@page import="java.io.BufferedWriter"%> 5 | <%@page import="java.io.FileWriter"%> 6 | <%@page import="java.io.IOException"%> 7 | <%@page import="java.util.Map.Entry"%> 8 | <%@page import="java.util.Set"%> 9 | <%@page import="java.util.Iterator"%> 10 | <%@page import="java.util.Map"%> 11 | <%@page import="java.io.File"%> 12 | <%@page import="java.util.HashMap"%> 13 | <%@ page language="java" contentType="text/html; charset=UTF-8" 14 | pageEncoding="UTF-8"%> 15 | 16 | <%@ page import="java.sql.*" %> 17 | <%@page import="java.util.ArrayList"%> 18 | <% 19 | request.setCharacterEncoding("utf-8"); 20 | 21 | String host = request.getParameter("host"); 22 | String username = request.getParameter("username"); 23 | String password = request.getParameter("password"); 24 | String database = request.getParameter("database"); 25 | String action = request.getParameter("action"); 26 | String path = new File(application.getRealPath(request.getRequestURI())).getParent(); 27 | 28 | class DumpDB{ 29 | private String host; 30 | private String username; 31 | private String password; 32 | private String database; 33 | private String path; 34 | 35 | public DumpDB(String host, String username, String password, String database, String path) throws IOException{ 36 | this.host = host; 37 | this.username = username; 38 | this.password = password; 39 | this.database = database; 40 | this.path = path; 41 | String ConnectUrl = "jdbc:mysql://" + this.host + "/" + this.database + "?user=" + this.username + "&password=" + this.password; 42 | this.AddFile2Zip(database, this.Dump(ConnectUrl)); 43 | } 44 | 45 | public HashMap Dump(String ConnectUrl){ 46 | HashMap data = new HashMap(); 47 | try{ 48 | Class.forName("com.mysql.jdbc.Driver"); 49 | Connection conn = DriverManager.getConnection(ConnectUrl); 50 | conn.setAutoCommit(false); 51 | DatabaseMetaData md = conn.getMetaData(); 52 | ResultSet rs = md.getTables(null, null, "%", null); 53 | //each tables 54 | while(rs.next()){ 55 | ArrayList al = new ArrayList(); 56 | String sql = "select * from " + rs.getString(3); 57 | Statement stmt = conn.createStatement(); 58 | ResultSet rs2 = stmt.executeQuery(sql); 59 | ResultSetMetaData rsmd = rs2.getMetaData(); 60 | 61 | String name = rsmd.getColumnName(1); 62 | 63 | String columns = ""; 64 | String coldata = ""; 65 | for(int i=1; i<=rsmd.getColumnCount();i++){ 66 | columns += rsmd.getColumnName(i) + ", "; 67 | } 68 | 69 | //add columns 70 | al.add(columns); 71 | 72 | while(rs2.next()){ 73 | coldata = ""; 74 | for(int i=1; i<=rsmd.getColumnCount();i++){ 75 | coldata += rs2.getString(i) + ", "; 76 | } 77 | al.add(coldata); 78 | 79 | } 80 | data.put(rs.getString(3), al); 81 | rs2.close(); 82 | stmt.close(); 83 | } 84 | conn.close(); 85 | rs.close(); 86 | 87 | }catch(ClassNotFoundException e){ 88 | System.out.println("Can't Found Jar package!"); 89 | }catch(SQLException e1){ 90 | System.out.println("SQL Error!"); 91 | } 92 | 93 | return data; 94 | } 95 | 96 | public void AddFile2Zip(String db, HashMap maps) throws IOException{ 97 | File zipfile = new File(this.path + "/" + db + ".zip"); 98 | ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile)); 99 | Set entries = maps.entrySet(); 100 | for(Iterator i = entries.iterator();i.hasNext();){ 101 | Entry e = i.next(); 102 | 103 | ArrayList value = (ArrayList)e.getValue(); 104 | 105 | //write file 106 | String filename = e.getKey() + ".txt"; 107 | //System.out.println(new File(filename).exists()); 108 | ZipEntry zip = new ZipEntry(filename); 109 | out.putNextEntry(zip); 110 | StringBuilder sb = new StringBuilder(); 111 | for(int j=0;j 149 | 150 | 151 | 152 | 153 | DumpDB (mysql) 154 | 155 | 156 |

blog: http://www.evalshell.com

157 |
158 | 159 | 160 | 161 | 164 | 167 | 170 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 |
162 | HOST 163 | 165 | User 166 | 168 | Password 169 | 171 | Database 172 |
181 | 182 |
183 | 184 | 185 | --------------------------------------------------------------------------------