Java code implemented using Mysql perform backup and restore regular window environment
// backup public void DOBACKUP () { Date currentTime = new Date(); System.out.println ( "current time is:" + new new a Date ()); Runtime runtime = Runtime.getRuntime(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss"); SdfDate String = sdf.format (currentTime); // backup file to the target String filePath = "E: \\ \\ time_ the Temporary" + sdfDate + ".sql" ; // command to be executed (% dwz% is the environment variable value) String CmdStr = "% DWZ the mysqldump --defaults-% Extra-File .. \\% =% DWZ the my.ini charm_village>" + filePath; the try { String[] cmd = {"CMD", "/C", cmdStr}; String [] path = { "DWZ = E: \\ \\ DWZ-Software MySQL 5.7.27-bin-Winx64 \\ \\" }; // external execution Process Process = Runtime.exec (cmd, path); ErrorStream InputStream = Process.getErrorStream (); // get the error message when the command is executed System.err.println (IOUtils.toString (errorStream, "UTF-8" ;)) // After blocking the current thread, and wait for external program termination code acquisition result int exitVal = process.waitFor (); IF (exitVal == 0 ) { System.out.println ( "Database Backup success!" ); } } catch (Exception e) { e.printStackTrace (); } }
About return result Type: Process, it has several methods:
1.destroy (): kill the child process
2.exitValue (): Returns the child process exit value, a value of 0 indicates normal termination
3.getErrorStream (): Gets the child process the error stream
4.getInputStream (): Gets the child process input stream
5.getOutputStream (): get the child process output stream
6.waitFor (): causes the current thread to wait, if necessary, until the process represented by this Process object has terminated. Assuming that the child process has been terminated. This method returns immediately. Assuming no child process terminated, the calling thread will be blocked. Until the child process exits, according to the convention, 0 indicates normal termination
Reference article:
Mysql: Java code for database backup and restore regular Explanation