当前位置:网站首页>项目实训11——对数据库的定时备份
项目实训11——对数据库的定时备份
2022-06-10 22:01:00 【ScarlettWX】
上一篇博客中讲到如何对特定数据库进行备份,本篇博客来讨论如何对一些数据库进行定时备份。
定时备份使用的方法和之前的方法一样,使用Runtime.getRuntime().exec(command)执行命令行语句,备份的sql文件存储在指定的路径的文件夹内。调用DBService即数据库服务对象可获得存储在系统数据库内的用户数据库,将其存储在List中。循环List.size()次数,根据List的index每次备份一个数据库。现在的问题是如何定时执行任务。实现方法为先开启一个线程再对一个数据库进行备份。使用cron存储定时的周期,以"0 30 9 * * ?"为例,第一位0表示0秒,第二位30表示30分钟,第三位9表示上午9时,其余位用其他符号代替表示每天的9:30。下面是定时的代码:
@Configuration //标记配置类,兼备Component的效果
@EnableScheduling //开启定时任务
public class SimpleScheduleConfig implements SchedulingConfigurer{
@Autowired
private DBService dbService;
@SneakyThrows
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
List<DB> dbList=dbService.findDBList();
Thread thread = new Thread(){
@SneakyThrows
@Override
public void run() {
super.run();
int dbid;
for(int i=0; i<dbList.size(); i++){
dbid=dbList.get(i).getId();
dbService.backupDB(dbid);
}
}
};
taskRegistrar.addTriggerTask(
thread,
triggerContext -> {
//执行周期每天9点30分
String cron = "0 30 9 * * ?";
//非空校验
if(StringUtils.isEmpty(cron)){
//TODO
}
//返回执行周期
return new CronTrigger(cron).nextExecutionTime(triggerContext);
}
);
}
}
控制台运行结果:
文件夹中的显示:
之前sql文件的命名格式为数据库名+当前日期+.sql,如果备份周期非常频繁,一天之内多次备份,则会造成后备份的文件覆盖之前的文件,会影响数据。所以我改变了命名格式,现在为数据库名+当前日期+当前时间+.sql。命名代码如下:
Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dt=df.format(date);
String datetime1=dt.replace(" ","_");
String datetime2=datetime1.replace(":","-");
// 备份文件名称
String fileName = dbName+datetime2+ ".sql";
注意文件名中不能有空格和冒号,要使用其他字符替代,否则不会有文件生成。
边栏推荐
- 关于idea中src下 无法new一个package
- Sherri Monroe was appointed as the new executive director of the additive manufacturers green trade association
- 【接口教程】EasyCVR如何通过接口设置平台级联?
- How to run Plink software -- three methods
- 【QPSK中频】基于FPGA的QPSK中频信号产生模块verilog设计
- Digital twin: third person mouse operation
- Storage engine analysis
- LuoYongHao: if I were the person in charge, I could make apple products go up more than three steps
- 关于高考的那些事儿
- 原生支持 ARM64 的首个版本!微软 Win11/10 免费工具集 PowerToys 0.59 发布
猜你喜欢

2022 Shanghai safety officer C certificate operation certificate examination question bank simulated examination platform operation

Vulnhub practice DC-1 target
![[tcapulusdb knowledge base] Introduction to tcapulusdb engine parameter adjustment](/img/74/6ce32e007c064c9255269fe38761a4.png)
[tcapulusdb knowledge base] Introduction to tcapulusdb engine parameter adjustment

Redis from entry to entry
![Authoritative guide to Web3 technology stack [2022]](/img/76/0f64604f5e5355300f5ec498ea23e1.png)
Authoritative guide to Web3 technology stack [2022]

Fallback operation in SVN

Opencv_100问_第二章 (6-10)
![[tcapulusdb knowledge base] tcapulusdb tcapdb capacity expansion and contraction introduction](/img/5c/b24a94cb2d46fe571c172d232503d5.png)
[tcapulusdb knowledge base] tcapulusdb tcapdb capacity expansion and contraction introduction

Opencv_ 100 questions_ Chapter II (6-10)

Can Huawei matepad become the secondary screen of your laptop?
随机推荐
C language internal skill cultivation [integer stored in memory]
IPO can't cure Weima's complications?
通达信股票开户安全吗?如何办理开户呢?
DC2 of vulnhub
Reading notes on the way of Huawei data
smb匿名
0223-总结
【GMM+KDE】基于MATLAB的GMM和KDE核估计得目标跟踪仿真
[original] analysis of nine price HPV data capture of Yilu app
数组、List、Set、Map、Properties依赖注入格式
vulnhub之dc4
2022 t elevator repair test question simulation test question bank and online simulation test
同花顺开户怎么样?开户安全吗?
JS sensitive information leak detection tool
Blue Bridge Cup_ Pick substring_ Combinatorial mathematics_ Multiplication principle/ Ruler method
Open source project PM how to design official website
优化代码去除if-else
Several reasons and solutions of virtual machine Ping failure
MA8601 pin√pin替代汤铭FE1.1s无须更改电路板|完美替代FE1.1s方案
LuoYongHao: if I were the person in charge, I could make apple products go up more than three steps