|
@@ -0,0 +1,94 @@
|
|
|
+package com.jjt.out.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.jjt.common.constant.Constants;
|
|
|
+import com.jjt.common.domain.FileDesc;
|
|
|
+import com.jjt.common.domain.IndexDO;
|
|
|
+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.service.IOutEsService;
|
|
|
+import com.jjt.out.service.IOutMysqlService;
|
|
|
+import com.jjt.system.service.ISysConfigService;
|
|
|
+import org.apache.commons.codec.digest.DigestUtils;
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
+import org.apache.http.HttpHost;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+import org.elasticsearch.client.Request;
|
|
|
+import org.elasticsearch.client.RestClient;
|
|
|
+import org.elasticsearch.client.RestHighLevelClient;
|
|
|
+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.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 数据同步Service业务层处理
|
|
|
+ *
|
|
|
+ * @author wukai
|
|
|
+ * @date 2023-06-06
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class OutMysqlServiceImpl extends OutBaseService implements IOutMysqlService {
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(OutMysqlServiceImpl.class);
|
|
|
+ @Resource
|
|
|
+ private ISysConfigService sysConfigService;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 导出数据库
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void export() {
|
|
|
+ String params = sysConfigService.selectConfigByKey("out.mysql.info");
|
|
|
+
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(params);
|
|
|
+
|
|
|
+// String hostname = jsonObject.getString("host");
|
|
|
+// int port = jsonObject.getIntValue("port");
|
|
|
+
|
|
|
+ String user = jsonObject.getString("user");
|
|
|
+ String pass = jsonObject.getString("pass");
|
|
|
+ String db = jsonObject.getString("db");
|
|
|
+
|
|
|
+ //组装导出命令
|
|
|
+ String time = DateUtils.dateTimeNow();
|
|
|
+ String tmpDir = tmpDIr();
|
|
|
+ String syncDir = syncDIr();
|
|
|
+ String exportName = String.format("%s_%s.sql", db, time);
|
|
|
+ String exportPath = tmpDir + exportName;
|
|
|
+ String cmd = String.format("mysqldump -u%s -p%s %s > %s", user, pass, db, exportPath);
|
|
|
+ LinuxCommand.exec(cmd);
|
|
|
+
|
|
|
+ try {
|
|
|
+ //移动备份文件
|
|
|
+ Path target = Paths.get(syncDir + exportName);
|
|
|
+ Files.move(Paths.get(exportPath), target, StandardCopyOption.REPLACE_EXISTING);
|
|
|
+
|
|
|
+ //生成描述json文件--start
|
|
|
+ String descName = syncDir + "sync-60-" + time + ".json";
|
|
|
+ String md5 = DigestUtils.md5Hex(Files.newInputStream(target));
|
|
|
+ FileDesc desc = new FileDesc();
|
|
|
+ desc.setName(exportName);
|
|
|
+ desc.setMd5(md5);
|
|
|
+ desc.setType(SyncType.mysql);
|
|
|
+ File descFile = new File(descName);
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ mapper.writeValue(descFile, desc);
|
|
|
+ //生成描述json文件--end
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("mysql备份文件从临时目录移动至同步目录失败!", exportPath, syncDir + exportName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|