|
@@ -1,18 +1,29 @@
|
|
|
package com.qmth.distributed.print.business.backup;
|
|
|
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import com.aventrix.jnanoid.jnanoid.NanoIdUtils;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.qmth.teachcloud.common.config.DictionaryConfig;
|
|
|
import com.qmth.teachcloud.common.contant.SystemConstant;
|
|
|
+import com.qmth.teachcloud.common.entity.SysConfig;
|
|
|
+import com.qmth.teachcloud.common.entity.SysOrg;
|
|
|
+import com.qmth.teachcloud.common.entity.SysRole;
|
|
|
+import com.qmth.teachcloud.common.entity.SysUser;
|
|
|
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
|
|
|
+import com.qmth.teachcloud.common.service.*;
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.io.*;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
import java.util.Objects;
|
|
|
import java.util.Optional;
|
|
|
|
|
@@ -45,18 +56,29 @@ public class MySQLDatabaseBackup {
|
|
|
@Value("${db.password}")
|
|
|
String password;
|
|
|
|
|
|
+ @Resource
|
|
|
+ SysRoleService sysRoleService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ SysUserService sysUserService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ CommonCacheService commonCacheService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ SysOrgService sysOrgService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ SysConfigService sysConfigService;
|
|
|
+
|
|
|
/**
|
|
|
* Java代码实现MySQL数据库导出
|
|
|
*
|
|
|
- * @param fileName
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public boolean exportDatabaseTool(String fileName) throws Exception {
|
|
|
- Optional.ofNullable(fileName).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("文件名不能为空"));
|
|
|
-
|
|
|
- File file = new File(dictionaryConfig.fssLocalFileDomain().getConfig() + File.separator + fileName);
|
|
|
-// File file = new File("/Users/king/Downloads/file-temp/db/backup" + File.separator + fileName);
|
|
|
+ public boolean exportDatabaseTool() throws Exception {
|
|
|
+ File file = new File(dictionaryConfig.fssLocalFileDomain().getConfig() + File.separator + "db" + File.separator + "backup" + File.separator + DateUtil.format(new Date(), SystemConstant.BACK_UP_DATE_PATTERN) + "_" + NanoIdUtils.randomNanoId() + "_backup.sql");
|
|
|
if (!file.exists()) {
|
|
|
file.getParentFile().mkdirs();
|
|
|
file.createNewFile();
|
|
@@ -100,20 +122,13 @@ public class MySQLDatabaseBackup {
|
|
|
|
|
|
InputStream inputStream = MySQLDatabaseBackup.class.getClassLoader().getResourceAsStream(fileName);
|
|
|
Optional.ofNullable(inputStream).orElseThrow(() -> ExceptionResultEnum.ERROR.exception(fileName + "未找到"));
|
|
|
- File file = new File(dictionaryConfig.fssLocalFileDomain().getConfig() + File.separator + fileName);
|
|
|
+ File file = new File(dictionaryConfig.fssLocalFileDomain().getConfig() + File.separator + "db" + File.separator + "delete" + File.separator + DateUtil.format(new Date(), SystemConstant.BACK_UP_DATE_PATTERN) + "_" + NanoIdUtils.randomNanoId() + "_" + schoolId + "_delete.sql");
|
|
|
if (!file.exists()) {
|
|
|
file.getParentFile().mkdirs();
|
|
|
file.createNewFile();
|
|
|
}
|
|
|
FileUtils.copyInputStreamToFile(inputStream, file);
|
|
|
|
|
|
-// File file = new File("/Users/king/Downloads/file-temp/db/delete" + File.separator + DateUtil.format(new Date(), SystemConstant.BACK_UP_DATE_PATTERN) + "_" + NanoIdUtils.randomNanoId() + "_" + schoolId + "_delete.sql");
|
|
|
-// if (!file.exists()) {
|
|
|
-// file.getParentFile().mkdirs();
|
|
|
-// file.createNewFile();
|
|
|
-// }
|
|
|
-// FileUtils.copyInputStreamToFile(new FileInputStream(new File(fileName)), file);
|
|
|
-
|
|
|
ByteArrayOutputStream ou = new ByteArrayOutputStream();
|
|
|
IOUtils.copy(new FileInputStream(file), ou);
|
|
|
String string = new String(ou.toByteArray(), StandardCharsets.UTF_8);
|
|
@@ -121,6 +136,28 @@ public class MySQLDatabaseBackup {
|
|
|
string = string.replaceAll("\\#\\{schoolId\\}", String.valueOf(schoolId));
|
|
|
}
|
|
|
IOUtils.write(string.getBytes(StandardCharsets.UTF_8), new FileOutputStream(file));
|
|
|
+
|
|
|
+ //删除部分重要缓存
|
|
|
+ //角色
|
|
|
+ QueryWrapper<SysRole> sysRoleQueryWrapper = new QueryWrapper<>();
|
|
|
+ sysRoleQueryWrapper.lambda().eq(SysRole::getSchoolId, schoolId);
|
|
|
+ List<SysRole> sysRoleList = sysRoleService.list(sysRoleQueryWrapper);
|
|
|
+
|
|
|
+ //用户
|
|
|
+ QueryWrapper<SysUser> sysUserQueryWrapper = new QueryWrapper<>();
|
|
|
+ sysUserQueryWrapper.lambda().eq(SysUser::getSchoolId, schoolId);
|
|
|
+ List<SysUser> sysUserList = sysUserService.list(sysUserQueryWrapper);
|
|
|
+
|
|
|
+ //机构
|
|
|
+ QueryWrapper<SysOrg> sysOrgQueryWrapper = new QueryWrapper<>();
|
|
|
+ sysOrgQueryWrapper.lambda().eq(SysOrg::getSchoolId, schoolId);
|
|
|
+ List<SysOrg> sysOrgList = sysOrgService.list(sysOrgQueryWrapper);
|
|
|
+
|
|
|
+ //配置项
|
|
|
+ QueryWrapper<SysConfig> sysConfigQueryWrapper = new QueryWrapper<>();
|
|
|
+ sysConfigQueryWrapper.lambda().eq(SysConfig::getSchoolId, schoolId);
|
|
|
+ List<SysConfig> sysConfigList = sysConfigService.list(sysConfigQueryWrapper);
|
|
|
+
|
|
|
String cmdarray[] = {"mysql -h" + this.host + " -P" + this.port + " -u" + this.username + " -p" + this.password + " " + this.databaseName, "source " + file.getPath()};
|
|
|
Runtime runtime = Runtime.getRuntime();
|
|
|
Process process = null;
|
|
@@ -152,8 +189,38 @@ public class MySQLDatabaseBackup {
|
|
|
log.error(SystemConstant.LOG_ERROR, e);
|
|
|
} finally {
|
|
|
if (process.waitFor() == 0 && Objects.nonNull(file)) {
|
|
|
- string = "删除数据成功";
|
|
|
+ string = DateUtil.format(new Date(), SystemConstant.DEFAULT_DATE_PATTERN) + "删除数据成功!!!";
|
|
|
IOUtils.write(string.getBytes(StandardCharsets.UTF_8), new FileOutputStream(file));
|
|
|
+ if (!CollectionUtils.isEmpty(sysRoleList)) {
|
|
|
+ for (SysRole s : sysRoleList) {
|
|
|
+ commonCacheService.removeRoleCache(s.getId());
|
|
|
+ commonCacheService.removeRolePrivilegeCache(s.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!CollectionUtils.isEmpty(sysUserList)) {
|
|
|
+ for (SysUser s : sysUserList) {
|
|
|
+ commonCacheService.removeUserAuthCache(s.getId());
|
|
|
+ commonCacheService.removeUserCache(s.getId());
|
|
|
+ commonCacheService.removeUserMenuCache(s.getId());
|
|
|
+ commonCacheService.removeUserRolePrivilegeCache(s.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!CollectionUtils.isEmpty(sysOrgList)) {
|
|
|
+ for (SysOrg s : sysOrgList) {
|
|
|
+ commonCacheService.removeOrgCache(s.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!CollectionUtils.isEmpty(sysConfigList)) {
|
|
|
+ for (SysConfig s : sysConfigList) {
|
|
|
+ commonCacheService.removeSysConfigCache(s.getSchoolId(), SystemConstant.PDF_SIZE_LIST);
|
|
|
+ commonCacheService.removeSysConfigCache(s.getSchoolId(), SystemConstant.ACCOUNT_SMS_VERIFY);
|
|
|
+ commonCacheService.removeSysConfigCache(s.getSchoolId(), SystemConstant.TEACHCLOUD_EXCHANGE_SERVICE_PATH);
|
|
|
+ commonCacheService.removeSysConfigCache(s.getSchoolId(), SystemConstant.SYNC_SERVICE_PATH);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|