Java calls Mysql dump to back up the database
SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMddHHmmss ");
Try {
String name = sdf. format (new Date ());
String filePath = System. getProperty ("user. dir") + "//" + name + ". SQL ";
// System actuator
Runtime rt = Runtime. getRuntime ();
// Export Database statements
StringBuffer cmd = new StringBuffer ();
Cmd. append ("mysqldump-u ");
Cmd. append (ServeConfig. dbUser );
Cmd. append ("-p ");
Cmd. append (ServeConfig. dbPass );
Cmd. append ("-- set-charset = utf8 ");
Cmd. append (ServeConfig. dbName );
// Execute export to obtain the input stream
Process child = rt.exe c (cmd. toString ());
InputStream in = child. getInputStream ();
InputStreamReader ir = new InputStreamReader (in, "utf8 ");
// Output file
FileOutputStream fo = new FileOutputStream (filePath );
OutputStreamWriter OS = new OutputStreamWriter (fo, "utf8 ");
// Start reading data
Char [] temp = new char [1024000];
Int len = 0;
While (len = ir. read (temp)> 0 ){
OS. write (temp, 0, len );
OS. flush ();
}
// Do not forget to close the input/output stream
In. close ();
Ir. close ();
OS. close ();
Fo. close ();
// Send the file to the backup server
FileUpLoad upload = FileUpLoad. createFileUpLoad (ServeConfig. backAddr, new File (filePath ));
Upload. tryStart ();
Upload. waitFinish ();
Upload. doClose ();
} Catch (Exception e ){
E. printStackTrace ();
}