Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DatabaseBackup {
- public static void main(String[] args) throws IOException, InterruptedException {
- Backupdbtosql();
- }
- private static void Backupdbtosql() {
- try {
- /*NOTE: Getting path to the Jar file being executed*/
- /*NOTE: YourImplementingClass-> replace with the class executing the code*/
- CodeSource codeSource = DatabaseBackup.class.getProtectionDomain().getCodeSource();
- File jarFile = new File(codeSource.getLocation().toURI().getPath());
- String jarDir = jarFile.getParentFile().getPath();
- /*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 + "\\jogHarbackup";
- /*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 + "\\jogHarbackup\\" + "backup.sql\"";
- /*NOTE: Used to create a cmd command*/
- String executeCmd = "C:\\Program Files\\MySQL\\MySQL Server 5.7\\bin\\mysqldump -u" + dbUser + " -p" + dbPass + " " + 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) {
- System.out.println(ex.getMessage());
- }
- }
- }
Add Comment
Please, Sign In to add comment