1.添加了文件备份的工程,并实现了基础功能。2.增加了定时备份文件的核心代码。
This commit is contained in:
parent
e097e6e1aa
commit
f59031bd5c
|
|
@ -0,0 +1,64 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.electromagnetic.data</groupId>
|
||||||
|
<artifactId>electromagnetic-data-new</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>electrmangnetic-backup</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>8</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.hutool</groupId>
|
||||||
|
<artifactId>hutool-all</artifactId>
|
||||||
|
<version>5.8.22</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.electromagnetic.data</groupId>
|
||||||
|
<artifactId>electromagnetic-common</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>electrmangnetic-backup</finalName>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
<version>2.6.12</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<configuration>
|
||||||
|
<mainClass>com.electromagnetic.industry.software.backup.MainApp</mainClass>
|
||||||
|
</configuration>
|
||||||
|
<goals>
|
||||||
|
<goal>repackage</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
|
||||||
|
</project>
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.electromagnetic.industry.software.backup;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class MainApp {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(MainApp.class, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,53 @@
|
||||||
|
package com.electromagnetic.industry.software.backup.controller;
|
||||||
|
|
||||||
|
import cn.hutool.core.exceptions.ExceptionUtil;
|
||||||
|
import cn.hutool.core.io.FileUtil;
|
||||||
|
import cn.hutool.json.JSONConfig;
|
||||||
|
import cn.hutool.json.JSONUtil;
|
||||||
|
import com.electromagnetic.industry.software.backup.pojo.BackupPro;
|
||||||
|
import com.electromagnetic.industry.software.common.pojo.BackupFileResLog;
|
||||||
|
import com.electromagnetic.industry.software.backup.service.FileService;
|
||||||
|
import com.electromagnetic.industry.software.common.resp.ElectromagneticResult;
|
||||||
|
import com.electromagnetic.industry.software.common.util.ElectromagneticResultUtil;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/data/file/backup")
|
||||||
|
@Slf4j
|
||||||
|
public class FileController {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private FileService fileService;
|
||||||
|
@Resource
|
||||||
|
private BackupPro backupPro;
|
||||||
|
|
||||||
|
@RequestMapping("/upload")
|
||||||
|
public ElectromagneticResult<?> upload(@RequestParam("file") MultipartFile file, @RequestParam("path") String path) {
|
||||||
|
BackupFileResLog backupFileResLog = BackupFileResLog.builder().backupStartTime(new Date()).fileName(file.getOriginalFilename()).backupStatus(true).build();
|
||||||
|
try {
|
||||||
|
fileService.upload(file, path);
|
||||||
|
} catch (Exception e) {
|
||||||
|
String details = ExceptionUtil.stacktraceToString(e);
|
||||||
|
backupFileResLog.setBackupStatus(false);
|
||||||
|
backupFileResLog.setFailInfoDetail(details);
|
||||||
|
log.error("备份文件失败,原因--->{}", 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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
package com.electromagnetic.industry.software.backup.pojo;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Component
|
||||||
|
@ConfigurationProperties(prefix = "ele.backup")
|
||||||
|
public class BackupPro {
|
||||||
|
|
||||||
|
private String saveFolder;
|
||||||
|
|
||||||
|
private String logPath;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.electromagnetic.industry.software.backup.service;
|
||||||
|
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public interface FileService {
|
||||||
|
|
||||||
|
void upload(MultipartFile file, String path) throws IOException;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,28 @@
|
||||||
|
package com.electromagnetic.industry.software.backup.serviceimp;
|
||||||
|
|
||||||
|
import cn.hutool.core.io.FileUtil;
|
||||||
|
import com.electromagnetic.industry.software.backup.pojo.BackupPro;
|
||||||
|
import com.electromagnetic.industry.software.backup.service.FileService;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class FileServiceImpl implements FileService {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private BackupPro backupPro;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void upload(MultipartFile file, String path) throws IOException {
|
||||||
|
String saveFolder = backupPro.getSaveFolder();
|
||||||
|
String fileName = file.getOriginalFilename();
|
||||||
|
String destPath = saveFolder + File.separator + path + File.separator + fileName;
|
||||||
|
if (!FileUtil.exist(destPath)) {
|
||||||
|
FileUtil.writeFromStream(file.getInputStream(), destPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
|
||||||
|
ele:
|
||||||
|
backup:
|
||||||
|
saveFolder: "E:/szsd/ele/data/backup/"
|
||||||
|
logPath: "E:/szsd/ele/data/backup.log"
|
||||||
|
|
||||||
|
spring:
|
||||||
|
servlet:
|
||||||
|
multipart:
|
||||||
|
max-file-size: 500MB
|
||||||
|
max-request-size: 500MB
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
package com.electromagnetic.industry.software.manage.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.electromagnetic.industry.software.manage.pojo.models.EleFileBackupLog;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface EleFileBackupLogMapper extends BaseMapper<EleFileBackupLog> {
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,35 @@
|
||||||
|
package com.electromagnetic.industry.software.manage.pojo.models;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@TableName("ele_file_backup_log")
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class EleFileBackupLog {
|
||||||
|
|
||||||
|
public EleFileBackupLog(Date reqStartTime, Date reqEndTime, long fileTime, String id) {
|
||||||
|
this.reqStartTime = reqStartTime;
|
||||||
|
this.reqEndTime = reqEndTime;
|
||||||
|
this.fileTime = fileTime;
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String id;
|
||||||
|
private Date reqStartTime;
|
||||||
|
private Date backupStartTime;
|
||||||
|
private Date backupEndTime;
|
||||||
|
private Date reqEndTime;
|
||||||
|
private Date createTime;
|
||||||
|
|
||||||
|
private String fileName;
|
||||||
|
|
||||||
|
private Boolean backupStatus;
|
||||||
|
|
||||||
|
private String failInfoDetail;
|
||||||
|
|
||||||
|
private long fileTime;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,93 @@
|
||||||
|
package com.electromagnetic.industry.software.manage.tasks;
|
||||||
|
|
||||||
|
import cn.hutool.core.bean.BeanUtil;
|
||||||
|
import cn.hutool.core.io.FileUtil;
|
||||||
|
import cn.hutool.core.text.StrFormatter;
|
||||||
|
import cn.hutool.http.HttpUtil;
|
||||||
|
import cn.hutool.json.JSONUtil;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import com.electromagnetic.industry.software.common.pojo.BackupFileResLog;
|
||||||
|
import com.electromagnetic.industry.software.common.resp.ElectromagneticResult;
|
||||||
|
import com.electromagnetic.industry.software.manage.mapper.EleFileBackupLogMapper;
|
||||||
|
import com.electromagnetic.industry.software.manage.pojo.models.EleFileBackupLog;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
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;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class BackupTask extends ServiceImpl<EleFileBackupLogMapper, EleFileBackupLog> {
|
||||||
|
|
||||||
|
private String host;
|
||||||
|
private int port;
|
||||||
|
|
||||||
|
private String prjDir;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private Environment environment;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
String osName = System.getProperty("os.name").toLowerCase();
|
||||||
|
String tmp = osName.startsWith("win") ? environment.getProperty("data.windows.path") : environment.getProperty("data.linux.path");
|
||||||
|
prjDir = FileUtil.getParent(tmp, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// @Scheduled(cron = "0 0 1 * * ?")
|
||||||
|
public void backup() {
|
||||||
|
long lastFileTime = getLastFileTime();
|
||||||
|
List<File> files = filter(lastFileTime, prjDir);
|
||||||
|
for (File file : files) {
|
||||||
|
String fileCode = getFileCode(file);
|
||||||
|
Date reqStartTime = new Date();
|
||||||
|
BackupFileResLog resLog = backup(file.getAbsolutePath());
|
||||||
|
Date reqEndTime = new Date();
|
||||||
|
EleFileBackupLog resDb = new EleFileBackupLog(reqStartTime, reqEndTime, file.lastModified(), fileCode);
|
||||||
|
BeanUtil.copyProperties(resLog, resDb);
|
||||||
|
this.saveOrUpdate(resDb);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<File> filter(long time, String dir) {
|
||||||
|
FileFilter filter = file -> file.lastModified() > time;
|
||||||
|
return FileUtil.loopFiles(dir, filter);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getFileCode(File file) {
|
||||||
|
String fileName = file.getName();
|
||||||
|
int index = fileName.lastIndexOf(".");
|
||||||
|
return fileName.substring(index + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private long getLastFileTime() {
|
||||||
|
List<EleFileBackupLog> eleFileBackupLogs = this.baseMapper.selectList(Wrappers.lambdaQuery(EleFileBackupLog.class)
|
||||||
|
.select(EleFileBackupLog::getFileTime)
|
||||||
|
.orderByDesc(EleFileBackupLog::getFileTime)
|
||||||
|
.last("limit 1"));
|
||||||
|
if (eleFileBackupLogs.isEmpty()) {
|
||||||
|
return 0L;
|
||||||
|
}
|
||||||
|
return eleFileBackupLogs.get(0).getFileTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
private BackupFileResLog backup(String filePath) {
|
||||||
|
String prjPath = "";
|
||||||
|
Map<String, Object> map = new HashMap<>();
|
||||||
|
map.put("file", new File(filePath));
|
||||||
|
map.put("path", prjPath);
|
||||||
|
String url = StrFormatter.format("http://{}:{}/data/file/backup/upload", host, port);
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
package com.electromagnetic.industry.software.common.pojo;
|
||||||
|
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
public class BackupFileResLog {
|
||||||
|
private Date backupStartTime;
|
||||||
|
private String fileName;
|
||||||
|
private Boolean backupStatus;
|
||||||
|
private Date backupEndTime;
|
||||||
|
private String failInfoDetail;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue