|  |  |  | 
|---|
|  |  |  | package com.doumee.api; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.doumee.core.model.ApiResponse; | 
|---|
|  |  |  | import com.doumee.biz.system.SystemDictDataBiz; | 
|---|
|  |  |  | 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 com.doumee.service.business.PlatformJobService; | 
|---|
|  |  |  | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.RequestMapping; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.RestController; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.io.BufferedReader; | 
|---|
|  |  |  | import java.io.File; | 
|---|
|  |  |  | import java.util.ArrayList; | 
|---|
|  |  |  | import java.util.Collections; | 
|---|
|  |  |  | import java.util.Comparator; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.io.IOException; | 
|---|
|  |  |  | import java.io.InputStreamReader; | 
|---|
|  |  |  | import java.sql.Connection; | 
|---|
|  |  |  | import java.sql.DriverManager; | 
|---|
|  |  |  | import java.sql.Statement; | 
|---|
|  |  |  | import java.text.SimpleDateFormat; | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @author 江蹄蹄 | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private PlatformJobService platformJobService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private SystemDictDataBiz systemDictDataBiz; | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @ApiOperation("数据库备份,保留最近7个备份sql") | 
|---|
|  |  |  | 
|---|
|  |  |  | public ApiResponse backupDatabase() { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | String timestamp = DateUtil.getNowLongTime(); | 
|---|
|  |  |  | String path = "/usr/local/jars/db/"; | 
|---|
|  |  |  | //            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"; | 
|---|
|  |  |  | // 使用mysqldump命令进行数据库备份 | 
|---|
|  |  |  | Process process = Runtime.getRuntime().exec(new String[]{"mysqldump ", "-u", "root", "-p","Atwl@2024", "antaiwuliu", "-r", backupPath}); | 
|---|
|  |  |  | process.waitFor(); | 
|---|
|  |  |  | 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 | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /*      // 使用mysqldump命令进行数据库备份 | 
|---|
|  |  |  | Process process = Runtime.getRuntime().exec(code); | 
|---|
|  |  |  | process.getErrorStream(); | 
|---|
|  |  |  | process.getInputStream(); | 
|---|
|  |  |  | process.waitFor();*/ | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 检查备份是否成功 | 
|---|
|  |  |  | if (new File(backupPath).exists()) { | 
|---|
|  |  |  | 
|---|
|  |  |  | deleteOldFiles(path,7);//保留最近7个文件 | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | log.error("数据库备份失败."+e.getMessage()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return ApiResponse.failed("数据库备份失败" ); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | public void run() { | 
|---|
|  |  |  | SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); | 
|---|
|  |  |  | String currentDateTime = dateFormat.format(new Date()); | 
|---|
|  |  |  | String fileName =  "backup_" + currentDateTime + ".sql"; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | Class.forName("com.mysql.cj.jdbc.Driver"); | 
|---|
|  |  |  | Connection connection = DriverManager.getConnection("localhost:3306/antaiwuliu", "root", "Atwl@2024"); | 
|---|
|  |  |  | Statement statement = connection.createStatement(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | String query = "SELECT * INTO OUTFILE '" + fileName + "' FROM your_table"; | 
|---|
|  |  |  | statement.execute(query); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | System.out.println("Database backup successful to " + fileName); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | statement.close(); | 
|---|
|  |  |  | connection.close(); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static void main(String[] args) { | 
|---|
|  |  |  | new DatabaseController().run();; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | static class ProcessHandleRunnable implements Runnable { | 
|---|
|  |  |  | private Process process; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public ProcessHandleRunnable(Process process) { | 
|---|
|  |  |  | this.process = process; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public void run() { | 
|---|
|  |  |  | BufferedReader br = null; | 
|---|
|  |  |  | InputStreamReader reader = null; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | System.out.println("start run..."); | 
|---|
|  |  |  | reader = new InputStreamReader(process.getInputStream()); | 
|---|
|  |  |  | br = new BufferedReader(reader); | 
|---|
|  |  |  | String line = null; | 
|---|
|  |  |  | while ((line = br.readLine()) != null) { | 
|---|
|  |  |  | System.out.println(line); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | System.out.println("stop run..."); | 
|---|
|  |  |  | } catch (IOException ex) { | 
|---|
|  |  |  | ex.printStackTrace(); | 
|---|
|  |  |  | } finally { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | if (br != null) | 
|---|
|  |  |  | br.close(); | 
|---|
|  |  |  | if (reader != null) | 
|---|
|  |  |  | reader.close(); | 
|---|
|  |  |  | } catch (IOException e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @param path | 
|---|
|  |  |  | */ | 
|---|