108 lines
3.1 KiB
Java
108 lines
3.1 KiB
Java
package com.tiesheng.database.config;
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
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;
|
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
|
import org.springframework.scheduling.annotation.SchedulingConfigurer;
|
|
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
|
|
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* @author hao
|
|
*/
|
|
@Configuration
|
|
@ConfigurationProperties(prefix = "tiesheng.db.backup")
|
|
@EnableScheduling
|
|
public class DbBackupConfig implements SchedulingConfigurer {
|
|
|
|
/**
|
|
* 数据库备份的路径
|
|
*/
|
|
private String path = "/root/backup/";
|
|
|
|
/**
|
|
* 备份文件的时间格式
|
|
*/
|
|
private String format = "yyyyMMdd";
|
|
|
|
/**
|
|
* 过期天数
|
|
*/
|
|
private Integer days = 7;
|
|
|
|
@Override
|
|
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
|
|
taskRegistrar.addCronTask(this::dbBackup, "0 0 1 * * ?");
|
|
}
|
|
|
|
|
|
/**
|
|
* 数据库备份
|
|
*/
|
|
public void dbBackup() {
|
|
if (!FileUtil.exist(path)) {
|
|
return;
|
|
}
|
|
|
|
String url = SpringUtil.getProperty("spring.datasource.url");
|
|
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", 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 -9f {}", saveFile));
|
|
|
|
// 删除过期备份
|
|
List<String> forStr = RuntimeUtil.execForLines("find " + backupDir + " -name *.sql.gz -mtime +" + days);
|
|
forStr.forEach(FileUtil::del);
|
|
|
|
} catch (Exception e) {
|
|
LogFactory.get().info(e);
|
|
}
|
|
}
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
// setter\getter
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
public String getPath() {
|
|
return path;
|
|
}
|
|
|
|
public void setPath(String path) {
|
|
this.path = path;
|
|
}
|
|
|
|
public String getFormat() {
|
|
return format;
|
|
}
|
|
|
|
public void setFormat(String format) {
|
|
this.format = format;
|
|
}
|
|
|
|
public Integer getDays() {
|
|
return days;
|
|
}
|
|
|
|
public void setDays(Integer days) {
|
|
this.days = days;
|
|
}
|
|
|
|
|
|
}
|