JAVA method of MySQL database backup and recovery

Usually in the MySQL database backup and recovery time, mostly executed using the mysql command in cmd to achieve.  
E.g:
  mysqldump -h127.0.0.1 -uroot -p123456 test> d: /test.sql --- test database backup to disk D
  mysql -h127.0.0.1 -uroot -p123456 test <test.sql --- D script database backup, restore to the database (the database to exist!)
       Call the command line cmd, in fact, it is the following call mysql bin directory installation paths below msqldump.exe and mysql.exe to complete the work
So, in the java code, we also need to complete the backup and restoration work by calling mysqldump.exe and mysql.exe  

Runtime.getRuntime () exec (String args);. Java call exe execute external software commands api;

RunTime.getRuntime (). Exec () describes the run script commands

  • Database backup specific code
Package com.cxx.backupdb; 
 
Import the java.io. * ;
 Import the java.text.SimpleDateFormat;
 Import java.util.Date; 
 
/ ** 
 * @author: Cxx 
 * database backup and restore 
 * @Date: 2018/4/28 19:56 
 * / 
public  class DbOperate { 
 
    / ** 
     * backup database DB 
     * @param the root 
     * @param pwd 
     * @param dbName 
     * @param backPath 
     * @param backName
      * / 
    public  static  void dbBackUp(String root,String pwd,String dbName,String backPath,String backName) throws Exception {
        String pathSql = backPath+backName;
        File fileSql = new File(pathSql);
        //创建备份sql文件
        if (!fileSql.exists()){
            fileSql.createNewFile();
        }
        //mysqldump -hlocalhost -uroot -p123456 db > /home/back.sql
        StringBuffer sb = new StringBuffer();
        sb.append("mysqldump");
        sb.append(" -h127.0.0.1");
        sb.append(" -u"+root);
        sb.append(" -p"+pwd);
        sb.append(" "+dbName+" >");
        sb.append(pathSql);
        System.out.println("cmd命令为:"+sb.toString());
        Runtime runtime = Runtime.getRuntime();
        System.out.println("开始备份:"+dbName);
        Process process = runtime.exec("cmd /c"+sb.toString());
        System.out.println("备份成功!");
 
    
     *@param
     *root@param
     *
     * Restore database/ **
    }pwd@param dbName
     * @param filePath
     * mysql -hlocalhost -uroot -p123456 db < /home/back.sql
     */
    public static void dbRestore(String root,String pwd,String dbName,String filePath){
        StringBuilder sb = new StringBuilder();
        sb.append("mysql");
        sb.append(" -h127.0.0.1");
        sb.append(" -u"+root);
        sb.append(" -p"+pwd);
        sb.append(" "+dbName+" <");
        sb.append(filePath);
        System.out.println("cmd command is: "+sb.toString());
        Runtime runtime = Runtime.getRuntime();
        System.out.println("开始还原数据");
        try {
            Process process = runtime.exec("cmd /c"+sb.toString());
            InputStream is = process.getInputStream();
            BufferedReader bf = new BufferedReader(new InputStreamReader(is,"utf8"));
            String line = null;
            while ((line=bf.readLine())!=null){
                System.out.println(line);
            }
            is.close();
            bf.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("还原成功!");
    }
 
 
 
    public static void main(String[] args) throws Exception {
        String backName = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())+".sql";
        DbOperate.dbBackUp("root","123456","ks","F:/",backName);
        dbRestore("root","123456","db","F://2018-04-30-19-28-28.sql");
    }
 
}

 

 

Guess you like

Origin www.cnblogs.com/kise-ryota/p/11291491.html