| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- 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 net.lingala.zip4j.ZipFile;
- 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;
- /**
- * 执行备份
- *
- * @param isInc 是否增量 true:增量 false:全量
- */
- private void exec(boolean isInc) {
- String params = sysConfigService.selectConfigByKey("out.mongo.info");
- JSONObject mongoInfo = JSONObject.parseObject(params);
- String host = mongoInfo.getString("host");
- String port = mongoInfo.getString("port");
- String dir = mongoInfo.getString("dir");
- String tmpDir = tmpDIr();
- String nowTime = DateUtils.dateTimeNow();
- tmpDir += "mongo/" + nowTime + "/";
- try {
- //创建目录及父目录
- Files.createDirectories(Paths.get(tmpDir));
- OutProcessInfo opi = new OutProcessInfo();
- opi.setProcessType(SyncType.mongo.toString());
- Date st = new Date();
- opi.setCreateTime(st);
- opi.setProcessKey(String.valueOf(System.currentTimeMillis() / 1000));
- //组装导出命令
- List<String> commands = new ArrayList<>();
- commands.add("/usr/bin/sh");
- if (isInc) {
- //增量
- commands.add("mongo-inc-bak.sh");
- } else {
- //全量
- commands.add("mongo-full-bak.sh");
- }
- commands.add(host);
- commands.add(port);
- if (isInc) {
- //如果是增量,需要获取时间戳
- OutProcessInfo pi = new OutProcessInfo();
- opi.setProcessType(SyncType.mongo.toString());
- List<OutProcessInfo> list = processInfoService.selectOutProcessInfoList(pi);
- String time = list.get(0).getProcessKey();
- commands.add(time);
- }
- commands.add(tmpDir);
- LinuxCommand.exec(commands, dir);
- Date et = new Date();
- opi.setCostTime(et.getTime() - st.getTime());
- processInfoService.insertOutProcessInfo(opi);
- //获取外网同步正式目录
- String syncDir = syncDIr();
- //打包文件--start
- //生成zip文件全路径名
- String zipName = "sync-mongo-" + nowTime + ".zip";
- //打包目标目录
- File targetDir = new File(tmpDir);
- ZipFile zipFile = new ZipFile(syncDir + zipName);
- /**
- * 获取分卷大小
- */
- String size = sysConfigService.selectConfigByKey("file.split.size");
- //GB转换成byte
- long splitSize = 1024 * 1024 * 1024 * Integer.parseInt(size);
- CompressZip.splitZip(targetDir, zipFile, splitSize);
- //打包文件--end
- //生成描述json文件--start
- try {
- String descName = syncDir + "sync-78-" + nowTime + ".json";
- if (!isInc) {
- //如果是全量,执行顺序要靠前,内网解析时,顺序号77代表全量,78代表增量
- descName = syncDir + "sync-77-" + nowTime + ".json";
- }
- String md5 = DigestUtils.md5Hex(Files.newInputStream(zipFile.getFile().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
- } catch (Exception e) {
- log.error("报错啦:{}", e.getMessage());
- e.printStackTrace();
- }
- }
- /**
- * 全量备份
- */
- @Override
- public void full() {
- exec(false);
- }
- /**
- * 增量备份
- */
- @Override
- public void inc() {
- exec(true);
- }
- }
|