perf:数据库备份
This commit is contained in:
@@ -5,7 +5,6 @@ import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.util.RuntimeUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import cn.hutool.log.LogFactory;
|
||||
import com.tiesheng.database.utls.TieshengDbUtil;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -29,6 +28,11 @@ public class DbBackupConfig {
|
||||
*/
|
||||
private String format = "yyyyMMdd";
|
||||
|
||||
/**
|
||||
* 过期天数
|
||||
*/
|
||||
private Integer days = 7;
|
||||
|
||||
|
||||
/**
|
||||
* 数据库备份
|
||||
@@ -38,16 +42,20 @@ public class DbBackupConfig {
|
||||
String username = SpringUtil.getProperty("spring.datasource.username");
|
||||
String password = SpringUtil.getProperty("spring.datasource.password");
|
||||
String dbName = TieshengDbUtil.getTableSchema(url);
|
||||
|
||||
String backupDir = StrUtil.format("{}{}", getPath(), dbName);
|
||||
try {
|
||||
String saveFile = StrUtil.format("{}{}/{}.sql", getPath(), dbName, DateUtil.format(new Date(), format));
|
||||
String saveFile = StrUtil.format("{}/{}.sql", backupDir, DateUtil.format(new Date(), format));
|
||||
FileUtil.mkParentDirs(saveFile);
|
||||
String cmd = StrUtil.format("mysqldump -u {} --password={} --databases {} " +
|
||||
"--compress --skip-opt --result-file {} ", username, password, dbName, saveFile);
|
||||
RuntimeUtil.execForStr(cmd);
|
||||
RuntimeUtil.execForStr(StrUtil.format("gzip -9 {}", saveFile));
|
||||
} catch (Exception e) {
|
||||
LogFactory.get().info(e);
|
||||
|
||||
// 删除过期备份
|
||||
RuntimeUtil.execForStr("find " + backupDir + " -mtime +" + days,
|
||||
" xargs rm -rf");
|
||||
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -71,4 +79,12 @@ public class DbBackupConfig {
|
||||
public void setFormat(String format) {
|
||||
this.format = format;
|
||||
}
|
||||
|
||||
public Integer getDays() {
|
||||
return days;
|
||||
}
|
||||
|
||||
public void setDays(Integer days) {
|
||||
this.days = days;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user