当前位置:网站首页>Project training 11 - regular backup of database
Project training 11 - regular backup of database
2022-06-10 23:14:00 【ScarlettWX】
In the last blog, I talked about how to back up a specific database , This blog will discuss how to backup some databases regularly .
The method used for scheduled backup is the same as before , Use Runtime.getRuntime().exec(command) Execute command line statements , Backup sql The file is stored in a folder in the specified path . call DBService That is, the database service object can obtain the user database stored in the system database , Store it in List in . loop List.size() frequency , according to List Of index Back up one database at a time . The problem now is how to perform tasks regularly . The implementation method is to start a thread and then back up a database . Use cron Period of storage timing , With "0 30 9 * * ?" For example , first place 0 Express 0 second , Second 30 Express 30 minute , Third 9 Morning 9 when , The rest of the bits are replaced by other symbols to represent the... Of each day 9:30. Here is the timing code :
@Configuration // Tag configuration class , Both Component The effect of
@EnableScheduling // Turn on timed tasks
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 -> {
// Execution cycle every day 9 spot 30 branch
String cron = "0 30 9 * * ?";
// Non empty verification
if(StringUtils.isEmpty(cron)){
//TODO
}
// Return to execution cycle
return new CronTrigger(cron).nextExecutionTime(triggerContext);
}
);
}
}
Console run results :
Display in folder :
Before sql The file naming format is database name + The current date +.sql, If the backup cycle is very frequent , Multiple backups in a day , It will cause the files backed up later to overwrite the previous files , Will affect the data . So I changed the naming format , Now the database name + The current date + current time +.sql. The naming code is as follows :
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(":","-");
// Backup file name
String fileName = dbName+datetime2+ ".sql";
Note that the file name cannot contain spaces or colons , To replace... With other characters , Otherwise, no files will be generated .
边栏推荐
- 中金财富证券证券股票开户安全吗?靠谱吗?
- Sdn/nfv application in cloud data center
- Vulnhub practice DC-1 target
- Operation of simulated examination platform for welder (primary) test questions in 2022
- 项目实训10——对特定数据库的备份
- 乘风破浪,探索数据可视化开发平台 FlyFish 开源背后的秘密!
- IP反查域名
- 我们对产业互联网的认识,还是困囿于互联网式的平台和中心的逻辑之中
- 中银证券股票开户安全吗?是正规的吗?
- Multus CNI deployment and basic use of kubernetes multi network card scheme
猜你喜欢

Why is the video fusion cloud service easycvr cluster video event query invalid?

数据与信息资源共享平台(六)

DC4 of vulnhub

简单阻抗匹配电路及公式

Online questionnaire system based on php+web+mysql

"Draw the bow as strong, use the arrow as long", Manfu technology opens a new track for the data service industry

Distributed Foundation

完美解码PureCodec 20220601

Icml2022 | revoir la traduction vocale de bout en bout du texte à partir de zéro

Ride the storm and explore the secret behind the open source of flyfish, a data visualization development platform!
随机推荐
线程池——治理线程的法宝
Custom view: graphics and image processing (I): using simple pictures
06 15 10 20 神秘代码,等你破译
Vulnhub's DC3
Laravel8 enables alicloud file upload
数组、List、Set、Map、Properties依赖注入格式
数学与素质教育
Face recognition software based on deepface model
Kubernetes binary installation (v1.20.15) (VI) deploying worknode nodes
数据与信息资源共享平台(七)
优化代码去除if-else
unity 代码为动画注册事件
js敏感信息泄露检测工具
28岁自学编程会不会太晚了?靠谱吗?
期货开户有什么限制嘛?哪里最安全?
What are the restrictions on opening futures accounts? Where is the safest?
Flowable BPMN相关知识
Native support for the first version of arm64! Microsoft win11/10 free tool set PowerToys 0.59 release
中银证券开户有什么风险吗?安全的吗?
[Interface tutorial] how does easycvr set platform cascading through the interface?