java自动备份数据库

来源:互联网 发布:ip mac绑定 编辑:程序博客网 时间:2024/05/18 20:52

  playframework自带任务调度  http://www.playframework.cn/wiki/Scheduled%20Job

 不设置编码会造成中文乱码


package jobs;



import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;


import play.Play;
import play.jobs.Job;
import play.jobs.On;


/**
 * 自动备份数据库:每天晚上2.00备份一次,日期对应文件名  7天一次全备份,一天一次增量备份
 * backup_err.log记录每天自动备份错误日志
 * @author wxl
 */
@On("0 0 12 * * ?")
public class DatabaseBackup extends Job{
@Override
public void doJob() throws Exception {
   Calendar calendar = Calendar.getInstance();  
       SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_HHmmss");  
       String currentTime = dateFormat.format(calendar.getTime());  
       String command = " mysqldump -h 192.168.1.61 -uroot --default-character-set=gbk -pmysql passionlife ";
       //当前周次
       String week = calendar.get(Calendar.WEEK_OF_YEAR)+"";
       System.out.println("备份开始");
       try {  
           long startTime = System.currentTimeMillis();  
           Runtime rt = Runtime.getRuntime();  
           Process child = rt.exec(command);  
           InputStream in = child.getInputStream();  
           InputStreamReader xx = new InputStreamReader(in, "gbk");  
           FileOutputStream fout = new FileOutputStream(new File(Play.applicationPath+"/sqlData/data/"+week+".sql"));  
           OutputStreamWriter writer = new OutputStreamWriter(fout, "gbk");  
 
           dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
 
           writer.write("-- 备份时间 " + 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--备份用时 " + (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(Play.applicationPath+"/sqlData/log/"+currentTime+"backup_err.log"));  
               dateFormat.applyPattern("yyyy-MM-dd HH:mm:ss");  
               currentTime = dateFormat.format(calendar.getTime());  
               print.println(currentTime + "  备份失败");  
               e.printStackTrace(print);  
               print.flush();  
           } catch (IOException e2) {  
 
           } finally {  
               if (print != null) {  
                   print.close();  
               }  
           }  
       }  
 
   }  
}
0 0
原创粉丝点击