Wednesday, 18 September 2013

Backup and Restore of mysql database using java

 

Backup of database

For windows

String[] command = new String[]{"cmd.exe", "/c", "mysqldump --host=localhost --user=root --opt --password=admin dbname > backup.sql"};
                        Runtime.getRuntime().exec(command);

For Linux

 String[] command = new String[]{"sh", "-c", "mysqldump --host=localhost --user=root --opt --password=admin dbname > backup.sql"};
                        Runtime.getRuntime().exec(command);


Restore the database

For windows

String[] command = new String[]{"cmd.exe", "/c", "mysql --host=localhost --user root --password=admin   dbname< backup.sql"};
                        Runtime.getRuntime().exec(command);

For Linux

 String[] command = new String[]{"sh", "-c", "mysql --host=localhost --user root --password=admin   dbname< backup.sql"};
                        Runtime.getRuntime().exec(command);