Mysql automatic backup, using MD5 for unique backup

Source: Internet
Author: User
Tags mysql automatic backup

Mysql automatic backup, using MD5 for unique backup
1. Name the backup database temp. SQL and calculate its MD5 value (for MD5 calculation method, see MD5.java)

<Span style = "white-space: pre"> </span> String dos = "cmd/c mysqldump-uroot-pwugroup weixin> D:/mysql_back/temp. SQL "; // doscommand runtime.getruntime(cmd.exe c (dos); // execute the doscommand Thread. sleep (1000); // sleep for one minute to prevent the following code from executing too fast File fileTemp = new File ("D:/mysql_back/temp. SQL "); String fileTempString = FileString. getFileString (fileTemp); String fileTempMd5 = MD5.GetMD5Code (fileTempString );
2. traverse the Backup Directory and calculate the MD5 value of the files under the directory, respectively compared with temp. SQL
<Span style = "white-space: pre"> </span> File dir = new File ("D:/mysql_back"); File [] fs = dir. listFiles (); for (int I = 1; I <fs. length; I ++) {// I starts from 1 to skip temp. SQL file String fileString = FileString. getFileString (fs [I]); String fileMd5 = MD5.GetMD5Code (fileString); // System. out. println (fileTempMd5); // System. out. println (fileMd5); if (fileMd5.equals (fileTempMd5) {// exit the System if the file is the same as the file. exit (0 );}}
4. Exit the program if there is the same
<Span style = "white-space: pre"> </span> if (fileMd5.equals (fileTempMd5) {// exit System if the file has the same name as the file. exit (0 );}
5. If all files in the directory are different, rename temp to the current time to save
<Span style = "white-space: pre"> </span> String name = new SimpleDateFormat ("yyyyMMddHHmmss "). format (new Date ()). toString (); // get the current time and Format File rename = new File (fileTemp. getParent () + File. separator <span style = "font-family: Arial, Helvetica, sans-serif;"> + "weixin _" + name + ". SQL "); </span> // if this file is not available, rename the created temp File // System. out. println (rename); fileTemp. renameTo (rename); System. exit (0 );
Code:
Package backup; import java. io. file; import java. io. IOException; import java. text. simpleDateFormat; import java. util. date; /***** @ author wuxingye **/public class Main {/***** main function ** @ param args * @ throws IOException * @ throws InterruptedException */public static void main (String [] args) throws IOException, InterruptedException {String dos = "cmd/c mysqldump-uroot-pwugroup weixin> D:/mysql_back/temp. SQL "; // doscommand runtime.getruntime(cmd.exe c (dos); // execute the doscommand Thread. sleep (1000); // sleep for one minute to prevent the following code from executing too fast File fileTemp = new File ("D:/mysql_back/temp. SQL "); String fileTempString = FileString. getFileString (fileTemp); String fileTempMd5 = MD5.GetMD5Code (fileTempString); // TODO Auto-generated method stubFile dir = new File ("D:/mysql_back "); file [] fs = dir. listFiles (); for (int I = 1; I <fs. length; I ++) {// I starts from 1 to skip temp. SQL file String fileString = FileString. getFileString (fs [I]); String fileMd5 = MD5.GetMD5Code (fileString); // System. out. println (fileTempMd5); // System. out. println (fileMd5); if (fileMd5.equals (fileTempMd5) {// exit the System if the file is the same as the file. exit (0) ;}} String name = new SimpleDateFormat ("yyyyMMddHHmmss "). format (new Date ()). toString (); // get the current time and Format File rename = new File (fileTemp. getParent () + File. separator // if this file is not available, rename the created temp File + "weixin _" + name + ". SQL "); // System. out. println (rename); fileTemp. renameTo (rename); System. exit (0 );}}
FileString. java

Package backup; import java. io. bufferedReader; import java. io. file; import java. io. fileReader; import java. io. IOException;/*** convert File to String, convert the file into a string ** @ author wuxingye */public class FileString {/***** @ param file * @ return returns the file content, returns */public static String getFileString (File file) {BufferedReader br = null; StringBuffer sb = null; try {br = new BufferedReader (new FileReader (File )); sb = new St RingBuffer (); String str = null; // read a row at a time until null is read as the end of the file while (str = br. readLine ())! = Null) {if (! Str. contains ("Dump completed on") {// the last row of the exported data file is -- Dump completed on 20:03:21 // the end time of the last row of the exported database, in this way, the MD5 of files everywhere is different, so this line of sb is removed. append (str); // StringBuffer is faster than String} br. close ();} catch (IOException e) {e. printStackTrace ();} finally {if (br! = Null) {try {br. close () ;}catch (IOException e1) {}} return sb. toString ();}}

MD5.java
Package backup; import java. security. messageDigest; import java. security. noSuchAlgorithmException; /***** obtain the MD5 value of the String ** @ author from the network **/public class MD5 {// Global Array private final static String [] strDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "", "B", "c", "d", "e", "f"}; public MD5 () {} // return format: numeric and String/***** @ param bByte * @ return byte to convert to String, hexadecimal String */private static String byteToArrayString (byte bByte) {int iRet = bByte; // System. out. println ("iRet =" + iRet); if (iRet <0) {iRet + = 256;} int iD1 = iRet/16; int iD2 = iRet % 16; return strDigits [iD1] + strDigits [iD2];} // The return format is only numbers. // private static String byteToNum (byte bByte) {// int iRet = bByte; // System. out. println ("iRet1 =" + iRet); // if (iRet <0) {// iRet + = 256; //} // return String. valueOf (iRet); // convert the byte array to a hexadecimal string/*** call byteToArrayString ** @ param bByte * @ return to convert the byte to a string, hexadecimal String */private static String byteToString (byte [] bByte) {StringBuffer sBuffer = new StringBuffer (); for (int I = 0; I <bByte. length; I ++) {sBuffer. append (byteToArrayString (bByte [I]);} return sBuffer. toString ();}/***** @ param strObj * @ return obtain the MD5 value of the String */public static String GetMD5Code (String strObj) {// System. out. println (strObj); String resultString = null; try {resultString = new String (strObj); MessageDigest md = MessageDigest. getInstance ("MD5"); // md. digest () the return value of this function is the byte array resultString = byteToString (md. digest (strObj. getBytes ();} catch (NoSuchAlgorithmException ex) {ex. printStackTrace () ;}return resultString ;}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.