Linux java mysql 定时备份和手动备份 quartz定时器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux java mysql 定时备份和手动备份 quartz定时器相关的知识,希望对你有一定的参考价值。
在第一篇中实现了,手动的,这篇实现自动用到了quartz
maven在这里 spring用的4.2.8 就不粘出来了
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz-jobs</artifactId> <version>2.2.1</version> </dependency>
配置文件如下
1 <bean name="exampleJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> 2 <property name="jobClass" value="cn.goldencis.tsa.common.job.BackupJob" /> 3 <property name="jobDataAsMap"> 4 <map> 5 <entry key="timeout" value="5" /> 6 </map> 7 </property> 8 </bean> 9 10 <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> 11 <property name="jobDetail" ref="exampleJob" /> 12 <property name="cronExpression" value="0 30 0 L * ?" /> <!-- 每月最后一天凌晨30分触发 --> 13 </bean> 14 15 <bean id="quartzfactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 16 <property name="triggers"> 17 <list> 18 <ref bean="cronTrigger" /> 19 </list> 20 </property> 21 </bean> 22 23 <bean id="quartzBean" class="cn.goldencis.tsa.system.entity.QuartzBean" > 24 <property name="cronTriggerStr" value="cronTrigger"/> 25 <property name="exampleJobStr" value="exampleJob"/> 26 </bean>
job类
1 package cn.goldencis.tsa.common.job; 2 3 import org.quartz.JobExecutionContext; 4 import org.quartz.JobExecutionException; 5 import org.springframework.scheduling.quartz.QuartzJobBean; 6 7 import cn.goldencis.tsa.common.utils.BackupUtil; 8 9 /** 10 * 执行数据库备份作业 11 * @author mll 12 * 2017年4月15日下午3:55:48 13 */ 14 public class BackupJob extends QuartzJobBean { 15 16 @Override 17 protected void executeInternal(JobExecutionContext context) 18 throws JobExecutionException { 19 BackupUtil.getInstance().backup(null); 20 } 21 }
quartz控制器
1 package cn.goldencis.tsa.common.utils; 2 3 import java.text.ParseException; 4 5 import org.apache.log4j.Logger; 6 import org.quartz.CronTrigger; 7 import org.quartz.JobDetail; 8 import org.quartz.JobKey; 9 import org.quartz.SchedulerException; 10 import org.quartz.TriggerKey; 11 import org.quartz.impl.StdScheduler; 12 import org.quartz.impl.triggers.CronTriggerImpl; 13 14 /** 15 * 定时器控制器,版本spring4以上,quartz 2.2 16 * 版本不一致,导致问题挺多的,避免不必要麻烦,不要更改版本 17 * 在此只有更新表达式方法 18 * @author mll 19 * 2017年4月15日下午4:04:42 20 * 21 */ 22 public class QuartzHandler { 23 24 final static String DEFAULT_CRON_EXPRESSION="0 30 0 L * ?"; //每月最后一天执行 25 26 private static Logger logger = Logger.getLogger(QuartzHandler.class); 27 28 private static QuartzHandler instance = new QuartzHandler(); 29 30 private QuartzHandler() { 31 32 } 33 34 public static QuartzHandler getInstance() { 35 return instance; 36 } 37 38 39 /** 40 * update expression 41 * @author mll 42 * @param cronTrigger 43 * @param sts 44 * @param cronTriggerName 45 * @param jobDetailName 46 * @param crom void 47 */ 48 public void updateCron(CronTrigger cronTrigger,StdScheduler sts,String cronTriggerName,String jobDetailName,String cron){ 49 50 String strcron = cron; 51 try { 52 logger.info("-----------------------QuartzHandler : start ----------------------"); 53 if(StringUtil.isEmpty(strcron)){ 54 strcron = DEFAULT_CRON_EXPRESSION; 55 } 56 57 CronTriggerImpl ct = (CronTriggerImpl) sts.getTrigger(new TriggerKey(cronTriggerName, StdScheduler.DEFAULT_GROUP)); 58 59 JobDetail jd = sts.getJobDetail(new JobKey(jobDetailName, StdScheduler.DEFAULT_GROUP)); 60 61 sts.unscheduleJob(new TriggerKey(cronTriggerName, StdScheduler.DEFAULT_GROUP)); 62 63 sts.deleteJob(new JobKey(jobDetailName, StdScheduler.DEFAULT_GROUP)); 64 65 ct.setCronExpression(strcron); 66 67 sts.scheduleJob(jd,ct); 68 69 logger.info("QuartzHandler : Update CronExpression is " + cron); 70 } catch (SchedulerException e) { 71 e.printStackTrace(); 72 } catch (ParseException e) { 73 e.printStackTrace(); 74 } 75 } 76 77 }
监听器 里面的CronExpressionUtil这个是拼接cron表达式的,自己写吧,不贴出来了
1 package cn.goldencis.tsa.system.utils; 2 3 import javax.annotation.Resource; 4 5 import org.quartz.CronTrigger; 6 import org.quartz.impl.StdScheduler; 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.context.ApplicationListener; 9 import org.springframework.context.event.ContextRefreshedEvent; 10 import org.springframework.stereotype.Component; 11 import org.springframework.web.context.ContextLoader; 12 13 import cn.goldencis.tsa.common.utils.CronExpressionUtil; 14 import cn.goldencis.tsa.common.utils.QuartzHandler; 15 import cn.goldencis.tsa.system.dao.AutoBackupMapper; 16 import cn.goldencis.tsa.system.entity.DBBackupMsg; 17 import cn.goldencis.tsa.system.entity.QuartzBean; 18 19 /** 20 * 监听器,负责spring启动后查询数据库,根据数据库信息设定corn 21 * @author mll 22 * 2017年4月15日20:35:20 23 */ 24 25 @Component 26 public class DBBackupListener implements ApplicationListener<ContextRefreshedEvent> { 27 28 final String IS_AUTO_FLAG = "1"; 29 30 private static DBBackupListener dBBackupListener=new DBBackupListener(); 31 32 @Autowired 33 private AutoBackupMapper autoBackupMapper; 34 35 @Resource(name="cronTrigger") 36 private CronTrigger cronTrigger; 37 38 @Resource(name = "quartzfactory") 39 private StdScheduler sts; 40 41 @Resource(name = "quartzBean") 42 private QuartzBean quartzBean; 43 44 private DBBackupListener() { 45 // TODO Auto-generated constructor stub 46 } 47 48 public static DBBackupListener getInstance(){ 49 return dBBackupListener; 50 } 51 52 @Override 53 public void onApplicationEvent(ContextRefreshedEvent event) { 54 if(event.getApplicationContext().getParent()==null){ 55 this.updateCornEx(null); 56 } 57 } 58 59 60 public void updateCornEx(DBBackupMsg db) { 61 DBBackupMsg mdb=db; 62 if(mdb==null){ 63 mdb = autoBackupMapper.selectAutoBackInfo(); 64 }else{ 65 cronTrigger = (CronTrigger)ContextLoader.getCurrentWebApplicationContext().getBean("cronTrigger"); 66 sts = (StdScheduler)ContextLoader.getCurrentWebApplicationContext().getBean("quartzfactory"); 67 quartzBean = (QuartzBean)ContextLoader.getCurrentWebApplicationContext().getBean("quartzBean"); 68 } 69 70 String cronEx = null; 71 if(IS_AUTO_FLAG.equals(mdb.getDay()) && IS_AUTO_FLAG.equals(mdb.getWeek())){ 72 cronEx = CronExpressionUtil.getCronExByDate(mdb.getDayParam(), mdb.getWeekParam()); 73 }else if(IS_AUTO_FLAG.equals(mdb.getWeek())){ 74 cronEx = CronExpressionUtil.getCronExByWeek( mdb.getWeekParam()); 75 }else if(IS_AUTO_FLAG.equals(mdb.getDay())){ 76 cronEx = CronExpressionUtil.getCronExByDay( mdb.getDayParam()); 77 } 78 //cronEx="*/5 * * * * ?"; 79 QuartzHandler.getInstance().updateCron(cronTrigger, sts, quartzBean.getCronTriggerStr(), quartzBean.getExampleJobStr(), cronEx); 80 } 81 }
这个是删除计划,每隔一段时间删除备份的文件
1 package cn.goldencis.tsa.system.task; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.scheduling.annotation.Scheduled; 8 import org.springframework.stereotype.Component; 9 import org.springframework.transaction.annotation.Transactional; 10 11 import cn.goldencis.tsa.common.utils.BackupUtil; 12 import cn.goldencis.tsa.system.dao.DBBackupMsgMapper; 13 import cn.goldencis.tsa.system.entity.DBBackupMsg; 14 15 /** 16 * delete backup task 17 * @author mll 18 */ 19 @Component 20 public class AutoDeleteBackupFileTask { 21 22 @Autowired 23 private DBBackupMsgMapper dBBackupMsgMapper; 24 25 /** 26 * everyday 0:30 27 */ 28 @Scheduled(cron = "0 30 0 * * ?") 29 @Transactional 30 public void autoDeleteBackup() { 31 List<DBBackupMsg> list=dBBackupMsgMapper.selectAutoDeleteInfo(); //select there are 90 days for file info 32 if(list != null && list.size()>0){ 33 StringBuilder sb=new StringBuilder(); 34 List<String> listS=new ArrayList<String>(); 35 for(DBBackupMsg temp : list){ 36 sb.append(temp.getBackupName()+ ","); 37 listS.add(temp.getBackupName()); 38 } 39 dBBackupMsgMapper.deleteByBackupName(listS); 40 BackupUtil.getInstance().deleteBackup(sb.toString()); 41 } 42 } 43 }
以上也就差不多了,不多解释了 还原还写了shell脚本,不过最后说让他们导出自己还原就没用,也没测试这个shell脚本(没测试没测试没测试) ,也粘出来吧,水平有限,将就看
1 #!bin/bash 2 3 # 4 PORTAL_DIR=$DEPLOY_DIR/tomcat 5 IPADR=$1 6 USERNAME=$2 7 PASSWORD=$3 8 DATABASE=$4 9 FILEPATH=$5 10 BACKUPNAME=$6 11 mysql="mysql -h "$IPADR" -u "$USERNAME" -p"$PASSWORD 12 RESTORESQL=$MYSQL" "$DATABASE" <"$FILEPATH"/"$BACKUPNAME 13 DROPDATABASE="drop database "$DATABASE 14 15 # 16 #cd $PORTAL_DIR/bin 17 #bash ./shutdown.sh 18 echo "closed server" 19 20 # 21 #$($MYSQL -e "$DROPDATABASE") 22 echo "$MYSQL -e ‘$DROPDATABASE‘" 23 24 # 25 #$($RESTORESQL) 26 echo "$RESTORESQL" 27 28 # 29 #$(reboot) 30 echo "reboot"
以上是关于Linux java mysql 定时备份和手动备份 quartz定时器的主要内容,如果未能解决你的问题,请参考以下文章
MySQL数据库全备和增备增量数据恢复案例以及定时清理 binlog 日志
linux中打包备份脚本,手动执行可以正常打包,使用crontab -e 添加到系统定时任务中执行就无法打包