Mysql backup and Restoration
The backup java code is as follows:
1/** 2 * back up a single database 3 * @ param dbName database name 4 * @ return success or failure 5 */6 @ Override 7 public boolean backup (String dbName) {8 try {9 logger.info (dbName + "Start backup! "); 10 String cmd =" \ "" + dataBaseBS. getInstallPath () + "bin \ mysqldump \"-h localhost-uroot-p123456 "+ dbName11 +"> \ "" + uploadPath + dbName + ". SQL \ ""; 12 Process process = runtime.getruntime(cmd.exe c ("cmd/c start/B" + cmd); 13 InputStreamReader isr = new InputStreamReader (process. getErrorStream (); 14 LineNumberReader input = new LineNumberReader (isr); 15 String line; 16 while (line = input . ReadLine ())! = Null) {17} 18 logger.info (dbName + "backup successful! "); 19 return true; 20} catch (Exception e) {21 logger. error (dbName +" backup failed! ", E); 22 return false; 23} 24}View Code
The code for restoring java is as follows:
1/** 2 * restore a single database 3 * @ param dbName database name 4 * @ return restore successful or failed 5 */6 @ Override 7 public boolean restore (String dbName) {8 try {9 logger.info (dbName + "start restoration! "); 10 String cmd =" \ "" + dataBaseBS. getInstallPath () + "bin \ mysql \"-h localhost-uroot-p123456 "+ dbName11 +" <\ "" + uploadPath + dbName + ". SQL \ ""; 12 Process process = runtime.getruntime(cmd.exe c ("cmd/c start/B" + cmd); 13 InputStreamReader isr = new InputStreamReader (process. getInputStream (); 14 LineNumberReader input = new LineNumberReader (isr); 15 String line; 16 while (line = input. rea DLine ())! = Null) {17} 18 logger.info (dbName + "restored successfully! "); 19 return true; 20} catch (Exception e) {21 logger. error (dbName +" Restore failed! ", E); 22 return false; 23} 24}View Code
Cmd command for restoring mysql:
String cmd = "\"\" \""+dataBaseBS.getInstallPath() +"bin\\mysqldump\" -h localhost -uroot -p123456 " + dbName + " > \"" + uploadPath + dbName + ".sql\""
The installation path of mysqldump and the SQL export path support spaces.
Cmd call command:
Process process = Runtime.getRuntime().exec("cmd /c start /b " + cmd);
The cmd command can be executed in the background without a command window or flashing.
Mysql backup batch file, mysql backup. bat
1 @ echo off 2 echo. 3 echo MySQL database backup 4 5 echo ***************************** 6 echo. 7 echo today is % date % 8 echo time is % time % 9 echo.10 echo *********************** ****** 11 12:: set the Ymd variable to the date format: yyyyMMdd13 set "Ymd = % date :~, 4% % date :~ 5, 2% % date :~ 8, 2% "14 15: Create a folder 16 md" D: \ JDBC \ % Ymd % "17 18: Backup command, multiple databases are separated by spaces 19 "mysqldump" -- single-transaction -- databases dbName-uroot-p123456 -- default-character-set = gbk> "D: \ JDBC \ % Ymd % \ backup. SQL "20 21 echo.22 23 echo MySQL database backup is complete, please check... 24 25 echo.26 echo.27 28: prompt to press any key to continue 29 pauseView Code
Generate the backup. SQL file under D: \ JDBC \ 20160612 \. Running effect: