Guest User

Untitled

a guest
Sep 1st, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. how can i backup and restore date from mysql data base
  2. mysql> mysql -u root -p 123 -h hostname club <dumpfile.sql;
  3.  
  4. 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
  5.  
  6. public static boolean backup(String mysqldumpPath, String backupPath) {
  7. boolean status = false;
  8. String username = "name";
  9. String password = "pword";
  10. String database = "database_name";
  11.  
  12.  
  13. String command = "/" + mysqldumpPath + "/mysqldump -u " + username + " -p" + password + " " + database + " -r " + backupPath;
  14. try {
  15. Process runtimeProcess = Runtime.getRuntime().exec(command);
  16. int processComplete = runtimeProcess.waitFor();
  17. if (processComplete == 0) {
  18. System.out.println("DatabaseManager.backup: Backup Successfull");
  19. status = true;
  20. } else {
  21. System.out.println("DatabaseManager.backup: Backup Failure!");
  22. }
  23. } catch (IOException ioe) {
  24. System.out.println("Exception IO");
  25. ioe.printStackTrace();
  26. } catch (Exception e) {
  27. System.out.println("Exception");
  28. e.printStackTrace();
  29. }
  30. return status;
  31. }
  32. public static boolean restore(String filePath){
  33. boolean status = false;
  34. String username = "name";
  35. String password = "pword";
  36. String[] command = new String[]{"mysql", "database_name", "-u" + username, "-p" + password, "-e", " source "+filePath };
  37.  
  38. try {
  39. Process runtimeProcess = Runtime.getRuntime().exec(command);
  40. int processComplete = runtimeProcess.waitFor();
  41. if (processComplete == 0) {
  42. System.out.println("DatabaseManager.restore: Restore Successfull");
  43. status = true;
  44. } else {
  45. System.out.println("DatabaseManager.restore: Restore Failure!");
  46. }
  47. } catch (IOException ioe) {
  48. System.out.println("Exception IO");
  49. ioe.printStackTrace();
  50. } catch (Exception e) {
  51. System.out.println("Exception");
  52. e.printStackTrace();
  53. }
  54. return status;
  55. }
  56.  
  57. //for testing
  58. public static void main(String args[]){
  59. String backupName = "D:/DatabaseBackup/backupHvs.sql";
  60. DatabaseManager.restore(backupName);
  61. }
  62.  
  63. mysqldump --add-drop-table -u<username> -p<password> <databasename> > dumpfile.sql
  64.  
  65. mysql -u<username> -p<password> <databasename> < dumpfile.sql
Add Comment
Please, Sign In to add comment