springboot 数据库还原
springboot 数据库还原数据库实体类public class MysqlBackups implements Serializable {private static final long serialVersionUID = 1L;private Long id;/*** mysql ip端口*/private String mysqlIp;/*** 数据库端口号*/priva
·
springboot 数据库还原
- 数据库实体类
public class MysqlBackups implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
/**
* mysql ip端口
*/
private String mysqlIp;
/**
* 数据库端口号
*/
private String mysqlPort;
/**
* 备份命令
*/
private String mysqlCmd;
/**
* 恢复命令
*/
private String mysqlBackCmd;
/**
* 数据库名称
*/
private String databaseName;
/**
* 备份文件名称
*/
private String backupsName;
/**
* 备份路径
*/
private String backupsPath;
/**
* 操作次数
*/
private Integer operation;
/**
* 备份时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
private Date backupTime;
/**
* 恢复时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
private Date recoveryTime;
/**
* 备注
*/
private String remark;
}
- 实现类
public interface IMysqlBackupsService extends IService<MysqlBackups> {
/**
* 恢复数据库
* @param backups
* @param userName
* @param password
* @return
*/
R rollback(MysqlBackups backups, String userName, String password);
}
@Service
public class MysqlBackupsServiceImpl extends ServiceImpl<MysqlBackupsMapper, MysqlBackups> implements IMysqlBackupsService {
/**
* 恢复数据库
* @param backups 备份id
* @param userName
* @param password
* @return
*/
@Transactional
@Override
public R rollback(MysqlBackups backups, String userName, String password) {
// 备份路径和文件名
StringBuilder realFilePath = new StringBuilder().append(backups.getBackupsPath()).append(backups.getBackupsName());
if (!new File(String.valueOf(realFilePath)).exists()) {
return R.error("文件不存在,恢复失败,请查看目录内文件是否存在后重新尝试!");
}
StringBuilder cmd = new StringBuilder()
.append("mysql -h")
.append(backups.getMysqlIp())
.append(" -u")
.append(userName)
.append(" -p")
.append(password)
.append(" ")
.append(backups.getDatabaseName())
.append(" < ")
.append(realFilePath);
String[] command = new String[0];
// 获取操作系统名称
String osName = System.getProperty("os.name").toLowerCase();
if (Constant.isSystem(osName)) {
// Windows
command = new String[]{"cmd", "/c", String.valueOf(cmd)};
} else {
// Linux
command = new String[]{"/bin/sh", "-c", String.valueOf(cmd)};
}
// 恢复指令写入到数据库
backups.setMysqlBackCmd(String.valueOf(cmd));
// 更新操作次数
backups.setRecoveryTime(new Date());
backups.setOperation(backups.getOperation() + 1);
try {
// 获取Runtime实例
Runtime.getRuntime().exec(command);
this.updateById(backups);
return R.ok("Mysql 数据库恢复成功,恢复文件名:{}"+ realFilePath);
} catch (Exception e) {
e.printStackTrace();
return R.error("网络异常,恢复失败,请稍后重新尝试!");
}
}
}
- 控制类
/**
* 数据库还原
* @param backups
* @return
*/
@PostMapping("/rollback")
public R rollback(@RequestBody MysqlBackups backups) {
if (backups.getId() == null) {
return R.error("id不能为null,请重新尝试!");
}
// 数据库用户名
String userName = this.userName;
// 数据库密码
String password = this.password;
// 根据id查询查询已有的信息
MysqlBackups mb = mysqlBackupsService.getById(backups.getId());
if (mb == null) {
return R.error("还原数据错误");
}
R r = mysqlBackupsService.rollback(mb, userName, password);
return r;
}
更多推荐
所有评论(0)