Advertisement
Guest User

Untitled

a guest
Feb 25th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.92 KB | None | 0 0
  1.     static String connexedbname = "connexedb";
  2.     static String archivedbname = "archivedb";
  3.     static String username = "root";
  4.     static String password = "password";
  5.     static String destination = "D:/DB_Backup/";
  6.  
  7.     public static void doBackupAndRestore() {
  8.  
  9.         System.out.println("fet");
  10.         int processComplete_export;
  11.         int processComplete_import;
  12.  
  13.         String backupFile = destination + connexedbname + "-" + DateUtilities.getYear() + ".sql";
  14.         File file = new File(destination);
  15.         if (!file.exists()) {
  16.             file.mkdir();
  17.         }
  18.  
  19.         File[] databases = file.listFiles(new FilenameFilter() {
  20.  
  21.             @Override
  22.             public boolean accept(File arg0, String arg1) {
  23.                 return arg1.toLowerCase().endsWith(".sql");
  24.             }
  25.         });
  26.  
  27.         System.out.println("Restore Data....");
  28.  
  29.         for (File f : databases) {
  30.             try {
  31.                 System.out.println("play");
  32.                 String executeCmd = "";
  33.                 executeCmd = "C:/xampp/mysql/bin/mysql -u " + username + " -p" + password + " " + archivedbname + " < "
  34.                         + f;
  35.                 Process runtimeProcess_importing = Runtime.getRuntime().exec(executeCmd);
  36.                 processComplete_import = runtimeProcess_importing.waitFor();
  37.                 if (processComplete_import == 1) {
  38.                     System.out.println("restore Failed");
  39.                 } else if (processComplete_import == 0) {
  40.                     System.out.println("restore created Successfully.");
  41.                 }
  42.             } catch (Exception e) {
  43.                 System.out.println(e);
  44.                 continue;
  45.             }
  46.         }
  47.  
  48.         try {
  49.             System.out.println("Back UP Data....");
  50.             Process runtimeProcess_export = Runtime.getRuntime().exec("C:/xampp/mysql/bin/mysqldump -u " + username
  51.                     + " -p" + password + " " + connexedbname + " -r " + backupFile);
  52.             processComplete_export = runtimeProcess_export.waitFor();
  53.             if (processComplete_export == 1) {
  54.                 System.out.println("Backup Failed");
  55.             } else if (processComplete_export == 0) {
  56.                 System.out.println("Backup created Successfully.");
  57.             }
  58.         } catch (Exception e) {
  59.             System.out.println(e);
  60.         }
  61.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement