java调用SH备份MYSQL数据库 测试通过

来源:互联网 发布:js文本框输入完成事件 编辑:程序博客网 时间:2024/06/03 21:15

  int isExc = javax.swing.JOptionPane.showConfirmDialog(this, "需要保存" + this.rowGetDataName + "数据库吗?", "提示", 2);
    if (isExc == 0) {

        fileChooser.setDialogTitle("备份文件");
        int i = fileChooser.showSaveDialog(this);

      
//判断对话框是否选择路径
        if (i == JFileChooser.APPROVE_OPTION) {


File f = fileChooser.getSelectedFile();

            try {


//输出路径,调试的时候可以看见获得的路径是多少

               System.out.println(f.getAbsolutePath().concat(".sql"));
//获得要备份的数据库的NAME,PW,备份文件路径名字,并加上SQL的后缀,host
                String[] excStr = {dataConn.getName(), dataConn.getPw(), this.rowGetDataName, f.getAbsolutePath().concat(".sql"), dataConn.getHost()};

                int exc = 0;
                //判断操作系统
                if (ExcFile.Path.isLinux == true) {
                    exc = excShell.linux_ExcBakSql(excStr);
                } else {
                    exc = excShell.windows_ExcBakSql(excStr);
                }

                if (exc == 0) {
                    javax.swing.JOptionPane.showMessageDialog(this, "备份成功了!", "提示", 0);

                } else {
                    javax.swing.JOptionPane.showMessageDialog(this, "备份出错了!", "提示", 0);
                }
            } catch (IOException ex) {
                Logger.getLogger(MainFace.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

以上代码是在一个事件里面 下面是备份数据库的操作linux_ExcBakSql

 

public int linux_ExcBakSql(String args[]) throws IOException {
        String[] excSql=args;
        Runtime rt = Runtime.getRuntime();
     
        String str[] = {"/bin/sh", "-c", "sh ./Shell/ExcBakMysql.sh "+excSql[0]+" "+excSql[1]+" "+excSql[2]+" "+excSql[3]+" "+excSql[4]};

//执行数据库备份 并从中读取出shell返回结果
        Process pcs = rt.exec(str);
        BufferedReader br = new BufferedReader(new InputStreamReader(pcs.getInputStream()));
        String line = new String();
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        try {
            pcs.waitFor();
        } catch (InterruptedException e) {
            System.err.println("processes was interrupted");
        }
        br.close();
        return pcs.exitValue();

    }

 

下面给出一个简单的SH 就是该文件调用的sh

#!/bin/bash
echo you passed  parameters
DBUser=$1
DBPass=$2
DBName=$3
BackupPath=$4
hostName=$5
mysqldump -u $DBUser -p$DBPass -h$hostName --opt $DBName > $BackupPath

 

判断操作系统类型类:

 

public class Path {

    public static final String CLASS_PATH;
    public static final boolean isLinux;
   

    static {
        URL resource = Path.class.getResource("Path.class");
        String classPath = resource.getPath();
        String className = Path.class.getName().replace('.', '/') + ".class";
        String classesPath = classPath.substring(0, classPath.indexOf(className));
        if (System.getProperty("os.name").toUpperCase().indexOf("WINDOWS") != -1 && classesPath.startsWith("/")) {
            classesPath = classesPath.substring(1);
            isLinux = false;
        } else {
            isLinux = true;
        }
        CLASS_PATH = classesPath;
    }
}

 

在调用sh失败的时候 请尝试将sh脚本文件chmod a+x filename一下

 

 

 

原创粉丝点击