| | |
| | | package com.doumee.api; |
| | | |
| | | import com.doumee.biz.system.SystemDictDataBiz; |
| | | import com.doumee.core.model.ApiResponse; |
| | | import com.doumee.service.business.third.model.ApiResponse; |
| | | import com.doumee.core.utils.Constants; |
| | | import com.doumee.core.utils.Date; |
| | | import com.doumee.core.utils.DateUtil; |
| | |
| | | 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.beans.factory.annotation.Qualifier; |
| | | import org.springframework.web.bind.annotation.PostMapping; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | |
| | |
| | | import java.sql.Statement; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | import java.util.concurrent.Executor; |
| | | |
| | | /** |
| | | * @author 江蹄蹄 |
| | |
| | | public class DatabaseController extends BaseController { |
| | | |
| | | @Autowired |
| | | @Qualifier("asyncExecutor") |
| | | private Executor asyncExecutor; |
| | | @Autowired |
| | | private PlatformJobService platformJobService; |
| | | @Autowired |
| | | private SystemDictDataBiz systemDictDataBiz; |
| | | |
| | | |
| | | @ApiOperation("数据库备份,保留最近7个备份sql") |
| | | @GetMapping("/backupDatabase") |
| | | @PostMapping("/backupDatabase") |
| | | public ApiResponse backupDatabase() { |
| | | try { |
| | | String timestamp = DateUtil.getNowLongTime(); |
| | | // String path = "/usr/local/jars/db/"; |
| | | // String backupPath = path + timestamp + ".sql"; |
| | | String path = systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.MYSQL_BACKUP_DIR).getCode(); |
| | | String code = systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.MYSQL_BACKUP_CDOE).getCode() ; |
| | | String backupPath = path + timestamp + ".sql"; |
| | | code= code.replace("${param}",backupPath); |
| | | log.error("数据库备份================: " + code); |
| | | ProcessBuilder builder = new ProcessBuilder(code); |
| | | // 重定向错误流到标准输出流 |
| | | builder.redirectErrorStream(true); |
| | | // stdout |
| | | Process process = builder.start(); |
| | | new Thread(new ProcessHandleRunnable(process)).start(); |
| | | process.waitFor(); // wait if needed |
| | | log.info("定时任务执行开始:backupDatabase-数据库备份"); |
| | | asyncExecutor.execute(() -> { |
| | | try { |
| | | String timestamp = DateUtil.getNowLongTime(); |
| | | String path = systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.MYSQL_BACKUP_DIR).getCode(); |
| | | String code = systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.MYSQL_BACKUP_CDOE).getCode() ; |
| | | String backupPath = path + timestamp + ".sql"; |
| | | code= code.replace("${param}",backupPath); |
| | | log.info("数据库备份命令: {}", code); |
| | | ProcessBuilder builder = new ProcessBuilder(code); |
| | | builder.redirectErrorStream(true); |
| | | Process process = builder.start(); |
| | | new Thread(new ProcessHandleRunnable(process)).start(); |
| | | process.waitFor(); |
| | | |
| | | /* // 使用mysqldump命令进行数据库备份 |
| | | Process process = Runtime.getRuntime().exec(code); |
| | | process.getErrorStream(); |
| | | process.getInputStream(); |
| | | process.waitFor();*/ |
| | | |
| | | // 检查备份是否成功 |
| | | if (new File(backupPath).exists()) { |
| | | log.info("数据库备份成功: " + backupPath); |
| | | return ApiResponse.success("数据库备份成功: " + backupPath); |
| | | } else { |
| | | log.error("数据库备份失败."); |
| | | if (new File(backupPath).exists()) { |
| | | log.info("数据库备份成功: {}", backupPath); |
| | | } else { |
| | | log.error("数据库备份失败."); |
| | | } |
| | | deleteOldFiles(path,7); |
| | | } catch (Exception e) { |
| | | log.error("数据库备份失败", e); |
| | | } |
| | | deleteOldFiles(path,7);//保留最近7个文件 |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | |
| | | log.error("数据库备份失败."+e.getMessage()); |
| | | } |
| | | return ApiResponse.failed("数据库备份失败" ); |
| | | }); |
| | | return ApiResponse.success("数据库备份已提交"); |
| | | } |
| | | public void run() { |
| | | SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); |