首页 > 代码库 > java操作数据库定时备份与还原

java操作数据库定时备份与还原

mysql每天23:00自动备份

  1. public class DatabaseBackup {  
  2.     /** 
  3.      *  
  4.      * @param dbdir mysql数据库安装路径 
  5.      * @param dbname  数据库的名称 
  6.      * @param backdir 备份的目录 
  7.      */  
  8.     public static void backup(String dbdir, String dbname, String backdir) {  
  9.         Calendar calendar = Calendar.getInstance();  
  10.         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_HHmmss");  
  11.         String currentTime = dateFormat.format(calendar.getTime());  
  12.         try {  
  13.             long startTime = System.currentTimeMillis();  
  14.             Runtime rt = Runtime.getRuntime();  
  15.             Process child = rt  
  16.                     .exec(dbdir + "/bin/mysqldump --default-character-set=utf8 -uroot -p123456 " + dbname);  
  17.             InputStream in = child.getInputStream();  
  18.             InputStreamReader xx = new InputStreamReader(in, "utf8");  
  19.   
  20.             FileOutputStream fout = new FileOutputStream(new File(backdir, dbname + "_" + currentTime + ".bak"));  
  21.             OutputStreamWriter writer = new OutputStreamWriter(fout, "utf8");  
  22.   
  23.             dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  24.   
  25.             writer.write("-- Dump by Microsoul at " + dateFormat.format(calendar.getTime()) + "\r\n");  
  26.   
  27.             String inStr;  
  28.             BufferedReader br = new BufferedReader(xx);  
  29.             // 这样实时写入文件很重要,网上有很多是将读取的存入字符串中,最后再写成文件,这样会导致Java的堆栈内存溢出。  
  30.             while ((inStr = br.readLine()) != null) {  
  31.                 writer.write(inStr);  
  32.                 writer.write("\r\n");  
  33.             }  
  34.   
  35.             writer.write("\r\n-- Use " + (System.currentTimeMillis() - startTime) + "ms\r\n");  
  36.   
  37.             writer.flush();  
  38.             in.close();  
  39.             xx.close();  
  40.             br.close();  
  41.             writer.close();  
  42.             fout.close();  
  43.         } catch (Exception e) {  
  44.             PrintStream print = null;  
  45.             try {  
  46.                 print = new PrintStream(new File(backdir, currentTime + "_backup_err.log"));  
  47.                 dateFormat.applyPattern("yyyy-MM-dd HH:mm:ss");  
  48.                 currentTime = dateFormat.format(calendar.getTime());  
  49.                 print.println(currentTime + "  backup failed.");  
  50.                 e.printStackTrace(print);  
  51.                 print.flush();  
  52.             } catch (IOException e2) {  
  53.   
  54.             } finally {  
  55.                 if (print != null) {  
  56.                     print.close();  
  57.                 }  
  58.             }  
  59.         }  
  60.   
  61.     }  
  62. }  

 以上是备份的代码;

Java代码  技术分享
  1. public class Test {  
  2.     public static void main(String[] args) {  
  3.         Calendar twentyOne = Calendar.getInstance();  
  4.         twentyOne.set(Calendar.HOUR_OF_DAY, 23);  
  5.         twentyOne.set(Calendar.MINUTE, 0);  
  6.         twentyOne.set(Calendar.SECOND, 0);  
  7.   
  8.         new Timer().schedule(new TimerTask() {  
  9.             @Override  
  10.             public void run() {  
  11.                 DatabaseBackup.backup("/usr/local/mysql", "test", "/home/xtiger/db/");  
  12.             }  
  13.         }, twentyOne.getTime(), 24 * 3600 * 1000);  
  14.     }  
  15. }  

java操作数据库定时备份与还原