Java備份MySQl資料庫,並備份圖片資料

來源:互聯網
上載者:User
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();        }    }}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.