如何用Java实现MySQL数据库的备份和恢复

来源:互联网 发布:js实现购物车删除功能 编辑:程序博客网 时间:2024/05/17 09:03

MySQL的一些前台工具是有备份恢复功能的,可是如何在我们的应用程序中实现这一功能呢?本文提供了示例代码来说明如何使用Java代码实现MySQL数据库的备份恢复。

 

本次实现是使用了MySQL数据库本身提供的备份命令mysqldump和恢复命令mysql,在java代码中通过从命令行调用这两条命令来实现备份和恢复。备份和恢复所使用的文件都是sql文件。

 

本代码是参照网上某网友提供的源码完成的。

[java] view plaincopy
  1. package xxx.utils;  
  2. import java.io.BufferedReader;  
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import java.io.InputStream;  
  9. import java.io.InputStreamReader;  
  10. import java.io.OutputStream;  
  11. import java.io.OutputStreamWriter;  
  12. import java.io.PrintWriter;  
  13. import java.io.UnsupportedEncodingException;  
  14. /** 
  15.  * MySQL数据库的备份与恢复 缺陷:可能会被杀毒软件拦截 
  16.  *  
  17.  * @author xxx 
  18.  * @version xxx 
  19.  */  
  20. public class DatabaseBackup {  
  21.     /** MySQL安装目录的Bin目录的绝对路径 */  
  22.     private String mysqlBinPath;  
  23.     /** 访问MySQL数据库的用户名 */  
  24.     private String username;  
  25.     /** 访问MySQL数据库的密码 */  
  26.     private String password;  
  27.     public String getMysqlBinPath() {  
  28.         return mysqlBinPath;  
  29.     }  
  30.     public void setMysqlBinPath(String mysqlBinPath) {  
  31.         this.mysqlBinPath = mysqlBinPath;  
  32.     }  
  33.     public String getUsername() {  
  34.         return username;  
  35.     }  
  36.     public void setUsername(String username) {  
  37.         this.username = username;  
  38.     }  
  39.     public String getPassword() {  
  40.         return password;  
  41.     }  
  42.     public void setPassword(String password) {  
  43.         this.password = password;  
  44.     }  
  45.     public DatabaseBackup(String mysqlBinPath, String username, String password) {  
  46.         if (!mysqlBinPath.endsWith(File.separator)) {  
  47.             mysqlBinPath = mysqlBinPath + File.separator;  
  48.         }  
  49.         this.mysqlBinPath = mysqlBinPath;  
  50.         this.username = username;  
  51.         this.password = password;  
  52.     }  
  53.     /** 
  54.      * 备份数据库 
  55.      *  
  56.      * @param output 
  57.      *            输出流 
  58.      * @param dbname 
  59.      *            要备份的数据库名 
  60.      */  
  61.     public void backup(OutputStream output, String dbname) {  
  62.         String command = "cmd /c " + mysqlBinPath + "mysqldump -u" + username  
  63.                 + " -p" + password + " --set-charset=utf8 " + dbname;  
  64.         PrintWriter p = null;  
  65.         BufferedReader reader = null;  
  66.         try {  
  67.             p = new PrintWriter(new OutputStreamWriter(output, "utf8"));  
  68.             Process process = Runtime.getRuntime().exec(command);  
  69.             InputStreamReader inputStreamReader = new InputStreamReader(process  
  70.                     .getInputStream(), "utf8");  
  71.             reader = new BufferedReader(inputStreamReader);  
  72.             String line = null;  
  73.             while ((line = reader.readLine()) != null) {  
  74.                 p.println(line);  
  75.             }  
  76.             p.flush();  
  77.         } catch (UnsupportedEncodingException e) {  
  78.             e.printStackTrace();  
  79.         } catch (IOException e) {  
  80.             e.printStackTrace();  
  81.         } finally {  
  82.             try {  
  83.                 if (reader != null) {  
  84.                     reader.close();  
  85.                 }  
  86.                 if (p != null) {  
  87.                     p.close();  
  88.                 }  
  89.             } catch (IOException e) {  
  90.                 e.printStackTrace();  
  91.             }  
  92.         }  
  93.     }  
  94.     /** 
  95.      * 备份数据库,如果指定路径的文件不存在会自动生成 
  96.      *  
  97.      * @param dest 
  98.      *            备份文件的路径 
  99.      * @param dbname 
  100.      *            要备份的数据库 
  101.      */  
  102.     public void backup(String dest, String dbname) {  
  103.         try {  
  104.             OutputStream out = new FileOutputStream(dest);  
  105.             backup(out, dbname);  
  106.         } catch (FileNotFoundException e) {  
  107.             e.printStackTrace();  
  108.         }  
  109.     }  
  110.     /** 
  111.      * 恢复数据库 
  112.      *  
  113.      * @param input 
  114.      *            输入流 
  115.      * @param dbname 
  116.      *            数据库名 
  117.      */  
  118.     public void restore(InputStream input, String dbname) {  
  119.         String command = "cmd /c " + mysqlBinPath + "mysql -u" + username  
  120.                 + " -p" + password + " " + dbname;  
  121.         try {  
  122.             Process process = Runtime.getRuntime().exec(command);  
  123.             OutputStream out = process.getOutputStream();  
  124.             String line = null;  
  125.             String outStr = null;  
  126.             StringBuffer sb = new StringBuffer("");  
  127.             BufferedReader br = new BufferedReader(new InputStreamReader(input,  
  128.                     "utf8"));  
  129.             while ((line = br.readLine()) != null) {  
  130.                 sb.append(line + "/r/n");  
  131.             }  
  132.             outStr = sb.toString();  
  133.             OutputStreamWriter writer = new OutputStreamWriter(out, "utf8");  
  134.             writer.write(outStr);  
  135.             writer.flush();  
  136.             out.close();  
  137.             br.close();  
  138.             writer.close();  
  139.         } catch (UnsupportedEncodingException e) {  
  140.             e.printStackTrace();  
  141.         } catch (IOException e) {  
  142.             e.printStackTrace();  
  143.         }  
  144.     }  
  145.     /** 
  146.      * 恢复数据库 
  147.      *  
  148.      * @param dest 
  149.      *            备份文件的路径 
  150.      * @param dbname 
  151.      *            数据库名 
  152.      */  
  153.     public void restore(String dest, String dbname) {  
  154.         try {  
  155.             InputStream input = new FileInputStream(dest);  
  156.             restore(input, dbname);  
  157.         } catch (FileNotFoundException e) {  
  158.             e.printStackTrace();  
  159.         }  
  160.     }  
  161.     public static void main(String[] args) {  
  162.         Configuration config = HibernateSessionFactory.getConfiguration();  
  163.         String binPath = config.getProperty("mysql.binpath");  
  164.         String userName = config.getProperty("connection.username");  
  165.         String pwd = config.getProperty("connection.password");  
  166.         DatabaseBackup bak = new DatabaseBackup(binPath, userName, pwd);  
  167.         bak.backup("c:/ttt.sql""ttt");  
  168.                 bak.restore("c:/ttt.sql""ttt");  
  169.     }  
  170. }  

 

最后的main方法只是一个简单的使用方法的示例代码。

本人所做的项目是使用了hibernate的,而这里需要提供MySQL的bin路径和用户名、密码,而hibernate.cfg.xml中本身就是需要配置数据库的用户名和密码,所以我把MySQL的bin路径也直接配置到了这个文件里面,也不需要创建专门的配置文件,不需要写读取配置文件的接口了。

如果不明白,可以去看hibernate.cfg.xml的说明,里面是可以配置其他的property的。

0 0