2011-04-26 85 views
2

我想知道如何从mysql数据库创建备份并恢复它。 我想在我的Java应用程序中使用它。我怎么能从mysql数据库备份和恢复日期

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

但它有这个错误:

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 
+0

您需要从命令行执行此操作,而不是从mySQL内执行 – 2011-04-26 16:32:52

回答

2

mysql -u root -p 123 -h hostname club < dumpfile.sql需要在命令行中的SO,而不是在mysql的控制台执行。

编辑:在Java中,您可以调用SO过程来做到这一点,或使用一些库进行备份/恢复。尝试使用dbunit为数据库执行此操作。 我用hunter和dbunit工作正常。

+0

是dbunit的一个程序吗? – 2011-04-26 16:47:39

+0

我下载了dbunit 1.5.5.jar,现在该如何使用它 – 2011-04-26 16:49:44

1

从Linux中的shell提示符,下面创建转储:

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

而且从shell提示符,以下进口转储文件到现有数据库:

mysql -u<username> -p<password> <databasename> < dumpfile.sql 
3
import java.io.IOException; 
/** 
* 
* @author [email protected] 
*/ 

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

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

这看起来像Hanba已经提出的。虽然他的例子更加强大,因为它等待完成过程。 – Leigh 2012-06-28 16:43:17