fix: mysql备份代码

This commit is contained in:
republicline 2024-10-15 10:02:14 +08:00
parent 0db4ccd368
commit c88206116e
4 changed files with 15 additions and 11 deletions

View File

@ -4,7 +4,6 @@ import com.rax.common.security.annotation.EnableRaxResourceServer;
import com.rax.common.swagger.annotation.EnableRaxDoc; import com.rax.common.swagger.annotation.EnableRaxDoc;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;

View File

@ -66,7 +66,7 @@ public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> impleme
.le(SysLog::getCreateTime, lastDayOfMonth) .le(SysLog::getCreateTime, lastDayOfMonth)
.eq(sysLog.getLogType() != null, SysLog::getLogType, sysLog.getLogType()) .eq(sysLog.getLogType() != null, SysLog::getLogType, sysLog.getLogType())
.orderByDesc(SysLog::getCreateTime).list(); .orderByDesc(SysLog::getCreateTime).list();
}else { } else {
list = this.lambdaQuery() list = this.lambdaQuery()
.eq(SysLog::getHospitalId, currentHospital) .eq(SysLog::getHospitalId, currentHospital)
.eq(sysLog.getLogType() != null, SysLog::getLogType, sysLog.getLogType()) .eq(sysLog.getLogType() != null, SysLog::getLogType, sysLog.getLogType())
@ -120,7 +120,11 @@ public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> impleme
userLambdaQueryWrapper.eq(SysUser::getUsername, sysLog.getCreateBy()); userLambdaQueryWrapper.eq(SysUser::getUsername, sysLog.getCreateBy());
SysUser sysUser = sysUserMapper.selectOne(userLambdaQueryWrapper); SysUser sysUser = sysUserMapper.selectOne(userLambdaQueryWrapper);
// 设置到日志对象中 // 设置到日志对象中
if (sysUser != null) {
sysLog.setHospitalId(sysUser.getHospitalId()); sysLog.setHospitalId(sysUser.getHospitalId());
}else {
return Boolean.FALSE;
}
} }
baseMapper.insert(sysLog); baseMapper.insert(sysLog);
return Boolean.TRUE; return Boolean.TRUE;

View File

@ -16,16 +16,17 @@ import java.util.concurrent.TimeUnit;
* @author: republicline * @author: republicline
* @description: mysql定时备份 * @description: mysql定时备份
*/ */
@Component
@Slf4j @Slf4j
@Component
public class MySqlTimer { public class MySqlTimer {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
backup(); MySqlTimer mySqlTimer = new MySqlTimer();
mySqlTimer.backup();
} }
@Scheduled(cron = "0 0 0 * * 0") @Scheduled(cron = "0 0 15 * * 1")
public static void backup() throws Exception { public void backup() throws Exception {
String connectionUrl = "jdbc:mysql://110.41.142.124:3306"; String connectionUrl = "jdbc:mysql://110.41.142.124:3306";
String ip = "110.41.142.124"; String ip = "110.41.142.124";
String port = "3306"; String port = "3306";
@ -34,7 +35,7 @@ public class MySqlTimer {
Connection connection = DriverManager.getConnection(connectionUrl, username, password); Connection connection = DriverManager.getConnection(connectionUrl, username, password);
Statement statement = connection.createStatement(); Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SHOW DATABASES"); ResultSet resultSet = statement.executeQuery("SHOW DATABASES");
// System.out.println("MySQL服务器上的数据库列表:"); System.out.println("MySQL服务器上的数据库列表:");
while (resultSet.next()) { while (resultSet.next()) {
String databaseName = resultSet.getString(1); String databaseName = resultSet.getString(1);
@ -56,6 +57,7 @@ public class MySqlTimer {
connection.close(); connection.close();
} }
/** /**
* 备份mysql数据库 * 备份mysql数据库
* *
@ -67,7 +69,7 @@ public class MySqlTimer {
* @param dbName 数据库名 * @param dbName 数据库名
* @throws Exception * @throws Exception
*/ */
public static void dbBackUpMysql(String ip, String port, String username, String pwd, String path, String dbName) throws Exception { public void dbBackUpMysql(String ip, String port, String username, String pwd, String path, String dbName) throws Exception {
//mysqldump -uroot -pldeSpQEL0Pbz5A61dCNb --host=123.56.234.243 --port=3309 edc > /opt/2024-10-08/edc.sql //mysqldump -uroot -pldeSpQEL0Pbz5A61dCNb --host=123.56.234.243 --port=3309 edc > /opt/2024-10-08/edc.sql
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String time = simpleDateFormat.format(new Date()); String time = simpleDateFormat.format(new Date());
@ -108,7 +110,6 @@ public class MySqlTimer {
} else if (System.getProperty("os.name").toLowerCase().indexOf("linux") > -1) { } else if (System.getProperty("os.name").toLowerCase().indexOf("linux") > -1) {
process = Runtime.getRuntime().exec("/bin/sh -c" + sb); process = Runtime.getRuntime().exec("/bin/sh -c" + sb);
} else { } else {
log.error("暂不支持该操作系统,进行数据库备份或还原!");
throw new Exception("暂不支持该操作系统,进行数据库备份或还原!"); throw new Exception("暂不支持该操作系统,进行数据库备份或还原!");
} }

View File

@ -165,7 +165,7 @@ spring:
driver-class-name: com.mysql.cj.jdbc.Driver driver-class-name: com.mysql.cj.jdbc.Driver
username: root username: root
password: root password: root
url: jdbc:mysql://localhost:3306/rax_backend1?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowPublicKeyRetrieval=true&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true&nullCatalogMeansCurrent=true # 一期 url: jdbc:mysql://110.41.142.124:3306/rax_backend1?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowPublicKeyRetrieval=true&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true&nullCatalogMeansCurrent=true # 一期
# url: jdbc:mysql://localhost:3306/rax_backend?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowPublicKeyRetrieval=true&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true&nullCatalogMeansCurrent=true # 二期 # url: jdbc:mysql://localhost:3306/rax_backend?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowPublicKeyRetrieval=true&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true&nullCatalogMeansCurrent=true # 二期
server: server: