zoukankan      html  css  js  c++  java
  • Java备份MySQl数据库,并备份图片数据

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.InputStreamReader;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    
    public class BackDataHelper {
        private String sqlPath="C:\\mysql\\bin\\";
        private String userName="user1";
        private String password="123456";
        private String dataName="datatest";
        private String folderName;
        private String backPath;
    
        private String ip="127.0.0.1";
        private String port="3306";
        
        public String getBackPath() {
            return backPath;
        }
        public void setBackPath(String backPath) {
            this.backPath = backPath;
        }
        public String getFolderName() {
            return folderName;
        }
        public void setFolderName(String folderName) {
            this.folderName = folderName;
        }
        public String getDataName() {
            return dataName;
        }
        public void setDataName(String dataName) {
            this.dataName = dataName;
        }
        public String getSqlPath() {
            return sqlPath;
        }
        public void setSqlPath(String sqlPath) {
            this.sqlPath = sqlPath;
        }
        public String getIp() {
            return ip;
        }
        public void setIp(String ip) {
            this.ip = ip;
        }
        public String getPort() {
            return port;
        }
        public void setPort(String port) {
            this.port = port;
        }
        
        public BackDataHelper(String sqlPath,String ip,String port,String backPath){
            this.sqlPath=sqlPath;
            this.ip=ip;
            this.port=port;
            this.folderName=getDateTime(new Date());
            this.backPath=backPath;
        }
        
        public BackDataHelper(String sqlPath,String ip,String port,String dataName,String backPath){
            this.sqlPath=sqlPath;
            this.ip=ip;
            this.port=port;
            this.dataName=dataName;
            this.folderName=getDateTime(new Date());
            this.backPath=backPath;
        }
        
        public BackDataHelper(String backPath){
            this.folderName=getDateTime(new Date());
            this.backPath=backPath;
        }
    
        private String getDateTime(Date date) {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd_HHmmss");
            String returnValue = "";
            if (date != null) {
                returnValue = df.format(date);
            }
            return (returnValue);
        }
    
        /**
         * 备份
         */
        public void executExport() throws Exception {
            String path=this.backPath+"\\"+this.folderName;
            File file = new File(path);
            if(!file.exists()){
                file.mkdirs();
            }
            String dataPath=path+"\\data.sql";
            String exec = "cmd /c " + this.sqlPath + "mysqldump.exe " + "-u" +this.userName + " " + "-p" + this.password + " " + "-h" + this.ip + " " + "-P" + this.port
                      + " " + dataName + " " + ">" +dataPath;
            Process p = Runtime.getRuntime().exec(exec);
            BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));   
            String readLine = br.readLine();   
            while (readLine != null) { 
                readLine = br.readLine();   
            }  
            if(br!=null){
                br.close();
            }
            GenerateKey.createEncrypt(dataPath);
            
            exec = "xcopy D:\\wwwroot\\"+this.dataName+"\\product\\*.*  "+path+"\\product\\*.*/e";
            p = Runtime.getRuntime().exec(exec);
            br = new BufferedReader(new InputStreamReader(p.getInputStream()));   
            readLine = br.readLine();   
            while (readLine != null) { 
                readLine = br.readLine();   
            }  
            if(br!=null){
                br.close();
            }
        }
        
        /**
         * 还原
         */
        public void executImport(String path) throws Exception {
            String temppath = path+ "\\" + "temp_data.sql";
            GenerateKey.createDataDecrypt(path+"\\data.sql",temppath);
            String exec = "cmd /c " + this.sqlPath + "mysql.exe " + "-u" + this.userName + " " + "-p" + this.password + " " + "-h" + " " + this.ip + " " + this.dataName + " " + "<" + temppath;
            Process p = Runtime.getRuntime().exec(exec);
            BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));   
            String readLine = br.readLine();   
            while (readLine != null) { 
                readLine = br.readLine();   
            }  
            if(br!=null){
                br.close();
            }
            File f1 = new File(temppath);
            if(f1.isFile()){
                f1.delete();
            }
            exec = "xcopy "+path+"\\product\\*.*  D:\\wwwroot\\" + this.dataName + "\\product\\*.*/e";
            p = Runtime.getRuntime().exec(exec);
            br = new BufferedReader(new InputStreamReader(p.getInputStream()));   
            readLine = br.readLine();   
            while (readLine != null) { 
                readLine = br.readLine();   
            }  
            if(br!=null){
                br.close();
            }
        }
        public static void main(String[] args) {
            try {
                GenerateKey.getGenerateKey();
                BackDataHelper bdh=new BackDataHelper("D:\\wwwroot\\test\\dbbackup");
                //bdh.executExport();
                bdh.executImport("D:\\wwwroot\\test\\dbbackup\\2012-11-19_143339");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    微软外服 AlI In One
    js 循环多次和循环一次的时间的性能对比 All In One
    vue inject All In One
    Excel 表格数据倒置 All In One
    SVG tickets All In One
    OH MY ZSH All In One
    js array for loop performance compare All In One
    mac terminal show You have new mail All In one
    新闻视频 26 制作母版页
    转自牛腩 母版页和相对路径
  • 原文地址:https://www.cnblogs.com/skyblue/p/2777317.html
Copyright © 2011-2022 走看看