perf:测试数据库备份
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
package com.tiesheng.database.config;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.util.RuntimeUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import com.tiesheng.database.utls.TieshengDbUtil;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@@ -33,10 +35,12 @@ public class DbBackupConfig {
|
||||
String url = SpringUtil.getProperty("spring.datasource.url");
|
||||
String username = SpringUtil.getProperty("spring.datasource.username");
|
||||
String password = SpringUtil.getProperty("spring.datasource.password");
|
||||
String dbName = TieshengDbUtil.getTableSchema(url);
|
||||
|
||||
try {
|
||||
RuntimeUtil.exec(StrUtil.format("mysqldump -u{} -p{} {} > {}{}.sql", username, password, username,
|
||||
getPath(), DateUtil.format(new Date(), format)
|
||||
String saveFile = StrUtil.format("{}{}/{}.sql", getPath(), dbName, DateUtil.format(new Date(), format));
|
||||
FileUtil.mkParentDirs(saveFile);
|
||||
RuntimeUtil.exec(StrUtil.format("mysqldump -u{} -p{} {} > {}", username, password, dbName, saveFile
|
||||
));
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.tiesheng.database.utls;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
public class TieshengDbUtil {
|
||||
|
||||
/**
|
||||
* 通过url获取table_schema
|
||||
*
|
||||
* @param url
|
||||
* @return
|
||||
*/
|
||||
public static String getTableSchema(String url) {
|
||||
return StrUtil.sub(url, url.lastIndexOf('/') + 1, StrUtil.indexOf(url, '?'));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user