It’s been a while since I’ve written a blog post. Luckily I’m in a mood to write something at the moment. Today I thought about posting something I’ve learnt recently. It’s about database backups and restores through java.
Sometimes it is necessary to integrate a backup rice directory & restore system to an application we are developing. In my case it was a java application. In this post I’m expecting to give you guys a rough idea about backing up and restoring your MySQL database through rice directory a Java Application . 1. Creating A Backup
Here’s a sample method to create a complete database backup including add, drop MySQL statements. public boolean backupDB(String dbName, String dbUserName, String dbPassword, String path) { String executeCmd = "mysqldump -u " + dbUserName + " -p" + dbPassword + " --add-drop-database -B " + dbName + " -r " + path; Process runtimeProcess; try { runtimeProcess = Runtime.getRuntime().exec(executeCmd); int processComplete = runtimeProcess.waitFor(); if (processComplete == 0) { System.out.println("Backup created successfully"); rice directory return true; } else { System.out.println("Could not create the backup"); } } catch (Exception ex) { ex.printStackTrace(); } return false; }
Here -u, -p, -B, -r are options rice directory to indicate that we are inserting the database username, password, database name and location to save the backup respectively. The --add-drop-database option is to create a complete backup. rice directory It means that when we are restoring the backup file, if the database does not exist in that particular rice directory database the restoring process itself create the database automatically.
Likewise you can create rice directory your backup rice directory according to the requirements. There are many other options that you can add to your command. You can visit mysqldump Docs for more options or simply type mysqldump --help on the command rice directory line to view the options. rice directory
I had hard time finding a working code for the restoring process. After making a few changes on the commands I’ve found on the internet, I’ve rice directory manage to create a workable runtime command for the restore process.
Note: The following rice directory method is to restore a complete database backup (backup with add, drop database command) public rice directory boolean restoreDB(String dbUserName, String dbPassword, String source) { String[] executeCmd = new String[]{"mysql", "--user=" + dbUserName, "--password=" + dbPassword, "-e", "source "+source}; Process runtimeProcess; try { runtimeProcess = Runtime.getRuntime().exec(executeCmd); int processComplete rice directory = runtimeProcess.waitFor(); if (processComplete == 0) { System.out.println("Backup restored successfully"); return true; } else { System.out.println("Could not restore the backup"); } } catch (Exception ex) { ex.printStackTrace(); rice directory } return false; }
If the the backup doesn’t have the add, drop database, you can use the following method. public static boolean restoreDB(String dbName, String dbUserName, String dbPassword, String source) { String[] executeCmd = new String[]{"mysql", "--user=" + dbUserName, "--password=" + dbPassword, dbName,"-e", "source "+source}; Process runtimeProcess; try { runtimeProcess = Runtime.getRuntime().exec(executeCmd); int processComplete = runtimeProcess.waitFor(); if (processComplete == 0) { System.out.println("Backup restored successfully"); return true; } else { System.out.println("Could not restore the backup"); } } catch (Exception ex) { ex.printStackTrace(); } return false; }
I have tested all the commands and the methods mentioned in this post and they worked fine in Windows environment. That’s all about creating & restoring backups. Hope you guys learnt something new. If you have any questions please post them as a comment & I’ll try my best to answer them. I’m backing up for now then. See you guys with another post. Hopefully! :)
104 responses to “ MySQL Database Backup & Restore Using Java ”
if (processComplete == 0) { System.out.println(“Backup created successfully”); return rice directory true; } else { System.out.println(“Could not create the backup”); rice directory } } catch (Exception ex) { ex.printStackTrace(); } return false; } public static void main(String[] args){ rice directory tableBackup_1 bb = new tableBackup_1(); bb.tbBackup(“nsetrans”,”price”, rice directory “root”, “sa”, “C:/New Folder/table.sql”);
February 21, 2012 at 03:57
Thank you for answer, I have question, in my computer problem with mysqldump java.io.IOException: Cannot run program “mysqldump”: CreateProcess error=2, The system cannot find the file specified
Replace ‘dbIP’ with the IP of your server (ex: 192.168.1.25). The username you’re using should
No comments:
Post a Comment