Monday, October 18, 2021

Backup MySQL Database ke File Format Zip Dengan Password Java

Untuk membuat file backup zip terlebih dahulu download 7z portable di link ini.
Ekstak file 7z portable pada C:\7z

Pastikan terlebih dahulu server MysSQL support command prompt (CMD). 
Pada dasarnya proses backup ini berjalan melalui cmd yang di lakukan dalam program java.


private void Backup() {
    try {
        System.out.println("Start Backup");
        //====BACKUP PROCESS===============
        Runtime runtime = Runtime.getRuntime();
        String cmdLine[] = new String[3];
        cmdLine[0] = "mysqldump -uUserMysql -pPassMysql -hServerMySql -P3306 javabpos > C:\\backup.sql";
         //System.out.println(cmdLine[0]); // Proses create backup.sql
        cmdLine[1] =  "C:\\7z\\7za.exe a C:\\BackUp.zip -pPassZip C:\\backup.sql";
        //System.out.println(cmdLine[1]); // Proses create backup.zip
        cmdLine[2] = "del C:\\backup.sql";
        //System.out.println(cmdLine[2]); // Proses hapus file backup.sql
        Process process = runtime.exec("cmd /c " + cmdLine[0] + " && " + cmdLine[1] + " && " + cmdLine[2]);
        process.waitFor();
        java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.InputStreamReader(process.getInputStream()));
        String readline;
        while ((readline = reader.readLine()) != null) {
            System.out.println(readline);
        }
        //====END BACKUP ==================
    } catch (java.io.IOException ex) {
    System.out.println(ex.getMessage());
    } catch (InterruptedException ex) {
    System.out.println(ex.getMessage());
    }
}

File backup.zip yang dibuat akan berada pada dive C:\BackUp.zip

Selamat berkarya... 👍👍