当前位置:网站首页>项目实训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";
注意文件名中不能有空格和冒号,要使用其他字符替代,否则不会有文件生成。
边栏推荐
- Simulated 100 questions and simulated examination of G2 utility boiler stoker examination in 2022
- 06151020 mysterious code, waiting for you to decipher
- 盲盒商城APP系统开发的软件特点和盲盒功能介绍
- Opencv_ 100 questions_ Chapter III (11-15)
- CCF CSP 202109-2 non zero segment division [100 points]
- 200 c language words, please collect!
- Face recognition software based on deepface model
- 数据与信息资源共享平台(七)
- A journey of database full SQL analysis and audit system performance optimization
- Sdn/nfv application in cloud data center
猜你喜欢

Reading notes on the way of Huawei data

vulnhub之dc4
![[tcapulusdb knowledge base] tcapulusdb tcapdb capacity expansion and contraction introduction](/img/5c/b24a94cb2d46fe571c172d232503d5.png)
[tcapulusdb knowledge base] tcapulusdb tcapdb capacity expansion and contraction introduction

Fallback operation in SVN

Play electronics, poor three generations

ICML2022 | 从零开始重新审视端到端的语音到文本翻译

完美解码PureCodec 20220601
![Authoritative guide to Web3 technology stack [2022]](/img/76/0f64604f5e5355300f5ec498ea23e1.png)
Authoritative guide to Web3 technology stack [2022]

MA8601 pin√pin替代汤铭FE1.1s无须更改电路板|完美替代FE1.1s方案

【006】初识字符串
随机推荐
Web3技术栈权威指南【2022】
MA8601 pin√pin替代汤铭FE1.1s无须更改电路板|完美替代FE1.1s方案
SMB anonymous
Watlow签订从施耐德电气手中收购Eurotherm的协议
The definition of the metauniverse and the seven infinite features
Operation of simulated examination platform for welder (primary) test questions in 2022
"Draw the bow as strong, use the arrow as long", Manfu technology opens a new track for the data service industry
图像拼接摄像头拼接笔记
Interpretation of dataset class of mmdetection
数据与信息资源共享平台(五)
但身示你五县非那最土zaiFKMW
CCF CSP 202109-4 collect cards
爬虫学习知识
Display of successful cases of target customer matching data table
Redis数据结构
Kubernetes binary installation (v1.20.15) (VI) deploying worknode nodes
Vscode common shortcuts
Auto.js pro 开发环境配置
ICML2022 | 从零开始重新审视端到端的语音到文本翻译
Sherri Monroe was appointed as the new executive director of the additive manufacturers green trade association