当前位置:网站首页>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 .
边栏推荐
猜你喜欢
![[Interface tutorial] how does easycvr set platform cascading through the interface?](/img/d5/b7cbe9522e91a7ae29558bb2abe1fa.jpg)
[Interface tutorial] how does easycvr set platform cascading through the interface?

Sealem Finance - a new decentralized financial platform based on Web3

PwnTheBox,Web:hello

Software features and functions of the blind box mall app system development

200 c language words, please collect!

Vscode common plug-ins and configurations

分布式基础
![Authoritative guide to Web3 technology stack [2022]](/img/76/0f64604f5e5355300f5ec498ea23e1.png)
Authoritative guide to Web3 technology stack [2022]

集度夏一平:不是所有事都向李彦宏汇报,靠产品跟小米华为竞争

Lenovo explained for the first time the five advantages of hybrid cloud Lenovo xcloud and how to build an intelligent digital base
随机推荐
Electronic Society C language level 1 7. Draw rectangle
Can Huawei matepad become the secondary screen of your laptop?
Thread pool: a magic weapon for managing threads
LeetCode+ 21 - 25
Reptiles learn knowledge
[raise bar C #] how to call the base of the interface
But it shows that you are the most native zaifkmw in five counties
Is there any risk in opening an account with BOC securities? Is it safe?
Fallback operation in SVN
Basic knowledge learning of Web cluster (1)
js敏感信息泄露检测工具
Sdn/nfv application in cloud data center
集度夏一平:不是所有事都向李彦宏汇报,靠产品跟小米华为竞争
What are the common methods of object
Online questionnaire system based on php+web+mysql
期货开户有什么限制嘛?哪里最安全?
vulnhub之dc4
MA8601 pin√pin替代汤铭FE1.1s无须更改电路板|完美替代FE1.1s方案
样板小作坊
Which securities company does qiniu's securities belong to? Is it safe?