perf:启用定时任务,每日凌晨1点执行数据备份
This commit is contained in:
@@ -9,6 +9,9 @@ import cn.hutool.log.LogFactory;
|
|||||||
import com.tiesheng.database.utls.TieshengDbUtil;
|
import com.tiesheng.database.utls.TieshengDbUtil;
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
import org.springframework.context.annotation.Configuration;
|
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.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -18,7 +21,8 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConfigurationProperties(prefix = "tiesheng.db-backup")
|
@ConfigurationProperties(prefix = "tiesheng.db-backup")
|
||||||
public class DbBackupConfig {
|
@EnableScheduling
|
||||||
|
public class DbBackupConfig implements SchedulingConfigurer {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 数据库备份的路径
|
* 数据库备份的路径
|
||||||
@@ -35,6 +39,11 @@ public class DbBackupConfig {
|
|||||||
*/
|
*/
|
||||||
private Integer days = 7;
|
private Integer days = 7;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
|
||||||
|
taskRegistrar.addCronTask(this::dbBackup, "0 1 * * *");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 数据库备份
|
* 数据库备份
|
||||||
@@ -54,13 +63,12 @@ public class DbBackupConfig {
|
|||||||
RuntimeUtil.execForStr(StrUtil.format("gzip -9f {}", saveFile));
|
RuntimeUtil.execForStr(StrUtil.format("gzip -9f {}", saveFile));
|
||||||
|
|
||||||
// 删除过期备份
|
// 删除过期备份
|
||||||
List<String> forStr = RuntimeUtil.execForLines("find " + backupDir + " -name *.sql.gz -mtime -" + days);
|
List<String> forStr = RuntimeUtil.execForLines("find " + backupDir + " -name *.sql.gz -mtime +" + days);
|
||||||
forStr.forEach(FileUtil::del);
|
forStr.forEach(FileUtil::del);
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LogFactory.get().info(e);
|
LogFactory.get().info(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////
|
||||||
@@ -90,4 +98,6 @@ public class DbBackupConfig {
|
|||||||
public void setDays(Integer days) {
|
public void setDays(Integer days) {
|
||||||
this.days = days;
|
this.days = days;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user