|
@@ -0,0 +1,121 @@
|
|
|
+package com.jjt.out.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.jjt.common.domain.FileDesc;
|
|
|
+import com.jjt.common.enums.SyncType;
|
|
|
+import com.jjt.common.utils.CompressZip;
|
|
|
+import com.jjt.common.utils.DateUtils;
|
|
|
+import com.jjt.common.utils.LinuxCommand;
|
|
|
+import com.jjt.out.domain.OutProcessInfo;
|
|
|
+import com.jjt.out.service.IOutMongoService;
|
|
|
+import com.jjt.out.service.IOutMysqlService;
|
|
|
+import com.jjt.out.service.IOutProcessInfoService;
|
|
|
+import com.jjt.system.service.ISysConfigService;
|
|
|
+import org.apache.commons.codec.digest.DigestUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Path;
|
|
|
+import java.nio.file.Paths;
|
|
|
+import java.nio.file.StandardCopyOption;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 数据同步Service业务层处理
|
|
|
+ *
|
|
|
+ * @author wukai
|
|
|
+ * @date 2023-06-06
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class OutMongoServiceImpl extends OutBaseService implements IOutMongoService {
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(OutMongoServiceImpl.class);
|
|
|
+ @Resource
|
|
|
+ private ISysConfigService sysConfigService;
|
|
|
+ @Resource
|
|
|
+ private IOutProcessInfoService processInfoService;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 导出数据库
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void full() {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 增量导出
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void inc() {
|
|
|
+ String params = sysConfigService.selectConfigByKey("out.mongo.info");
|
|
|
+
|
|
|
+ JSONObject mongoInfo = JSONObject.parseObject(params);
|
|
|
+ String host = mongoInfo.getString("host");
|
|
|
+ int port = mongoInfo.getIntValue("port");
|
|
|
+ String user = mongoInfo.getString("user");
|
|
|
+ String pass = mongoInfo.getString("pass");
|
|
|
+
|
|
|
+ String tmpDir = tmpDIr();
|
|
|
+ OutProcessInfo opi = new OutProcessInfo();
|
|
|
+ opi.setProcessType(SyncType.mongo.toString());
|
|
|
+ List<OutProcessInfo> list = processInfoService.selectOutProcessInfoList(opi);
|
|
|
+ String time = list.get(0).getProcessKey();
|
|
|
+
|
|
|
+ String nowTime = DateUtils.dateTimeNow();
|
|
|
+
|
|
|
+ tmpDir += "mongo/" + nowTime + "/";
|
|
|
+ String queryStr = String.format("{\"ts\":{\"$gt\":{\"$timestamp\":{\"t\":%s,\"i\":1}}},\"op\":{\"$ne\":\"d\"},\"op\":{\"$ne\":\"n\"}}", time);
|
|
|
+ String cmd = String.format("/usr/bin/mongodump --host %s --port %s -d local -c oplog.rs -q '%s' -o %s", host, port, queryStr, tmpDir);
|
|
|
+ opi = new OutProcessInfo();
|
|
|
+ opi.setProcessType(SyncType.mongo.toString());
|
|
|
+ Date st = new Date();
|
|
|
+ opi.setCreateTime(st);
|
|
|
+ try {
|
|
|
+ LinuxCommand.exec(cmd);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("报错啦:{}", e.getMessage());
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ Date et = new Date();
|
|
|
+ opi.setCostTime(et.getTime() - st.getTime());
|
|
|
+ processInfoService.insertOutProcessInfo(opi);
|
|
|
+
|
|
|
+
|
|
|
+ //获取外网同步正式目录
|
|
|
+ String syncDir = syncDIr();
|
|
|
+
|
|
|
+ //打包文件--start
|
|
|
+ //生成zip文件全路径名
|
|
|
+ String zipName = "sync-mongo-" + time + ".zip";
|
|
|
+
|
|
|
+ //打包目标目录
|
|
|
+ File targetDir = new File(tmpDir);
|
|
|
+ File zipFile = new File(syncDir + zipName);
|
|
|
+ CompressZip.zip(targetDir, zipFile);
|
|
|
+ //打包文件--end
|
|
|
+
|
|
|
+ //生成描述json文件--start
|
|
|
+ try {
|
|
|
+ String descName = syncDir + "sync-70-" + time + ".json";
|
|
|
+ String md5 = DigestUtils.md5Hex(Files.newInputStream(zipFile.toPath()));
|
|
|
+ FileDesc desc = new FileDesc();
|
|
|
+ desc.setName(zipName);
|
|
|
+ desc.setMd5(md5);
|
|
|
+ desc.setType(SyncType.mongo);
|
|
|
+ File descFile = new File(descName);
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ mapper.writeValue(descFile, desc);
|
|
|
+ } catch (IOException e) {
|
|
|
+ }
|
|
|
+ //生成描述json文件--end
|
|
|
+ }
|
|
|
+}
|