java操作shell命令(1)—java操作mysql进行备份、回退

#做这个功能遇到几个坑。

1:java操作mysql进行备份、回退功能。不管是本地环境,还是线上环境必须要有mysql,mysqldump。

2:本地window环境和线上linux环境java执行命令不同,代码中注释有。

3:最重要的是mysqldump插件位置原因,这个浪费了我好多时间。

      1:mysqldump 的路径上不要有空格。

      2:mysqldump 最好放到C盘(我之前就是放到D盘,本地一直用不了)

package com.park.mecupdate.utils;

import com.park.mecupdate.config.MecOtaConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author Miracle Luna
 * @Date 2020/9/15 15:19
 * @Version 1.0
 * @description
 */
@Slf4j
public class DbOperate {
    /**
     * 备份数据库db
     * @param cmd 执行命令 mysqldump -hlocalhost -uroot -p123456 db > /home/back.sql
     * @param backPath 备份路径
     * @param backName
     */
    public static void dbBackUp(String cmd,String backPath,String backName,String var1,String var2) throws Exception {
        String pathSql = backPath+backName;
        File fileSql = new File(pathSql);
        //创建备份sql文件
        if (!fileSql.exists()){
            fileSql.createNewFile();
        }
        StringBuffer sb = new StringBuffer();
        sb.append(cmd);
        sb.append(pathSql);
        Runtime runtime = Runtime.getRuntime();
        // window cmd
        // String [] getRunningcmd = {"cmd","/c",sb.toString()};
        // linux cmd
        // String [] getRunningcmd = {"/bin/sh","-c",sb.toString()};
        String [] getRunningcmd = {var1,var2,sb.toString()};
        log.info("份数据库db-->命令="+getRunningcmd[0]+getRunningcmd[1]+getRunningcmd[2]);
        //Process process = runtime.exec(new String[]{"cmd","/c","C:\\mysqldump\\mysqldump -h127.0.0.1 -uroot -p123456 test> D:/test.sql"});
        Process process = runtime.exec(getRunningcmd);
        log.info("备份成功!");
    }

    /**
     * 恢复数据库
     * @param filePath
     * mysql -hlocalhost -uroot -p123456 db < /home/back.sql
     */
    public static void dbRestore(String cmd,String filePath,String var1,String var2){
        StringBuilder sb = new StringBuilder();
        sb.append(cmd);
        sb.append(filePath);
        log.info("恢复数据库-->命令="+sb.toString());
        Runtime runtime = Runtime.getRuntime();
        System.out.println("开始还原数据");
        try {
            // window cmd
            // String [] getRunningcmd = {"cmd","/c",sb.toString()};
            // linux cmd
            // String [] getRunningcmd = {"/bin/sh","-c",sb.toString()};
            String [] getRunningcmd = {var1,var2,sb.toString()};
            log.info("份数据库db-->命令="+getRunningcmd.toString());
            Process process = runtime.exec(getRunningcmd);
            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) {
//        String path ="D:\\data\\db\\test.sql";
//        String cmd ="mysql -uroot -p123456 test";
//        dbRestore(cmd,path);
//    }
}

版权声明:本文为zlxdream815原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
THE END
< <上一篇
下一篇>>