package com.doumee.api;
|
|
import com.doumee.core.model.ApiResponse;
|
import com.doumee.core.utils.Date;
|
import com.doumee.core.utils.DateUtil;
|
import com.doumee.service.business.PlatformJobService;
|
import io.swagger.annotations.Api;
|
import io.swagger.annotations.ApiOperation;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RestController;
|
|
import java.io.File;
|
import java.util.ArrayList;
|
import java.util.Collections;
|
import java.util.Comparator;
|
import java.util.List;
|
|
/**
|
* @author 江蹄蹄
|
* @date 2023/11/30 15:33
|
*/
|
@Api(tags = "月台作业定时")
|
@RestController
|
@Slf4j
|
@RequestMapping("/timer/db")
|
public class DatabaseController extends BaseController {
|
|
@Autowired
|
private PlatformJobService platformJobService;
|
|
|
@ApiOperation("数据库备份,保留最近7个备份sql")
|
@GetMapping("/backupDatabase")
|
public ApiResponse backupDatabase() {
|
try {
|
String timestamp = DateUtil.getNowLongTime();
|
String path = "/usr/local/jars/db/";
|
String backupPath = path + timestamp + ".sql";
|
// 使用mysqldump命令进行数据库备份
|
Process process = Runtime.getRuntime().exec(new String[]{"mysqldump ", "-u", "root", "-p","Atwl@2024", "antaiwuliu", "-r", backupPath});
|
process.waitFor();
|
|
// 检查备份是否成功
|
if (new File(backupPath).exists()) {
|
log.info("数据库备份成功: " + backupPath);
|
return ApiResponse.success("数据库备份成功: " + backupPath);
|
} else {
|
log.error("数据库备份失败.");
|
}
|
deleteOldFiles(path,7);//保留最近7个文件
|
} catch (Exception e) {
|
e.printStackTrace();
|
log.error("数据库备份失败."+e.getMessage());
|
}
|
return ApiResponse.failed("数据库备份失败" );
|
}
|
|
/**
|
* @param path
|
*/
|
public void deleteOldFiles(String path,int num) {
|
//文件路径
|
List<File> list = getFileSort(path);
|
if(list==null || list.size() <= num){
|
return;
|
}
|
for (int i = 0; i <list.size(); i++) {
|
if(i>=7){
|
list.get(i).delete();
|
}
|
}
|
}
|
|
/**
|
* 获取目录下所有文件(按时间排序)
|
*
|
* @param path
|
* @return
|
*/
|
public static List<File> getFileSort(String path) {
|
|
List<File> list = getFiles(path, new ArrayList<>());
|
|
if (list != null && list.size() > 0) {
|
|
Collections.sort(list, new Comparator<File>() {
|
public int compare(File file, File newFile) {
|
if (file.lastModified() < newFile.lastModified()) {
|
return 1;
|
} else if (file.lastModified() == newFile.lastModified()) {
|
return 0;
|
} else {
|
return -1;
|
}
|
|
}
|
});
|
|
}
|
|
return list;
|
}
|
|
/**
|
*
|
* 获取目录下所有文件
|
*
|
* @param realpath
|
* @param files
|
* @return
|
*/
|
public static List<File> getFiles(String realpath, List<File> files) {
|
File realFile = new File(realpath);
|
if (realFile.isDirectory()) {
|
File[] subfiles = realFile.listFiles();
|
for (File file : subfiles) {
|
if (file.isDirectory()) {
|
getFiles(file.getAbsolutePath(), files);
|
} else {
|
files.add(file);
|
}
|
}
|
}
|
return files;
|
}
|
|
}
|