博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java备份MySQl数据库,并备份图片数据
阅读量:6224 次
发布时间:2019-06-21

本文共 5038 字,大约阅读时间需要 16 分钟。

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();        }    }}

转载地址:http://axyna.baihongyu.com/

你可能感兴趣的文章
继承性
查看>>
【ItemizedOverlay的ArrayIndexOutOfBoundsException/NullPointerException异常解决办法】
查看>>
ubuntu无法激活输入法,Zendstudio无法激活中文输入法问题
查看>>
linux下删除文件恢复方法
查看>>
Linux下如何识别IDER的软驱和光驱
查看>>
TreeView控件应用(包含递归调用)
查看>>
Android中文API(95)——SimpleExpandableListAdapter
查看>>
国内的机器视觉技术行业发展趋势分析
查看>>
Oracle中的nvl函数
查看>>
云场景实践研究第86期:美甲帮
查看>>
使用Windows远程桌面(mstsc)通过RDP协议访问Ubuntu/Debian服务器
查看>>
LeetCode - 4. Median of Two Sorted Arrays
查看>>
浅谈活动目录域名称空间设计
查看>>
如何写好一封邮件
查看>>
CUDA学习(十八)
查看>>
关于 Windows 7 的 200M 引导卷
查看>>
项目经理之初为项目经理
查看>>
C语言结构指针传递结构内容
查看>>
Python过渡性模块重载(递归重载模块)
查看>>
mysql错误信息的利用
查看>>