开发者

how can i backup and restore date from mysql data base

i want to know how can i create backup from mysql database and restore it. i want to use it in my java application.

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

but it has this error:

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 nea开发者_StackOverflow中文版r 'mysql -u root -p mehdi  -h hostname club < dumpfile.sql' at line 1   


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);
}

}


mysql -u root -p 123 -h hostname club < dumpfile.sql needs to be executed in command line in your SO and NOT in mysql console.

EDIT: In Java you can call a SO process to do that or use some library to make backup/restore. Try dbunit to make this actions for a database. I used dbunit with hibernate and works fine.


From the shell prompt in Linux, the following creates the dump:

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

Also from the shell prompt, the following imports the dumpfile into an existing database:

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


    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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜