parent
d58d783979
commit
2e4f7eb099
|
|
@ -68,4 +68,27 @@ public class FileController {
|
|||
public ResponseEntity<InputStreamResource> download(@RequestParam("id") String id) throws Exception {
|
||||
return fileService.download(id);
|
||||
}
|
||||
|
||||
@RequestMapping("/backupSql")
|
||||
public ElectromagneticResult<?> backupSql(@RequestParam("file") MultipartFile file) {
|
||||
BackupFileResLog backupFileResLog = BackupFileResLog.builder().backupStartTime(new Date()).fileName(file.getOriginalFilename()).backupSuccess(true).build();
|
||||
try {
|
||||
fileService.backupSql(file);
|
||||
} catch (Exception e) {
|
||||
String details = ExceptionUtil.stacktraceToString(e);
|
||||
backupFileResLog.setBackupSuccess(false);
|
||||
backupFileResLog.setFailInfoDetail(details);
|
||||
log.error("备份sql文件失败,原因--->{}", e.getMessage(), e);
|
||||
}
|
||||
JSONConfig jsonConfig = JSONConfig.create();
|
||||
jsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
backupFileResLog.setBackupEndTime(new Date());
|
||||
StringBuffer info = new StringBuffer()
|
||||
.append("\n")
|
||||
.append("#")
|
||||
.append("\n")
|
||||
.append(JSONUtil.toJsonStr(backupFileResLog, jsonConfig));
|
||||
FileUtil.appendUtf8String(info.toString(), backupPro.getLogPath());
|
||||
return ElectromagneticResultUtil.success(JSONUtil.toJsonStr(backupFileResLog, jsonConfig));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,4 +14,6 @@ public interface FileService {
|
|||
void remove(String id);
|
||||
|
||||
ResponseEntity<InputStreamResource> download(String id) throws Exception;
|
||||
|
||||
void backupSql(MultipartFile file) throws Exception;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,11 @@
|
|||
package com.electromagnetic.industry.software.backup.serviceimp;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.util.ZipUtil;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import com.electromagnetic.industry.software.backup.pojo.BackupPro;
|
||||
import com.electromagnetic.industry.software.backup.service.FileService;
|
||||
import com.electromagnetic.industry.software.common.util.EleCommonUtil;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.InputStreamResource;
|
||||
import org.springframework.http.MediaType;
|
||||
|
|
@ -14,6 +17,8 @@ import javax.annotation.Resource;
|
|||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import static com.electromagnetic.industry.software.common.cons.ElectromagneticConstants.FILE_SEC_PASSWD;
|
||||
|
||||
@Service
|
||||
public class FileServiceImpl implements FileService {
|
||||
|
||||
|
|
@ -45,9 +50,22 @@ public class FileServiceImpl implements FileService {
|
|||
.body(new InputStreamResource(fileSystemResource.getInputStream()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void backupSql(MultipartFile file) throws IOException {
|
||||
String destPath = backupPro.getSaveFolder() + File.separator + "sqls" + File.separator + file.getOriginalFilename();
|
||||
if (!FileUtil.exist(destPath)) {
|
||||
FileUtil.writeFromStream(file.getInputStream(), destPath);
|
||||
}
|
||||
int index = destPath.lastIndexOf(".");
|
||||
String zipPath = destPath.substring(0, index) + ".zip";
|
||||
ZipUtil.zip(destPath, zipPath);
|
||||
EleCommonUtil.encryptFile(zipPath, SecureUtil.aes(FILE_SEC_PASSWD.getBytes()));
|
||||
FileUtil.del(destPath);
|
||||
}
|
||||
|
||||
private String getFileSysPathById(String id) {
|
||||
String saveFolder = backupPro.getSaveFolder();
|
||||
return saveFolder + File.separator + id;
|
||||
return saveFolder + File.separator + "prj_files" + File.separator + id;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -54,6 +54,10 @@ public class ElePropertyConfig {
|
|||
@Value("${winPrefix}")
|
||||
private String winPrefix = "";
|
||||
|
||||
@Getter
|
||||
@Value("${backup.mysql.path}")
|
||||
private String sqlDirs;
|
||||
|
||||
public String getEleTmpPath() {
|
||||
if (EleCommonUtil.isWinOs()) {
|
||||
return winPrefix + eleTmpPath;
|
||||
|
|
|
|||
|
|
@ -3,7 +3,9 @@ package com.electromagnetic.industry.software.manage.controller;
|
|||
import com.electromagnetic.industry.software.common.annotations.UserOperation;
|
||||
import com.electromagnetic.industry.software.common.enums.UserOperationModuleEnum;
|
||||
import com.electromagnetic.industry.software.common.resp.ElectromagneticResult;
|
||||
import com.electromagnetic.industry.software.common.util.ElectromagneticResultUtil;
|
||||
import com.electromagnetic.industry.software.manage.service.FileBackLogService;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
|
@ -22,4 +24,11 @@ public class FileBackupLogController {
|
|||
public ElectromagneticResult<?> list(@RequestParam int pageNum, @RequestParam int pageSize) {
|
||||
return fileBackLogService.query(pageNum, pageSize);
|
||||
}
|
||||
|
||||
@GetMapping(value = "restore")
|
||||
@UserOperation(value = "系统恢复", modelName = UserOperationModuleEnum.BACKUP_FILE)
|
||||
public ElectromagneticResult<?> restore() {
|
||||
fileBackLogService.restore();
|
||||
return ElectromagneticResultUtil.success(true);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,4 +6,5 @@ public interface FileBackLogService {
|
|||
|
||||
ElectromagneticResult<?> query(Integer pageNumber, Integer pageSize);
|
||||
|
||||
void restore();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,25 +3,39 @@ package com.electromagnetic.industry.software.manage.service.serviceimpl;
|
|||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.electromagnetic.industry.software.common.enums.EleDataTypeEnum;
|
||||
import com.electromagnetic.industry.software.common.enums.FileBackupSource;
|
||||
import com.electromagnetic.industry.software.common.resp.ElectromagneticResult;
|
||||
import com.electromagnetic.industry.software.common.util.ElectromagneticResultUtil;
|
||||
import com.electromagnetic.industry.software.common.util.UserThreadLocal;
|
||||
import com.electromagnetic.industry.software.manage.mapper.EdFileInfoMapper;
|
||||
import com.electromagnetic.industry.software.manage.mapper.FileBackupLogMapper;
|
||||
import com.electromagnetic.industry.software.manage.pojo.models.EdFileInfo;
|
||||
import com.electromagnetic.industry.software.manage.pojo.models.FileBackupLog;
|
||||
import com.electromagnetic.industry.software.manage.pojo.resp.FileBackLogVO;
|
||||
import com.electromagnetic.industry.software.manage.pojo.resp.RespPageVO;
|
||||
import com.electromagnetic.industry.software.manage.service.FileBackLogService;
|
||||
import com.electromagnetic.industry.software.manage.tasks.BackupHandler;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class FileBackLogServiceImpl extends ServiceImpl<FileBackupLogMapper, FileBackupLog> implements FileBackLogService {
|
||||
|
||||
@Resource
|
||||
private EdFileInfoMapper edFileInfoMapper;
|
||||
@Resource
|
||||
private BackupHandler backupHandler;
|
||||
@Resource
|
||||
private CommonService commonService;
|
||||
|
||||
@Override
|
||||
public ElectromagneticResult<?> query(Integer pageNumber, Integer pageSize) {
|
||||
|
|
@ -41,6 +55,21 @@ public class FileBackLogServiceImpl extends ServiceImpl<FileBackupLogMapper, Fil
|
|||
fileBackLogVO.setBackEndTime(DateUtil.date(fileBackupLog.getEndTime()));
|
||||
list.add(fileBackLogVO);
|
||||
}
|
||||
UserThreadLocal.setSuccessInfo("", "", "查询备份日志成功");
|
||||
return ElectromagneticResultUtil.success(new RespPageVO<>(total, list));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void restore() {
|
||||
List<EdFileInfo> edFileInfos = edFileInfoMapper.selectList(Wrappers.lambdaQuery(EdFileInfo.class)
|
||||
.select(EdFileInfo.class, file -> !StrUtil.equals(file.getColumn(), "file_content"))
|
||||
.eq(EdFileInfo::getPermanentDeleted, false)
|
||||
.eq(EdFileInfo::getDataType, EleDataTypeEnum.FILE.code));
|
||||
for (EdFileInfo edFileInfo : edFileInfos) {
|
||||
byte[] bytes = backupHandler.downloadFile(edFileInfo.getId());
|
||||
String destPath = commonService.getFileSysPath(edFileInfo.getFilePath(), edFileInfo.getDataOwn());
|
||||
FileUtil.writeBytes(bytes, destPath);
|
||||
}
|
||||
UserThreadLocal.setSuccessInfo("", "", "数据库恢复成功");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,12 +2,14 @@ package com.electromagnetic.industry.software.manage.service.serviceimpl;
|
|||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.electromagnetic.industry.software.common.enums.*;
|
||||
import com.electromagnetic.industry.software.common.pojo.BackupFileResLog;
|
||||
import com.electromagnetic.industry.software.common.resp.ElectromagneticResult;
|
||||
import com.electromagnetic.industry.software.common.util.EleCommonUtil;
|
||||
import com.electromagnetic.industry.software.common.util.ElectromagneticResultUtil;
|
||||
|
|
@ -24,7 +26,9 @@ import com.electromagnetic.industry.software.manage.pojo.resp.RespPageVO;
|
|||
import com.electromagnetic.industry.software.manage.service.FileRecycleService;
|
||||
import com.electromagnetic.industry.software.manage.service.FileSystemService;
|
||||
import com.electromagnetic.industry.software.manage.service.PermissionService;
|
||||
import com.electromagnetic.industry.software.manage.tasks.BackupHandler;
|
||||
import com.electromagnetic.industry.software.manage.tasks.BackupTask;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
|
|
@ -32,9 +36,11 @@ import javax.annotation.Resource;
|
|||
import java.io.File;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class FileRecycleServiceImpl implements FileRecycleService {
|
||||
|
||||
@Resource
|
||||
|
|
@ -46,13 +52,11 @@ public class FileRecycleServiceImpl implements FileRecycleService {
|
|||
@Resource
|
||||
private CommonService commonService;
|
||||
@Resource
|
||||
private FileBackupLogMapper fileBackupLogMapper;
|
||||
@Resource
|
||||
private FileSystemService fileSystemService;
|
||||
@Resource
|
||||
private ElePropertyConfig elePropertyConfig;
|
||||
@Resource
|
||||
private BackupTask backupTask;
|
||||
private BackupHandler backupHandler;
|
||||
|
||||
@Override
|
||||
public ElectromagneticResult<?> list(RecycleFileQueryDTO pars) {
|
||||
|
|
@ -113,86 +117,27 @@ public class FileRecycleServiceImpl implements FileRecycleService {
|
|||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public ElectromagneticResult<?> remove(String fileId) {
|
||||
|
||||
// 备份该文件
|
||||
List<EdFileInfo> edFileInfos = this.edFileInfoMapper.selectList(Wrappers.lambdaQuery(EdFileInfo.class)
|
||||
.select(EdFileInfo.class, file -> !StrUtil.equals(file.getColumn(), "file_content"))
|
||||
.eq(EdFileInfo::getFileId, fileId));
|
||||
// List<String> fileSysPaths = new ArrayList<>();
|
||||
for (EdFileInfo edFileInfo : edFileInfos) {
|
||||
String fileSysPath = commonService.getFileSysPath(edFileInfo.getFilePath(), edFileInfo.getDataOwn());
|
||||
// 移动到tmp目录,七天后删除
|
||||
fileSystemService.moveFile(fileSysPath, elePropertyConfig.getEleTmpPath() + File.separator + new File(fileSysPath).getName());
|
||||
// FileBackupLog fileBackupLog = fileBackupLogMapper.selectOne(Wrappers.lambdaQuery(FileBackupLog.class).eq(FileBackupLog::getFileId, edFileInfo.getId()));
|
||||
//
|
||||
// String saveFileName = edFileInfo.getFileName() + "." + edFileInfo.getFileType() + "." + edFileInfo.getFileCode();
|
||||
|
||||
// // 表示从没有备份过该文件
|
||||
// if (fileBackupLog == null) {
|
||||
// long startTime = System.currentTimeMillis();
|
||||
// BackupFileResLog backup = backupTask.backup(fileSysPath, edFileInfo.getId());
|
||||
// long endTime = System.currentTimeMillis();
|
||||
//
|
||||
// FileBackupLog backupLog = new FileBackupLog()
|
||||
// .setId(IdWorker.getSnowFlakeIdString())
|
||||
// .setFileId(edFileInfo.getId())
|
||||
// .setStartTime(startTime)
|
||||
// .setEndTime(endTime)
|
||||
// .setDuration(endTime - startTime)
|
||||
// .setFileTime(FileUtil.lastModifiedTime(fileSysPath).getTime())
|
||||
// .setCreateTime(new Date())
|
||||
// .setFileName(saveFileName)
|
||||
// .setFileCode(commonService.getFileCode(fileSysPath))
|
||||
// .setFileCreateTime(edFileInfo.getCreatedTime())
|
||||
// .setSource(FileBackupSource.REMOVE.code);
|
||||
//
|
||||
// if (backup.getBackupSuccess()) {
|
||||
// backupLog.setBackupSuccess(true);
|
||||
// fileBackupLogMapper.insert(backupLog);
|
||||
// } else {
|
||||
// backupLog.setBackupSuccess(false);
|
||||
// backupLog.setFailInfoDetail(backup.getFailInfoDetail());
|
||||
// fileBackupLogMapper.insert(backupLog);
|
||||
// throw new BizException(StrFormatter.format("删除文件 {} 失败,原因 备份该文件出现错误,联系管理员查看日志", saveFileName));
|
||||
// }
|
||||
//
|
||||
// } else {
|
||||
// long startTime = System.currentTimeMillis();
|
||||
// BackupFileResLog backup = backupTask.backup(fileSysPath, edFileInfo.getId());
|
||||
// long endTime = System.currentTimeMillis();
|
||||
// fileBackupLog.setStartTime(startTime)
|
||||
// .setEndTime(endTime)
|
||||
// .setDuration(endTime - startTime)
|
||||
// .setFileTime(FileUtil.lastModifiedTime(fileSysPath).getTime())
|
||||
// .setCreateTime(new Date())
|
||||
// .setFileCode(commonService.getFileCode(fileSysPath))
|
||||
// .setFileCreateTime(edFileInfo.getCreatedTime())
|
||||
// .setFileName(saveFileName);
|
||||
// if (backup.getBackupSuccess()) {
|
||||
// fileBackupLog.setBackupSuccess(true);
|
||||
// fileBackupLogMapper.update(fileBackupLog, null);
|
||||
// } else {
|
||||
// fileBackupLog.setBackupSuccess(false);
|
||||
// fileBackupLog.setFailInfoDetail(backup.getFailInfoDetail());
|
||||
// fileBackupLogMapper.update(fileBackupLog, null);
|
||||
// throw new BizException(StrFormatter.format("删除文件 {} 失败,原因 备份该文件出现错误,联系管理员查看日志", saveFileName));
|
||||
// }
|
||||
// }
|
||||
// fileSysPaths.add(fileSysPath);
|
||||
// 更新MySQL数据库
|
||||
this.edFileInfoMapper.update(new EdFileInfo(), Wrappers.lambdaUpdate(EdFileInfo.class)
|
||||
.eq(EdFileInfo::getFileId, fileId)
|
||||
.set(EdFileInfo::getEffectFlag, EffectFlagEnum.NOT_EFFECTIVE.code)
|
||||
.set(EdFileInfo::getPermanentDeleted, true)
|
||||
.set(EdFileInfo::getAllDeleted, true));
|
||||
UserThreadLocal.setSuccessInfo("", "", "删除文件 {} 成功,文件id {}", edFileInfos.get(0).getFileName() + "." + edFileInfos.get(0).getFileType(), fileId);
|
||||
BackupFileResLog resLog = backupHandler.deleteFile(edFileInfo.getId());
|
||||
if (!Optional.ofNullable(resLog).map(BackupFileResLog::getBackupSuccess).orElse(false)) {
|
||||
log.warn("删除备份文件异常");
|
||||
}
|
||||
return ElectromagneticResultUtil.success("删除文件成功");
|
||||
}
|
||||
|
||||
// 移动到tmp目录,七天后删除
|
||||
// for (String fileSysPath : fileSysPaths) {
|
||||
//
|
||||
// }
|
||||
|
||||
// 更新MySQL数据库
|
||||
this.edFileInfoMapper.update(new EdFileInfo(), Wrappers.lambdaUpdate(EdFileInfo.class)
|
||||
.eq(EdFileInfo::getFileId, fileId)
|
||||
.set(EdFileInfo::getEffectFlag, EffectFlagEnum.NOT_EFFECTIVE.code)
|
||||
.set(EdFileInfo::getPermanentDeleted, true)
|
||||
.set(EdFileInfo::getAllDeleted, true));
|
||||
UserThreadLocal.setSuccessInfo("", "", "删除文件 {} 成功,文件id {}", edFileInfos.get(0).getFileName() + "." + edFileInfos.get(0).getFileType(), fileId);
|
||||
return ElectromagneticResultUtil.success("删除文件成功");
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,57 @@
|
|||
package com.electromagnetic.industry.software.manage.tasks;
|
||||
|
||||
import cn.hutool.core.text.StrFormatter;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.electromagnetic.industry.software.common.pojo.BackupFileResLog;
|
||||
import com.electromagnetic.industry.software.common.resp.ElectromagneticResult;
|
||||
import com.electromagnetic.industry.software.manage.config.ElePropertyConfig;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
public class BackupHandler {
|
||||
|
||||
@Resource
|
||||
private ElePropertyConfig elePropertyConfig;
|
||||
|
||||
public BackupFileResLog backupFiles(String filePath, String id) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("file", new File(filePath));
|
||||
map.put("id", id);
|
||||
String url = StrFormatter.format("http://{}:{}/data/file/upload", elePropertyConfig.getRemoteHost(), elePropertyConfig.getRemotePort());
|
||||
String res = HttpUtil.post(url, map);
|
||||
ElectromagneticResult<?> resObj = JSONUtil.toBean(res, ElectromagneticResult.class);
|
||||
String data = JSONUtil.toJsonStr(resObj.getData());
|
||||
return JSONUtil.toBean(data, BackupFileResLog.class);
|
||||
}
|
||||
|
||||
public BackupFileResLog backupSql(String filePath) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("file", new File(filePath));
|
||||
String url = StrFormatter.format("http://{}:{}/data/file/backupSql", elePropertyConfig.getRemoteHost(), elePropertyConfig.getRemotePort());
|
||||
String res = HttpUtil.post(url, map);
|
||||
ElectromagneticResult<?> resObj = JSONUtil.toBean(res, ElectromagneticResult.class);
|
||||
String data = JSONUtil.toJsonStr(resObj.getData());
|
||||
return JSONUtil.toBean(data, BackupFileResLog.class);
|
||||
}
|
||||
|
||||
public BackupFileResLog deleteFile(String id) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("id", id);
|
||||
String url = StrFormatter.format("http://{}:{}/data/file/remove", elePropertyConfig.getRemoteHost(), elePropertyConfig.getRemotePort());
|
||||
String res = HttpUtil.get(url, map);
|
||||
ElectromagneticResult<?> resObj = JSONUtil.toBean(res, ElectromagneticResult.class);
|
||||
String data = JSONUtil.toJsonStr(resObj.getData());
|
||||
return JSONUtil.toBean(data, BackupFileResLog.class);
|
||||
}
|
||||
|
||||
public byte[] downloadFile(String id) {
|
||||
String url = StrFormatter.format("http://{}:{}/data/file/download?id={}", elePropertyConfig.getRemoteHost(), elePropertyConfig.getRemotePort(), id);
|
||||
return HttpUtil.downloadBytes(url);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,19 +1,17 @@
|
|||
package com.electromagnetic.industry.software.manage.tasks;
|
||||
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.date.DateTime;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.text.StrFormatter;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.electromagnetic.industry.software.common.enums.EleDataTypeEnum;
|
||||
import com.electromagnetic.industry.software.common.enums.FileBackupSource;
|
||||
import com.electromagnetic.industry.software.common.enums.UserOperationModuleEnum;
|
||||
import com.electromagnetic.industry.software.common.pojo.BackupFileResLog;
|
||||
import com.electromagnetic.industry.software.common.resp.ElectromagneticResult;
|
||||
import com.electromagnetic.industry.software.common.util.IdWorker;
|
||||
import com.electromagnetic.industry.software.manage.config.ElePropertyConfig;
|
||||
import com.electromagnetic.industry.software.manage.mapper.EdFileInfoMapper;
|
||||
|
|
@ -31,9 +29,7 @@ import javax.annotation.Resource;
|
|||
import java.io.File;
|
||||
import java.io.FileFilter;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Component
|
||||
|
|
@ -51,11 +47,8 @@ public class BackupTask {
|
|||
private EdFileInfoMapper edFileInfoMapper;
|
||||
@Resource
|
||||
private CommonService commonService;
|
||||
|
||||
private static List<File> filter(long time, String dir) {
|
||||
FileFilter filter = file -> file.lastModified() < time;
|
||||
return FileUtil.loopFiles(dir, filter);
|
||||
}
|
||||
@Resource
|
||||
private BackupHandler backupHandler;
|
||||
|
||||
@Scheduled(cron = "0 0 1 * * ?")
|
||||
public void backup() {
|
||||
|
|
@ -68,7 +61,7 @@ public class BackupTask {
|
|||
.eq(EdFileInfo::getId, id));
|
||||
String sysFilePath = commonService.getFileSysPath(fileInfo.getFilePath(), fileInfo.getDataOwn());
|
||||
long startTime = System.currentTimeMillis();
|
||||
BackupFileResLog resLog = backup(sysFilePath, id);
|
||||
BackupFileResLog resLog = backupHandler.backupFiles(sysFilePath, id);
|
||||
long endTime = System.currentTimeMillis();
|
||||
fileBackupLogMapper.update(null, Wrappers.<FileBackupLog>lambdaUpdate()
|
||||
.eq(FileBackupLog::getFileId, id)
|
||||
|
|
@ -95,7 +88,7 @@ public class BackupTask {
|
|||
}
|
||||
String fileSysPath = commonService.getFileSysPath(edFileInfo.getFilePath(), edFileInfo.getDataOwn());
|
||||
long startTime = System.currentTimeMillis();
|
||||
BackupFileResLog resLog = backup(fileSysPath, edFileInfo.getId());
|
||||
BackupFileResLog resLog = backupHandler.backupFiles(fileSysPath, edFileInfo.getId());
|
||||
long endTime = System.currentTimeMillis();
|
||||
FileBackupLog backupLog = new FileBackupLog()
|
||||
.setId(IdWorker.getSnowFlakeIdString())
|
||||
|
|
@ -163,14 +156,61 @@ public class BackupTask {
|
|||
}
|
||||
}
|
||||
|
||||
public BackupFileResLog backup(String filePath, String id) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("file", new File(filePath));
|
||||
map.put("id", id);
|
||||
String url = StrFormatter.format("http://{}:{}/data/file/backup/upload", elePropertyConfig.getRemoteHost(), elePropertyConfig.getRemotePort());
|
||||
String res = HttpUtil.post(url, map);
|
||||
ElectromagneticResult<?> resObj = JSONUtil.toBean(res, ElectromagneticResult.class);
|
||||
String data = JSONUtil.toJsonStr(resObj.getData());
|
||||
return JSONUtil.toBean(data, BackupFileResLog.class);
|
||||
@Scheduled(cron = "0 0 * * * ?")
|
||||
public void backupSql() {
|
||||
String sqlDirs = elePropertyConfig.getSqlDirs();
|
||||
File[] files = new File(sqlDirs).listFiles((file, name) -> name.endsWith(".sql"));
|
||||
File maxModifyTimeFile = null;
|
||||
long tmp = 0;
|
||||
if (ArrayUtil.isEmpty(files)) {
|
||||
return;
|
||||
}
|
||||
for (File file : files) {
|
||||
if (file.lastModified() > tmp) {
|
||||
tmp = file.lastModified();
|
||||
maxModifyTimeFile = file;
|
||||
}
|
||||
}
|
||||
|
||||
if (maxModifyTimeFile == null) {
|
||||
return;
|
||||
}
|
||||
String fileName = maxModifyTimeFile.getName();
|
||||
List<FileBackupLog> fileBackupLogs = fileBackupLogMapper.selectList(Wrappers.<FileBackupLog>lambdaQuery()
|
||||
.eq(FileBackupLog::getFileName, fileName)
|
||||
.eq(FileBackupLog::getSource, FileBackupSource.SQL.code));
|
||||
List<FileBackupLog> successBacks = fileBackupLogs.stream().filter(FileBackupLog::isBackupSuccess).collect(Collectors.toList());
|
||||
if (CollUtil.isNotEmpty(successBacks)) {
|
||||
return;
|
||||
}
|
||||
long startTime = System.currentTimeMillis();
|
||||
BackupFileResLog resLog = backupHandler.backupSql(maxModifyTimeFile.getAbsolutePath());
|
||||
long endTime = System.currentTimeMillis();
|
||||
List<FileBackupLog> failBacks = fileBackupLogs.stream().filter(FileBackupLog::isBackupSuccess).collect(Collectors.toList());
|
||||
if (CollUtil.isEmpty(failBacks)) {
|
||||
FileBackupLog backupLog = new FileBackupLog()
|
||||
.setId(IdWorker.getSnowFlakeIdString())
|
||||
.setFileId(Base64.encode(fileName))
|
||||
.setFileCode(Base64.encode(fileName))
|
||||
.setBackupSuccess(resLog.getBackupSuccess())
|
||||
.setCreateTime(new Date())
|
||||
.setStartTime(startTime)
|
||||
.setEndTime(endTime)
|
||||
.setDuration(endTime - startTime)
|
||||
.setFailInfoDetail(resLog.getFailInfoDetail())
|
||||
.setFileTime(FileUtil.lastModifiedTime(maxModifyTimeFile).getTime())
|
||||
.setFileName(fileName)
|
||||
.setFileCreateTime(FileUtil.lastModifiedTime(maxModifyTimeFile))
|
||||
.setSource(FileBackupSource.SQL.code);
|
||||
fileBackupLogMapper.insert(backupLog);
|
||||
} else {
|
||||
fileBackupLogMapper.update(new FileBackupLog(), Wrappers.<FileBackupLog>lambdaUpdate()
|
||||
.eq(FileBackupLog::getSource, FileBackupSource.SQL.code)
|
||||
.eq(FileBackupLog::getFileName, fileName)
|
||||
.set(FileBackupLog::getFailInfoDetail, resLog.getFailInfoDetail())
|
||||
.set(FileBackupLog::isBackupSuccess, resLog.getBackupSuccess()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ data.repo.download.path=/szsd/data/eleData/dev/repo_download/
|
|||
|
||||
prj.folder.max.length=6
|
||||
|
||||
# backup
|
||||
# backupFiles
|
||||
tmp.file.store.days=7
|
||||
backup.remote.host=127.0.0.1
|
||||
backup.remote.port=1111
|
||||
|
|
|
|||
|
|
@ -6,7 +6,8 @@ import lombok.AllArgsConstructor;
|
|||
public enum FileBackupSource {
|
||||
|
||||
SYS_BACKUP(0, "系统备份"),
|
||||
REMOVE(1, "删除文件");
|
||||
REMOVE(1, "删除文件"),
|
||||
SQL(2, "sql文件");
|
||||
|
||||
public int code;
|
||||
public String desc;
|
||||
|
|
|
|||
Loading…
Reference in New Issue