|
|
@@ -1,10 +1,12 @@
|
|
|
-package cn.reghao.autodop.dmaster.backup;
|
|
|
+package cn.reghao.autodop.dmaster.backup.scheduler;
|
|
|
|
|
|
import cn.reghao.autodop.common.shell.ShellExecutor;
|
|
|
import cn.reghao.autodop.dmaster.backup.entity.BakService;
|
|
|
+import cn.reghao.autodop.dmaster.common.mongo.MongoManager;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.quartz.*;
|
|
|
import org.quartz.impl.StdSchedulerFactory;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
|
/**
|
|
|
* 备份调度器
|
|
|
@@ -12,20 +14,24 @@ import org.springframework.stereotype.Service;
|
|
|
* @author reghao
|
|
|
* @date 2020-11-10 13:41:11
|
|
|
*/
|
|
|
-@Service
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
public class BackupScheduler {
|
|
|
private Scheduler scheduler;
|
|
|
private ShellExecutor executor;
|
|
|
+ private MongoManager mongoManager;
|
|
|
|
|
|
- public BackupScheduler() throws SchedulerException {
|
|
|
+ public BackupScheduler(MongoManager mongoManager) throws SchedulerException {
|
|
|
this.scheduler = StdSchedulerFactory.getDefaultScheduler();
|
|
|
this.executor = new ShellExecutor();
|
|
|
+ this.mongoManager = mongoManager;
|
|
|
}
|
|
|
|
|
|
public void add(BakService bakService) throws SchedulerException {
|
|
|
JobDataMap jobDataMap = new JobDataMap();
|
|
|
// 只传入一组 KV
|
|
|
- jobDataMap.put(bakService.getBakScript(), executor);
|
|
|
+ jobDataMap.put(bakService.getBakScriptPath(), executor);
|
|
|
+ jobDataMap.put("mongo", mongoManager);
|
|
|
JobDetail jobDetail = JobBuilder.newJob(ScriptBakJob.class)
|
|
|
.withIdentity(bakService.getServiceName())
|
|
|
.usingJobData(jobDataMap)
|
|
|
@@ -38,6 +44,7 @@ public class BackupScheduler {
|
|
|
CronTrigger cronTrigger = triggerBuilder.build();
|
|
|
|
|
|
scheduler.scheduleJob(jobDetail, cronTrigger);
|
|
|
+ log.info("添加新定时任务 {}...", bakService.getServiceName());
|
|
|
}
|
|
|
|
|
|
public void remove() {
|
|
|
@@ -54,26 +61,4 @@ public class BackupScheduler {
|
|
|
scheduler.pauseAll();
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- public static void main(String[] args) throws SchedulerException {
|
|
|
- BakService bakService = new BakService();
|
|
|
- bakService.setServiceName("nexus");
|
|
|
- bakService.setIp("192.168.0.50");
|
|
|
- String bakScript = "logfile=$1\n" +
|
|
|
- "\n" +
|
|
|
- "cat > rsyncd_secret << EOF\n" +
|
|
|
- "123456\n" +
|
|
|
- "EOF\n" +
|
|
|
- "chmod 600 rsyncd_secret\n" +
|
|
|
- "\n" +
|
|
|
- "rsync -avzP --delete --password-file=./rsyncd_secret azy@192.168.0.50::nexus /home/reghao/opt/data/backup/nexus > ${logfile} 2>&1";
|
|
|
- bakScript = "/home/reghao/work/azy/svn/test/4-运维管理/备份还原/rsync_nexus.sh";
|
|
|
- bakService.setBakScript(bakScript);
|
|
|
- String cronExp = "0 */1 * * * ?";
|
|
|
- bakService.setCronExp(cronExp);
|
|
|
-
|
|
|
- BackupScheduler scheduler = new BackupScheduler();
|
|
|
- scheduler.add(bakService);
|
|
|
- scheduler.start();
|
|
|
- }
|
|
|
}
|