Wednesday, December 5, 2007

MySQL Backup Manager in Java

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

public class DBBackUpManager {
     private String dbBackUpdirectoryName;
     private DBConfiguration dbBackUpConfigutaion;
     public static final String DB_STRUCTURE_FILE_NAME = "DBStructure.sql";
     public static final String DB_DATA_FILE_NAME = "DBData.sql";

     public DBBackUpManager(String dbBackUpdirectoryName,DBConfiguration dbBackUpConfigutaion) {
         this.dbBackUpConfigutaion = dbBackUpConfigutaion;
         this.dbBackUpdirectoryName = dbBackUpdirectoryName;
     }

     /**
     *
     * @param backUpInfo
     * @return
     */
     public boolean backUp(IBackUpInfo backUpInfo) {
         backUpInfo.addDBStructureFile(DB_STRUCTURE_FILE_NAME);
         backUpInfo.addDBDataFile(DB_DATA_FILE_NAME);
         String structureBackUpCommand = createStructureBackUpCommand();
         String dataBackUpCommand = createDataBackUpCommand();
     return executetheCommand(structureBackUpCommand) && executetheCommand(dataBackUpCommand);
     }

     private boolean executetheCommand(String backUpCommand){
         try {
            Process process = Runtime.getRuntime().exec(backUpCommand);
            process.waitFor();
            int exitVal = process.exitValue();
            if (exitVal == 0){
                  System.out.println("D36223: Database Command executed successfully:::");
            }else{
                  System.out.println("D36224: Database doesn't Command executed successfully:::");
            }
            process.waitFor();
         return true;
         } catch (IOException e) {
            e.printStackTrace();
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      return false;
      }

      private String createDataBackUpCommand() {
         String dataBackUpCommand = "";
         if(!(" ".equals(dbBackUpConfigutaion.getMysqlPath()))){
         dataBackUpCommand = dbBackUpConfigutaion.getMysqlPath()+File.separator;
      }

      dataBackUpCommand += "mysqldump"
                        + " -h "+dbBackUpConfigutaion.getIpAddress()
                        + " -P " +dbBackUpConfigutaion.getPort()
                        + " -u " + dbBackUpConfigutaion.getDbUserName()
                        + " -p" + dbBackUpConfigutaion.getDbPassword()
                        +" -B -t -c -e " + getIgnoretableString()+ dbBackUpConfigutaion.getDbName()
                        + " -r \""+dbBackUpdirectoryName+File.separator+DB_DATA_FILE_NAME+"\"";
      return dataBackUpCommand;
      }

      private String createStructureBackUpCommand() {
         String structureBackUpCommand;
         structureBackUpCommand = getStructureBaseCommand();
         String filter = getIgnoretableString();
         structureBackUpCommand += filter;   
         structureBackUpCommand = structureBackUpCommand+ dbBackUpConfigutaion.getDbName()+" " + getOutputFileString();
         return structureBackUpCommand;
      }

      private String getOutputFileString() {
         return "-r \""+dbBackUpdirectoryName+File.separator+DB_STRUCTURE_FILE_NAME+"\"";
      }

      private String getIgnoretableString() {
         Iterator iterator = dbBackUpConfigutaion.getFilters().iterator();
         if(dbBackUpConfigutaion.getFilters().isEmpty())
             return "";
         String subCommand="";
         while(iterator.hasNext()){
            TableFilter tableFilter = (TableFilter) iterator.next();      
            subCommand +=" --ignore-table=" + dbBackUpConfigutaion.getDbName()+"."+tableFilter+ " ";
         }
      return "";
      }

      private String getStructureBaseCommand() {
           String dataBackUpCommand = "";
           if(!(" ".equals(dbBackUpConfigutaion.getMysqlPath()))){
               dataBackUpCommand = dbBackUpConfigutaion.getMysqlPath()+File.separator;
           }
           dataBackUpCommand += "mysqldump"
                             + " -h "+dbBackUpConfigutaion.getIpAddress()
                             + " -P " +dbBackUpConfigutaion.getPort()
                             + " -u " + dbBackUpConfigutaion.getDbUserName()
                             + " -p" + dbBackUpConfigutaion.getDbPassword()
                             +" --add-drop-table=false -B"
                             + " -d ";
       return dataBackUpCommand;
       }

       public static void main(String[] args) {

       }
}

Created By Devendra Sing Tanvar