| | |
| | | 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.web.bind.annotation.PostMapping; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | |
| | |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.io.InputStreamReader; |
| | | import java.util.ArrayList; |
| | | import java.util.Collections; |
| | | import java.util.Comparator; |
| | | import java.util.List; |
| | | import java.sql.Connection; |
| | | import java.sql.DriverManager; |
| | | import java.sql.Statement; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * @author 江蹄蹄 |
| | |
| | | |
| | | |
| | | @ApiOperation("数据库备份,保留最近7个备份sql") |
| | | @GetMapping("/backupDatabase") |
| | | @PostMapping("/backupDatabase") |
| | | public ApiResponse backupDatabase() { |
| | | try { |
| | | String timestamp = DateUtil.getNowLongTime(); |
| | |
| | | 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; |