parent
							
								
									c81902e905
								
							
						
					
					
						commit
						b5f6a8d5ba
					
				@ -0,0 +1,63 @@
 | 
				
			|||||||
 | 
					package com.ruoyi.web.controller.monitor;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.ruoyi.common.config.RuoYiConfig;
 | 
				
			||||||
 | 
					import com.ruoyi.common.core.controller.BaseController;
 | 
				
			||||||
 | 
					import com.ruoyi.common.core.domain.AjaxResult;
 | 
				
			||||||
 | 
					import org.apache.shiro.authz.annotation.RequiresPermissions;
 | 
				
			||||||
 | 
					import org.springframework.stereotype.Controller;
 | 
				
			||||||
 | 
					import org.springframework.web.bind.annotation.GetMapping;
 | 
				
			||||||
 | 
					import org.springframework.web.bind.annotation.RequestMapping;
 | 
				
			||||||
 | 
					import org.springframework.web.bind.annotation.RequestParam;
 | 
				
			||||||
 | 
					import org.springframework.web.bind.annotation.ResponseBody;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.io.File;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * packageName com.ruoyi.web.controller.monitor
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @author wangxy
 | 
				
			||||||
 | 
					 * @version JDK 8
 | 
				
			||||||
 | 
					 * @className BackupController
 | 
				
			||||||
 | 
					 * @date 2024/9/13
 | 
				
			||||||
 | 
					 * @description 数据库备份
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					@Controller
 | 
				
			||||||
 | 
					@RequestMapping("/monitor/backup")
 | 
				
			||||||
 | 
					public class BackupController  extends BaseController {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private String prefix = "/monitor/backup";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @RequiresPermissions("monitor:backup:view")
 | 
				
			||||||
 | 
					    @GetMapping()
 | 
				
			||||||
 | 
					    public String backData() {
 | 
				
			||||||
 | 
					        return  prefix + "/backup";
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @GetMapping("backups")
 | 
				
			||||||
 | 
					    @ResponseBody
 | 
				
			||||||
 | 
					    public AjaxResult backups(@RequestParam String username,
 | 
				
			||||||
 | 
					                              @RequestParam String password,
 | 
				
			||||||
 | 
					                              @RequestParam String hostname,
 | 
				
			||||||
 | 
					                              @RequestParam String port,
 | 
				
			||||||
 | 
					                              @RequestParam String databaseName) {
 | 
				
			||||||
 | 
					        String sqlFileName = RuoYiConfig.getDataBaseBackUp() + ".sql";
 | 
				
			||||||
 | 
					        File file = new File(File.separator + sqlFileName);
 | 
				
			||||||
 | 
					        //目录生成
 | 
				
			||||||
 | 
					        if (!file.getParentFile().exists()) {
 | 
				
			||||||
 | 
					            file.getParentFile().mkdirs();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
 | 
					            ProcessBuilder processBuilder = new ProcessBuilder("pg_dump", "-Fp", "-f", sqlFileName,
 | 
				
			||||||
 | 
					                    "\"host=" + hostname, "port=" + port, "user=" + username, "password=" + password, "dbname=" + databaseName + "\"");
 | 
				
			||||||
 | 
					            Process process = processBuilder.start();
 | 
				
			||||||
 | 
					            if (process.waitFor() == 0) {
 | 
				
			||||||
 | 
					                return AjaxResult.success("备份成功!");
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                return AjaxResult.error("备份失败!");
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        } catch (Exception e) {
 | 
				
			||||||
 | 
					            e.printStackTrace();
 | 
				
			||||||
 | 
					            return AjaxResult.error("备份失败!");
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
					Loading…
					
					
				
		Reference in new issue