当前位置:Gxlcms > 数据库问题 > java操作数据库定时备份与还原

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

时间:2021-07-01 10:21:17 帮助过:15人阅读

  •     /** 
  •      *  
  •      * @param dbdir mysql数据库安装路径 
  •      * @param dbname  数据库的名称 
  •      * @param backdir 备份的目录 
  •      */  
  •     public static void backup(String dbdir, String dbname, String backdir) {  
  •         Calendar calendar = Calendar.getInstance();  
  •         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_HHmmss");  
  •         String currentTime = dateFormat.format(calendar.getTime());  
  •         try {  
  •             long startTime = System.currentTimeMillis();  
  •             Runtime rt = Runtime.getRuntime();  
  •             Process child = rt  
  •                     .exec(dbdir + "/bin/mysqldump --default-character-set=utf8 -uroot -p123456 " + dbname);  
  •             InputStream in = child.getInputStream();  
  •             InputStreamReader xx = new InputStreamReader(in, "utf8");  
  •   
  •             FileOutputStream fout = new FileOutputStream(new File(backdir, dbname + "_" + currentTime + ".bak"));  
  •             OutputStreamWriter writer = new OutputStreamWriter(fout, "utf8");  
  •   
  •             dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  •   
  •             writer.write("-- Dump by Microsoul at " + dateFormat.format(calendar.getTime()) + "\r\n");  
  •   
  •             String inStr;  
  •             BufferedReader br = new BufferedReader(xx);  
  •             // 这样实时写入文件很重要,网上有很多是将读取的存入字符串中,最后再写成文件,这样会导致Java的堆栈内存溢出。  
  •             while ((inStr = br.readLine()) != null) {  
  •                 writer.write(inStr);  
  •                 writer.write("\r\n");  
  •             }  
  •   
  •             writer.write("\r\n-- Use " + (System.currentTimeMillis() - startTime) + "ms\r\n");  
  •   
  •             writer.flush();  
  •             in.close();  
  •             xx.close();  
  •             br.close();  
  •             writer.close();  
  •             fout.close();  
  •         } catch (Exception e) {  
  •             PrintStream print = null;  
  •             try {  
  •                 print = new PrintStream(new File(backdir, currentTime + "_backup_err.log"));  
  •                 dateFormat.applyPattern("yyyy-MM-dd HH:mm:ss");  
  •                 currentTime = dateFormat.format(calendar.getTime());  
  •                 print.println(currentTime + "  backup failed.");  
  •                 e.printStackTrace(print);  
  •                 print.flush();  
  •             } catch (IOException e2) {  
  •   
  •             } finally {  
  •                 if (print != null) {  
  •                     print.close();  
  •                 }  
  •             }  
  •         }  
  •   
  •     }  
  • }  
  •  以上是备份的代码;

    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操作数据库定时备份与还原

    标签:data   http   nbsp   oid   bar   读取   ota   base   min   

    人气教程排行