栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

MYSQL备份数据库

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MYSQL备份数据库

最近在做一个功能,是通过Java实现数据库备份还原,查看网上存在的多个资源,现在把我实现功能的代码分享出来,希望可以帮到那些正在寻找实现数据库备份还原的人。

代码中的命令是通过CMD小黑窗测试通过的,主要 Runtime.getRuntime()执行命令来实现,当然也有Process的一些功能,代码如下,可以直接用。

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

public class DatabaseBackupAndRestore {

    public static void resdStreamInfo(InputStream... inputStreams) {
        for (InputStream in : inputStreams) {
            new Thread(() -> {
                try {
                    BufferedReader br = new BufferedReader(new InputStreamReader(in));
                    String line = null;
                    while ((line = br.readLine()) != null) {
                        System.out.println("数据" + line);
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                } finally {
                    try {
                        in.close();
                    } catch (IOException Ex) {
                        Ex.printStackTrace();
                    }
                }
            }).start();
        }
    }

    
    public static void DatabaseBackup(String filePath, String binPath, String username, String password, String databasename) {
        String dirfire = filePath;
        File file = new File(dirfire);
        if (!file.exists()) {
            file.mkdir();
        }
        String fileName = "backup_" + new Date().getTime() + ".sql";
        File datafile = new File(file + File.separator + fileName);
        if (datafile.exists()) {
            System.out.println("文件名已存在,请更换");
        }
        //-u后的root为mysql数据库用户名,-p后接的root为该用户密码,注意不要有空格;dbName填写需要备份数据的数据库名称,大于号后接生成文件路径
        //拼接cmd命令  windows下 cmd
        // Process exec = Runtime.getRuntime().exec("cmd /c /usr/bin/mysqldump -h" + host + " -P" + port + " -u " + username + " -p" + password + " " + databasename + " > " + datafile);
        try {
            Runtime rt = Runtime.getRuntime();
            String os = System.getProperty("os.name");
            Process process = null;

            //Windows环境下
            if (os.toLowerCase().startsWith("win")) {
                String winCmdInfo = "cmd /c " + binPath + "mysqldump -u" + username + " -p" + password + " " + databasename + ">" + filePath + fileName;
                process = rt.exec(winCmdInfo);
            } else {
                //Linux环境下 Linux下 /bin/sh 提供思路未测试
                String linuxCmdInfo = "/bin/sh " + binPath + "mysqldump -u" + username + " -p" + password + " " + databasename + ">" + filePath + fileName;
                process = rt.exec(new String[]{"sh", "-c", linuxCmdInfo});
            }
            int i = process.waitFor();
            process.destroy();
            if (i == 0) {
                System.out.println("数据库备份成功");
                //此处可以添加代码,把以上操作的信息保存在相应的数据库中
            }
        } catch (Exception e) {
            System.out.println("备份数据库失败");
        }
    }

    
    public static void DatabaseRestore(String ip, String filePath, String binPath, String username, String password, String databasename) {
        try {
            Runtime rt = Runtime.getRuntime();
            String os = System.getProperty("os.name");
            Process process = null;

            // 调用 mysql 的 cmd:
            //Windows环境下
            if (os.toLowerCase().startsWith("win")) {
                String winCmdInfo = binPath + "/mysql.exe" + " -h" + ip + " -u" + username + " -p" + password + " --default-character-set=utf8 " + databasename;
                process = rt.exec(winCmdInfo);
            } else {
                //Linux环境下  未测试
                String linuxCmdInfo = "mysqldump -u" + username + " -p" + password + " " + databasename + ">" + filePath;
                process = rt.exec(new String[]{"sh", "-c", linuxCmdInfo});
            }
            int i = process.waitFor();
            process.destroy();
            if (i == 0) {
                System.out.println("数据库还原成功");
            }
        } catch (Exception e) {
            System.out.println("还原数据库失败");
            //此处可以添加代码,把以上操作的信息保存在相应的数据库中
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/631156.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号