123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- package com.hb.proj.sysbackup.service;
- import java.io.File;
- import java.io.IOException;
- import java.util.Calendar;
- import java.util.Date;
- import org.apache.commons.io.FileUtils;
- import org.apache.commons.lang3.StringUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import com.hb.proj.model.BackupLogPO;
- import com.hb.proj.model.BackupSchedulePO;
- import com.hb.proj.sys.service.DBBackupService;
- import com.hb.xframework.util.ApplicationContextUtils;
- import com.hb.xframework.util.DateUtil;
- /**
- * 增量备份任务(Select ...into oufile 也是一种逻辑备份方式,恢复速度比较快,但是只能备份表中的数据,不能包含表结构)
- * 备份区段:上次备份时间(没有则是1月前)至 当前时间
- * 通过load data的方式,实现回复还原操作(LOAD DATA INFILE 'data.txt' INTO TABLE db2.my_table;)
- * @author cwen
- *
- */
- public class BackupIncreaseTask implements Runnable {
- private static final Logger logger = LoggerFactory.getLogger(BackupIncreaseTask.class);
-
- private BackupSchedulePO sche; //待执行的任务计划
-
- private BackupProperties properties; //备份配置属性
-
- private DBBackupService service;
-
- private String caller;
-
- public BackupIncreaseTask(BackupSchedulePO sche,BackupProperties properties,String caller) {
- this.caller=caller;
- this.sche=sche;
- this.properties=properties;
- this.service=ApplicationContextUtils.getBean("DBBackupService", DBBackupService.class);
- }
-
- @Override
- public void run() {
- logger.info("开始进行增量备份...");
-
- if(StringUtils.isBlank(sche.getBackupObj())) {
- logger.info("未找到备份数据表,取消操作");
- return;
- }
-
- BackupLogPO log=null,prlog=null;
- try {
- String[] tabs=sche.getBackupObj().split(";");
-
- for(String tab : tabs) {
-
- if(!tab.contains(":")) {
- logger.info("数据表{}配置不符合增量备份要求【table:field】,取消操作",tab);
- continue;
- }
-
- log=buildLog(tab);
- log.setDataEndTime(new Date());
- service.saveBackupLog(log);
-
- prlog=service.getLastBackup(log.getTaskName());
-
- service.createBackup(buildSQL(tab,getDataTime(prlog),log.getDataEndTime(),log.getBackupFile()));
-
- log.setStatus(BackupLogPO.STAT_SUCCESS);
- log.setEndTime(new Date());
- service.saveBackupLog(log);
- }
- }
- catch(Exception e) {
- e.printStackTrace();
- logger.error("备份进程出现异常,任务取消:{}",e.getMessage());
- if(log!=null) {
- log.setStatus(BackupLogPO.STAT_FAILED);
- log.setEndTime(new Date());
- log.setNote("任务执行出错");
- service.saveBackupLog(log);
- }
- }
-
- }
-
- private String buildSQL(String tab,Date startTime,Date endTime,String saveFile) {
- String[] tabField=tab.split(":");
- StringBuilder sql=new StringBuilder();
- sql.append("select * from "+tabField[0]);
- sql.append(" where "+tabField[1]+" > '"+DateUtil.format(startTime, "yyyy-MM-dd HH:mm:ss")+"' and "+tabField[1]+" <= '"+DateUtil.format(endTime, "yyyy-MM-dd HH:mm:ss")+"' into outfile '"+saveFile+"'");
- return sql.toString();
- }
-
- private BackupLogPO buildLog(String tab) throws IOException {
- tab=tab.split(":")[0];
- String fileName="["+tab+"]"; //sche.getScheduleName()+"["+tab+"]";
-
- BackupLogPO backupLog=new BackupLogPO(fileName,sche.getScheduleId(),BackupLogPO.STAT_RUNNING);
-
- String path=properties.getSavePath();
-
- //sql语句中的分隔符特殊处理
-
- /*if(!path.endsWith(File.separator)) {
- path+=File.separator;
- }
-
- path+=DateUtil.format(new Date(), "yyyyMM")+File.separator;
- */
-
- path+="\\\\"+DateUtil.format(new Date(), "yyyyMM")+"\\\\";
-
- FileUtils.forceMkdir(new File(path));
-
- backupLog.setBackupFile(path+fileName+DateUtil.format(new Date(), "yyyyMMddHHmm")+".sql");
- backupLog.setOperator(caller);
- return backupLog;
- }
- /**
- * 默认从上次备份时间开始,无上次备份则从前一天开始
- * @param log
- * @return
- */
- private Date getDataTime(BackupLogPO log) {
- if(log==null || log.getDataEndTime()==null) {
- Calendar ca=Calendar.getInstance();
- ca.add(Calendar.DAY_OF_MONTH, -1);
- return ca.getTime();
- }
- else {
- return log.getDataEndTime();
- }
- }
- }
|