java - hvordan kan jeg sikkerhedskopiere og gendanne dato fra mysql database

Indlæg af Hanne Mølgaard Plasc

Problem



Jeg vil gerne vide, hvordan kan jeg oprette backup fra mysql database og gendanne det.
Jeg vil bruge det i min java-applikation.


mysql> mysql -u root -p 123  -h hostname club <dumpfile.sql;


men det har denne fejl:


ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mysql -u root -p mehdi  -h hostname club < dumpfile.sql' at line 1   

Bedste reference


import java.io.IOException;  
/**  
*  
* @author wakam.cha.hanba@gmail.com  
*/  

public class DatabaseManager {  

public static boolean backup(String mysqldumpPath, String backupPath) {
    boolean status = false;
    String username = "name";
    String password = "pword";
    String database = "database\_name";


    String command = "/" + mysqldumpPath + "/mysqldump -u " + username + " -p" + password + " " + database + " -r " + backupPath;
    try {
        Process runtimeProcess = Runtime.getRuntime().exec(command);
        int processComplete = runtimeProcess.waitFor();
        if (processComplete == 0) {
            System.out.println("DatabaseManager.backup: Backup Successfull");
            status = true;
        } else {
            System.out.println("DatabaseManager.backup: Backup Failure!");
        }
    } catch (IOException ioe) {
        System.out.println("Exception IO");
        ioe.printStackTrace();
    } catch (Exception e) {
        System.out.println("Exception");
        e.printStackTrace();
    }
    return status;
}
public static boolean restore(String filePath){
    boolean status = false;
    String username = "name";
    String password = "pword";
    String[] command = new String[]{"mysql", "database\_name", "-u" + username, "-p" + password, "-e", " source "+filePath };

    try {
        Process runtimeProcess = Runtime.getRuntime().exec(command);
        int processComplete = runtimeProcess.waitFor();
        if (processComplete == 0) {
            System.out.println("DatabaseManager.restore: Restore Successfull");
            status = true;
        } else {
            System.out.println("DatabaseManager.restore: Restore Failure!");
        }
    } catch (IOException ioe) {
        System.out.println("Exception IO");
        ioe.printStackTrace();
    } catch (Exception e) {
        System.out.println("Exception");
        e.printStackTrace();
    }
    return status;
}

//for testing
public static void main(String args[]){
    String backupName = "D:/DatabaseBackup/backupHvs.sql";
    DatabaseManager.restore(backupName);
}

}

Andre referencer 1


mysql -u root -p 123 -h hostname club < dumpfile.sql skal udføres i kommandolinjen i din SO og IKKE i mysql-konsollen.


EDIT: I Java kan du ringe en SO-proces for at gøre det eller bruge noget bibliotek til at foretage backup/gendannelse. Prøv dbunit at gøre disse handlinger til en database.
Jeg brugte dbunit med dvale og fungerer fint.

Andre referencer 2


Fra shell prompt i Linux, skaber følgende dumpet:


mysqldump --add-drop-table -u<username> -p<password> <databasename> > dumpfile.sql


Også fra shell prompt, importerer følgende dumpfilen til en eksisterende database:


mysql -u<username> -p<password> <databasename> < dumpfile.sql

Andre referencer 3


    try {
        Runtime.getRuntime().exec("/C:\Program Files\MySQL\MySQL Server 5.0\bin\mysqldump -uroot -p123456 computer -r D:\zerso\backup.sql");

        JOptionPane.showMessageDialog(null, "  make   back  up");
    } catch (IOException ex) {
        ex.printStackTrace();
        JOptionPane.showMessageDialog(null, "  not   back  up");
    } catch (Exception e) {
        System.out.println("Exception");
        e.printStackTrace();