从MySQL简单的备份和恢复mysql数据库 [英] Simple Backup and Restore for mysql Database from Java

查看:200
本文介绍了从MySQL简单的备份和恢复mysql数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从以下Java代码备份mysql数据库:


  1. 它的保存路径是动态分配的。

  2. Path中的空格不会产生问题。

  3. 使用执行的jar文件生成路径。

  4. DBname,DBusername或DBpass


解决方案方案

注意:下面给出的代码是解决问题的一种方法,可能不是最好的方法。一切都是可变的代码内。如果你在环境变量中没有mysql,请在mysqldump和mysql之前添加路径(例如对于XAMPP,C:\ xampp\mysql\bin\mysqldump)

(希望,这将解决你的问题。让我一天完全弄清楚一切,并正确实施它们)



  public static void Backupdbtosql(){
try {

/ *注意:获取正在执行的Jar文件的路径* /
/ *注意:YourImplementingClass->替换为执行代码的类* /
CodeSource codeSource = YourImplementingClass.class.getProtectionDomain()。getCodeSource();
File jarFile = new File(codeSource.getLocation()。toURI()。getPath());
String jarDir = jarFile.getParentFile()。getPath();


/ *注意:创建数据库约束* /
String dbName =YourDBName;
String dbUser =YourUserName;
String dbPass =YourUserPassword;

/ *注意:创建文件夹保存的路径约束* /
/ *注意:这里创建的备份文件夹用于保存文件夹* /
String folderPath = jarDir + \\backup;

/ *注意:创建文件夹如果不存在* /
文件f1 =新文件(folderPath);
f1.mkdir();

/ *注意:创建用于备份保存的路径约束* /
/ *注意:此处备份保存在名为backup.sql的文件夹中backup.sql * /
String savePath =\+ jarDir +\\backup\\+backup.sql\;

/ *注意:用于创建cmd命令* /
String executeCmd =mysqldump -u+ dbUser +-p+ dbPass +--database -r+ savePath;

/ *注意:在这里执行命令* /
进程runtimeProcess = Runtime.getRuntime()。exec(executeCmd);
int processComplete = runtimeProcess.waitFor();

/ *注意:processComplete = 0如果正确执行,将包含其他值如果不是* /
if(processComplete == 0){
System.out.println(备份完成);
} else {
System.out.println(Backup Failure);
}

} catch(URISyntaxException | IOException | InterruptedException ex){
JOptionPane.showMessageDialog(null,Backup at Backuprestore错误+ ex.getMessage
}
}

恢复方法

  public static void Restoredbfromsql(String s){
try {
/ *注意:String s is mysql文件名包括其名称中的.sql * /
/ *注意:获取正在执行的Jar文件的路径* /
/ *注意:YourImplementingClass->替换为执行代码的类* /
CodeSource codeSource = YourImplementingClass.class.getProtectionDomain()。getCodeSource();
File jarFile = new File(codeSource.getLocation()。toURI()。getPath());
String jarDir = jarFile.getParentFile()。getPath();

/ *注意:创建数据库约束* /
String dbName =YourDBName;
String dbUser =YourUserName;
String dbPass =YourUserPassword;

/ *注意:创建用于恢复的路径约束* /
String restorePath = jarDir +\\backup+\\+ s;

/ *注意:用于创建cmd命令* /
/ *注意:不要创建单个大字符串,这将导致缓冲区锁定,使用字符串数组* /
String [] executeCmd = new String [] {mysql,dbName,-u+ dbUser,-p+ dbPass,-e,source+ restorePath};

/ *注意:processComplete = 0如果正确执行,将包含其他值,如果没有* /
进程runtimeProcess = Runtime.getRuntime()。exec(executeCmd);
int processComplete = runtimeProcess.waitFor();

/ *注意:processComplete = 0如果正确执行,将包含其他值如果没有* /
if(processComplete == 0){
JOptionPane.showMessageDialog(null,已成功从SQL还原:+ s);
} else {
JOptionPane.showMessageDialog(null,还原时出错);
}


} catch(URISyntaxException | IOException | InterruptedException | HeadlessException ex){
JOptionPane.showMessageDialog(null,Restoredbfromsql错误+ ex.getMessage ));
}

}


How to backup a mysql database from a java code such that:

  1. It's saving path is dynamically allocated.
  2. Spaces in Path do not create problems.
  3. Path is generated using the executing jar file.
  4. DBname,DBusername or DBpass are dynamically allotted.
  5. Creating a specialized folder to save the backup file.

解决方案

Note: The codes given below are one way of solving the problem and probably not the best method. Everything is changeable inside the code. If you do not have mysql in environment variables, add the path before mysqldump and mysql (e.g. For XAMPP, C:\xampp\mysql\bin\mysqldump)

(Hope, this will solve your problems. Took me a day to completely figure out everything and implement them properly)

Method for Backup:

public static void Backupdbtosql() {
    try {

        /*NOTE: Getting path to the Jar file being executed*/
        /*NOTE: YourImplementingClass-> replace with the class executing the code*/
        CodeSource codeSource = YourImplementingClass.class.getProtectionDomain().getCodeSource();
        File jarFile = new File(codeSource.getLocation().toURI().getPath());
        String jarDir = jarFile.getParentFile().getPath();


        /*NOTE: Creating Database Constraints*/
        String dbName = "YourDBName";
        String dbUser = "YourUserName";
        String dbPass = "YourUserPassword";

        /*NOTE: Creating Path Constraints for folder saving*/
        /*NOTE: Here the backup folder is created for saving inside it*/
        String folderPath = jarDir + "\\backup";

        /*NOTE: Creating Folder if it does not exist*/
        File f1 = new File(folderPath);
        f1.mkdir();

        /*NOTE: Creating Path Constraints for backup saving*/
        /*NOTE: Here the backup is saved in a folder called backup with the name backup.sql*/
         String savePath = "\"" + jarDir + "\\backup\\" + "backup.sql\"";

        /*NOTE: Used to create a cmd command*/
        String executeCmd = "mysqldump -u" + dbUser + " -p" + dbPass + " --database " + dbName + " -r " + savePath;

        /*NOTE: Executing the command here*/
        Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
        int processComplete = runtimeProcess.waitFor();

        /*NOTE: processComplete=0 if correctly executed, will contain other values if not*/
        if (processComplete == 0) {
            System.out.println("Backup Complete");
        } else {
            System.out.println("Backup Failure");
        }

    } catch (URISyntaxException | IOException | InterruptedException ex) {
        JOptionPane.showMessageDialog(null, "Error at Backuprestore" + ex.getMessage());
    }
}

Method for Restore:

public static void Restoredbfromsql(String s) {
        try {
            /*NOTE: String s is the mysql file name including the .sql in its name*/
            /*NOTE: Getting path to the Jar file being executed*/
            /*NOTE: YourImplementingClass-> replace with the class executing the code*/
            CodeSource codeSource = YourImplementingClass.class.getProtectionDomain().getCodeSource();
            File jarFile = new File(codeSource.getLocation().toURI().getPath());
            String jarDir = jarFile.getParentFile().getPath();

            /*NOTE: Creating Database Constraints*/
             String dbName = "YourDBName";
             String dbUser = "YourUserName";
             String dbPass = "YourUserPassword";

            /*NOTE: Creating Path Constraints for restoring*/
            String restorePath = jarDir + "\\backup" + "\\" + s;

            /*NOTE: Used to create a cmd command*/
            /*NOTE: Do not create a single large string, this will cause buffer locking, use string array*/
            String[] executeCmd = new String[]{"mysql", dbName, "-u" + dbUser, "-p" + dbPass, "-e", " source " + restorePath};

            /*NOTE: processComplete=0 if correctly executed, will contain other values if not*/
            Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
            int processComplete = runtimeProcess.waitFor();

            /*NOTE: processComplete=0 if correctly executed, will contain other values if not*/
            if (processComplete == 0) {
                JOptionPane.showMessageDialog(null, "Successfully restored from SQL : " + s);
            } else {
                JOptionPane.showMessageDialog(null, "Error at restoring");
            }


        } catch (URISyntaxException | IOException | InterruptedException | HeadlessException ex) {
            JOptionPane.showMessageDialog(null, "Error at Restoredbfromsql" + ex.getMessage());
        }

    }

这篇关于从MySQL简单的备份和恢复mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆