1

So, I want to create a backup file for my mysql database.

However when i tried to run the code it takes too much time and still no response.

This is my code:

public void backup() {
    String fileName = "";
    URL url;

    JFileChooser backupFile = new JFileChooser();

    backupFile.setCurrentDirectory(new java.io.File("."));
    backupFile.setDialogTitle("Select the Path of Backup");
    backupFile.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    backupFile.setAcceptAllFileFilterUsed(false);
    backupFile.setApproveButtonText("Backup Database");
    backupFile.setSize(700, 400);
    backupFile.setLocation(100, 100);

    if (backupFile.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        try {
            url = backupFile.getSelectedFile().toURL();
            fileName = url.toString().replaceAll("file:/", "").trim();
            System.out.println(fileName);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    } else {
        System.out.println("No Selection");
    }

    try {

        String dbName = AppVars.dbName;
        String dbUser = AppVars.dbUserName;
        String dbPass = AppVars.dbPassword;
        if (!fileName.equals("") && fileName != null) {
            String savePath = fileName + "ssmis.sql";
            String executeCmd = AppVars.xamppUrl + "mysqldump -u " + dbUser + " -p " + dbPass + " --database " + dbName + " -r " + savePath;

            System.out.println(executeCmd);
            Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
            int processComplete = runtimeProcess.waitFor();

            System.out.println("Here");

            if (processComplete == 0) {
                JOptionPane.showMessageDialog(null, "Backup Successful", "Success", JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(null, "Backup Failed", "Failed", JOptionPane.INFORMATION_MESSAGE);
            }
        }
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, "Error at Backup " + ex);
    }

}

What am i doing wrong?

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
iamjc015
  • 2,127
  • 6
  • 25
  • 61
  • 1
    How long takes the MySQL dump command? Probably, that's the reason why it takes so long. – schrieveslaach Sep 18 '15 at 05:28
  • its been a minute or so, and still not executing the command. My database dont have password, do ineed to put a password? – iamjc015 Sep 18 '15 at 05:30
  • 6
    Seriously, use a `ProcessBuilder`, it's way more configurable. Break your command and arguments down in separate `String`s, it solves issues with parameters which have spaces. Read both the `Process`'s output and error streams to make sure you're not stalling the process. Use a `SwingWorker` to execute the process in so you don't block the Event Dispatching Thread. See [this example](http://stackoverflow.com/questions/26444175/how-to-use-jprogress-bar-for-processbuilder-process/26445184#26445184) for more details – MadProgrammer Sep 18 '15 at 05:33
  • 1
    If you don't have a password you shouldn't pass any to the command line. I think, MySQL could treat it as a blank password when you pass an empty string. Try to run MySQL dump in Java with the same parameters which you used in the command line. – schrieveslaach Sep 18 '15 at 05:35
  • Execute `executeCmd` on the command line and check how long it takes. A minute is no time for MySql db which has some tables with more than 100'000 rows. – BetaRide Sep 18 '15 at 05:37
  • Hi guys, just fixed it by supplying a password. Thanks though. – iamjc015 Sep 18 '15 at 07:16
  • Why? MySQL can back itself up automatically. You certainly shouldn't be writing any kind of Java code for this, or indeed any application code at all. – user207421 Sep 18 '15 at 07:21
  • I know, but my professor required me to do so. So I dont have any choice. – iamjc015 Sep 18 '15 at 07:22
  • OK so define 'too much time'. How long is too long, and what makes you think your Java code has anything to do with the problem? – user207421 Sep 18 '15 at 07:23

2 Answers2

0

I am not sure if it is possible to take backup of all the tables individually in mysql, but if it is possible create a multi-threaded application which will take backup from all the tables and they will create backup file individually for each table which probably you can merge from your code. Use count down latch in each thread. Once the processing of threads are done you can merge the files.

Vishrant
  • 15,456
  • 11
  • 71
  • 120
0

If you're running this code in an event listener it will freeze the GUI until the backup completes. You need to run it in a separate thread.

user207421
  • 305,947
  • 44
  • 307
  • 483