OutMongoServiceImpl.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package com.jjt.out.service.impl;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import com.jjt.common.domain.FileDesc;
  5. import com.jjt.common.enums.SyncType;
  6. import com.jjt.common.utils.CompressZip;
  7. import com.jjt.common.utils.DateUtils;
  8. import com.jjt.common.utils.LinuxCommand;
  9. import com.jjt.out.domain.OutProcessInfo;
  10. import com.jjt.out.service.IOutMongoService;
  11. import com.jjt.out.service.IOutMysqlService;
  12. import com.jjt.out.service.IOutProcessInfoService;
  13. import com.jjt.system.service.ISysConfigService;
  14. import net.lingala.zip4j.ZipFile;
  15. import org.apache.commons.codec.digest.DigestUtils;
  16. import org.slf4j.Logger;
  17. import org.slf4j.LoggerFactory;
  18. import org.springframework.stereotype.Service;
  19. import javax.annotation.Resource;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.nio.file.Files;
  23. import java.nio.file.Path;
  24. import java.nio.file.Paths;
  25. import java.nio.file.StandardCopyOption;
  26. import java.util.ArrayList;
  27. import java.util.Date;
  28. import java.util.List;
  29. /**
  30. * 数据同步Service业务层处理
  31. *
  32. * @author wukai
  33. * @date 2023-06-06
  34. */
  35. @Service
  36. public class OutMongoServiceImpl extends OutBaseService implements IOutMongoService {
  37. private static final Logger log = LoggerFactory.getLogger(OutMongoServiceImpl.class);
  38. @Resource
  39. private ISysConfigService sysConfigService;
  40. @Resource
  41. private IOutProcessInfoService processInfoService;
  42. /**
  43. * 执行备份
  44. *
  45. * @param isInc 是否增量 true:增量 false:全量
  46. */
  47. private void exec(boolean isInc) {
  48. String params = sysConfigService.selectConfigByKey("out.mongo.info");
  49. JSONObject mongoInfo = JSONObject.parseObject(params);
  50. String host = mongoInfo.getString("host");
  51. String port = mongoInfo.getString("port");
  52. String dir = mongoInfo.getString("dir");
  53. String tmpDir = tmpDIr();
  54. String nowTime = DateUtils.dateTimeNow();
  55. tmpDir += "mongo/" + nowTime + "/";
  56. try {
  57. //创建目录及父目录
  58. Files.createDirectories(Paths.get(tmpDir));
  59. OutProcessInfo opi = new OutProcessInfo();
  60. opi.setProcessType(SyncType.mongo.toString());
  61. Date st = new Date();
  62. opi.setCreateTime(st);
  63. opi.setProcessKey(String.valueOf(System.currentTimeMillis() / 1000));
  64. //组装导出命令
  65. List<String> commands = new ArrayList<>();
  66. commands.add("/usr/bin/sh");
  67. if (isInc) {
  68. //增量
  69. commands.add("mongo-inc-bak.sh");
  70. } else {
  71. //全量
  72. commands.add("mongo-full-bak.sh");
  73. }
  74. commands.add(host);
  75. commands.add(port);
  76. if (isInc) {
  77. //如果是增量,需要获取时间戳
  78. OutProcessInfo pi = new OutProcessInfo();
  79. opi.setProcessType(SyncType.mongo.toString());
  80. List<OutProcessInfo> list = processInfoService.selectOutProcessInfoList(pi);
  81. String time = list.get(0).getProcessKey();
  82. commands.add(time);
  83. }
  84. commands.add(tmpDir);
  85. LinuxCommand.exec(commands, dir);
  86. Date et = new Date();
  87. opi.setCostTime(et.getTime() - st.getTime());
  88. processInfoService.insertOutProcessInfo(opi);
  89. //获取外网同步正式目录
  90. String syncDir = syncDIr();
  91. //打包文件--start
  92. //生成zip文件全路径名
  93. String zipName = "sync-mongo-" + nowTime + ".zip";
  94. //打包目标目录
  95. File targetDir = new File(tmpDir);
  96. ZipFile zipFile = new ZipFile(syncDir + zipName);
  97. /**
  98. * 获取分卷大小
  99. */
  100. String size = sysConfigService.selectConfigByKey("file.split.size");
  101. //GB转换成byte
  102. long splitSize = 1024 * 1024 * 1024 * Integer.parseInt(size);
  103. CompressZip.splitZip(targetDir, zipFile, splitSize);
  104. //打包文件--end
  105. //生成描述json文件--start
  106. try {
  107. String descName = syncDir + "sync-78-" + nowTime + ".json";
  108. if (!isInc) {
  109. //如果是全量,执行顺序要靠前,内网解析时,顺序号77代表全量,78代表增量
  110. descName = syncDir + "sync-77-" + nowTime + ".json";
  111. }
  112. String md5 = DigestUtils.md5Hex(Files.newInputStream(zipFile.getFile().toPath()));
  113. FileDesc desc = new FileDesc();
  114. desc.setName(zipName);
  115. desc.setMd5(md5);
  116. desc.setType(SyncType.mongo);
  117. File descFile = new File(descName);
  118. ObjectMapper mapper = new ObjectMapper();
  119. mapper.writeValue(descFile, desc);
  120. } catch (IOException e) {
  121. }
  122. //生成描述json文件--end
  123. } catch (Exception e) {
  124. log.error("报错啦:{}", e.getMessage());
  125. e.printStackTrace();
  126. }
  127. }
  128. /**
  129. * 全量备份
  130. */
  131. @Override
  132. public void full() {
  133. exec(false);
  134. }
  135. /**
  136. * 增量备份
  137. */
  138. @Override
  139. public void inc() {
  140. exec(true);
  141. }
  142. }