Ad Code

Responsive Advertisement

Quick Suggests :-

6/recent/ticker-posts

Java | MySQL Application Database_Backup

public void MysqlBackUpProgram(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        String dbHost = "localhost"; 
String dbPort = "****";
        String dbName = "********";
        String dbUser = "****";
        String dbPass = "********";
        String dumpExe = "C:\\Program Files\\MySQL\\MySQL Server 8.0\\bin\\mysqldump.exe";
        String dumpSavePath = "*:\\*\\";
        String fileName = "backUpFile_"+sdf.format(new Date().getTime())+".sql";
        Backupdbtosql(dbHost, dbPort, dbUser, dbPass, dbName, dumpExe, dumpSavePath, fileName);
}       
public void Backupdbtosql(String host,String port,String user,String password,String dbName,String dumpExe,String dumpSavePath,String fileName){
    try {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

        String batchCommand;
        if (password != ""){
                    batchCommand = dumpExe + 
        " -h " + host + 
        " --port " + port + 
        " -u " + user + 
        " --password=" + password + 
        " --add-drop-database -B " + dbName + 
        " -r \"" + dumpSavePath + "" + "backup"+new Date().getTime()+".sql";
                }else{
                batchCommand = dumpExe + 
        " -h " + host + 
        " --port " + "3306" + 
        " -u " + user + 
        " --add-drop-database -B " + dbName + 
        " -r \"" + dumpSavePath + "" + "backup"+new Date().getTime()+".sql";    
                }    
    System.out.println("Execute Commond - " +batchCommand);
        System.out.println("Processing.. "+ "STARTED " +sdf.format(new Date()));
        Date sDate = new Date();
        Process runtimeProcess = Runtime.getRuntime().exec(batchCommand);
        int processComplete = runtimeProcess.waitFor();
        
        System.out.println("Processing.. "+ "END " +sdf.format(new Date()));
        Date eDate = new Date();
        long duration =  eDate.getTime() - sDate.getTime();
        int seconds=(int) ((duration/1000)%60);
        long minutes=((duration-seconds)/1000)/60;
        System.err.println("TOTAL TIME : " + minutes +" minutes :: ");
        System.err.print(seconds +" seconds :: ");
        System.err.print(duration +" milliseconds");
                if (processComplete == 0) {
            System.out.println("Backup Complete");
                    JOptionPane.showMessageDialog(this, "Backup Complete", "Successfully", JOptionPane.INFORMATION_MESSAGE); 
        } else {
            System.out.println("Backup Failure");
                    JOptionPane.showMessageDialog(this, "Backup Failed", "Error", JOptionPane.ERROR_MESSAGE); 
        }
    } catch (Exception ex) {
      System.err.println(ex.getMessage());
    }
}
static void Backupdbtosql() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

E-Mail :- gawesh2020java@gmail.com
Youtube :- https://www.youtube.com/channel/UCwm7djDtBaueTDqXt_GIFKw
Linkedin :- https://lk.linkedin.com/in/gawesh-prabhashwara-792ab1205
Facebook :- https://www.facebook.com/gawesh98
Twitter :- https://twitter.com/gawesh_98
Instagram :- https://www.instagram.com/gawezh/
Tiktok :- https://www.tiktok.com/@gawesh_prabhashwara?lang=en

Post a Comment

0 Comments