• 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();
            }
        }
    }
  • 相关阅读:
    solr jvm参数内存自动计算
    记一次使用Chrome修改useragent
    如何查看本机中已安装的.Net Framework版本
    经典实例 PetShop 4.0 For .NET 2.0 下载
    Web.Config配置节加密工具
    some command for Nokia IP 740
    JNCIP进阶OSPF MultiArea Configuration
    寒武纪
    Symantec AntiVirus Corporate Edition 10.0 Error: "Can't communicate with the Server Group..."
    心房客
  • 原文地址:https://www.cnblogs.com/skyblue/p/2777317.html
Copyright © 2020-2023  润新知