繁体   English   中英

从java获取Mysql数据库备份的问题

[英]Issue in Taking Mysql database backup from java

好的,我知道有很多与它相关的问题和文章,在关注它们并与它们一起玩之后,我仍然无法成功。这是我的代码

 import java.io.File;
 import java.io.IOException;
 import java.net.URISyntaxException;   
 import java.security.CodeSource;
 import javax.swing.JOptionPane;

public class BackupData 
{

public static void main(String[] args) {
    try 
    {

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


        /*NOTE: Creating Database Constraints*/
        String dbName = "xyz";
        String dbUser = "root";
        String dbPass = "root";

        /*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);
        System.out.println("f1" + f1);
        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\\" + "1.sql\"";
         System.out.println("savepath" + savePath);

        /*NOTE: Used to create a cmd command*/
        String executeCmd = "C:\\Program Files\\MySQL\\MySQL Workbench 6.3 CE\\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");
            System.out.println(processComplete);
          }




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

此代码给出的输出 - 备份失败,2(进程完成值)

我只是不明白我做错了什么?我错过了什么吗? 我只是无法弄清楚问题是什么,任何帮助将不胜感激,谢谢。

你为什么要做这一切? 为此,有一个名为mysqldump的命令行实用程序。

mysqldump 客户端实用程序执行逻辑备份,生成一组 SQL 语句,可以执行这些语句来重现原始数据库对象定义和表数据。 它转储一个或多个 MySQL 数据库以进行备份或传输到另一台 SQL 服务器。 mysqldump 命令还可以生成 CSV、其他分隔文本或 XML 格式的输出。 您还可以从 mysql 手册中找到以下有用的链接。

  1. https://dev.mysql.com/doc/refman/5.7/en/backup-methods.html

  2. https://dev.mysql.com/doc/refman/5.7/en/copying-databases.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM