fix: mysql备份代码

This commit is contained in:
republicline 2024-10-09 10:14:58 +08:00
parent 1add5ac60e
commit 5243f35dd6

View File

@ -1,5 +1,11 @@
package com.rax.admin.timmer; package com.rax.admin.timmer;
import java.io.File;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
/** /**
* @project_name: rax-remote-v2 * @project_name: rax-remote-v2
* @time: 2024/10/8 10:57 * @time: 2024/10/8 10:57
@ -7,4 +13,114 @@ package com.rax.admin.timmer;
* @description: mysql定时备份 * @description: mysql定时备份
*/ */
public class MySqlTimer { public class MySqlTimer {
public static void main(String[] args) throws Exception {
backup();
}
public static void backup() throws Exception {
String connectionUrl = "jdbc:mysql://110.41.142.124:3306";
String ip = "110.41.142.124";
String port = "3306";
String username = "root";
String password = "Xg137839";
Connection connection = DriverManager.getConnection(connectionUrl, username, password);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SHOW DATABASES");
System.out.println("MySQL服务器上的数据库列表:");
while (resultSet.next()) {
String databaseName = resultSet.getString(1);
System.out.println(databaseName);
if ("sys".equals(databaseName)) {
continue;
}
dbBackUpMysql(
ip,
port,
username,
password,
"/RuiAx/mysql_backup",
databaseName);
}
resultSet.close();
statement.close();
connection.close();
}
/**
* 备份mysql数据库
*
* @param username 账号
* @param pwd 密码
* @param ip 地址
* @param port 端口
* @param path 路径
* @param dbName 数据库名
* @throws Exception
*/
public static 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
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String time = simpleDateFormat.format(new Date());
path = path + File.separator + time + File.separator;
String pathSql = path + dbName + ".sql";
File filePath = new File(path);
File fileSql = new File(pathSql);
//创建备份sql文件
if (!filePath.exists()) {
filePath.mkdirs();
}
if (!fileSql.exists()) {
fileSql.createNewFile();
}
//mysqldump -uroot -pldeSpQEL0Pbz5A61dCNb --host=123.56.234.243 --port=3309 edc > /opt/2024-10-08/edc.sql
StringBuffer sb = new StringBuffer();
sb.append("mysqldump");
sb.append(" -u" + username);
sb.append(" -p" + pwd);
sb.append(" --host=" + ip);
sb.append(" --port=" + port);
sb.append(" " + dbName + " >");
sb.append(pathSql);
System.out.println("cmd命令为" + sb.toString());
System.out.println("开始备份:" + dbName);
Process process = null;
//判断操作系统 windwos与linux使用的语句不一样
if (System.getProperty("os.name").toLowerCase().indexOf("windows") > -1) {
process = Runtime.getRuntime().exec("cmd /c" + sb);
} else if (System.getProperty("os.name").toLowerCase().indexOf("linux") > -1) {
process = Runtime.getRuntime().exec("/bin/sh -c" + sb);
} else {
throw new Exception("暂不支持该操作系统,进行数据库备份或还原!");
}
//设置超时五分钟
process.waitFor(300, TimeUnit.SECONDS);
//输出返回的错误信息
// StringBuffer mes = new StringBuffer();
// String tmp = "";
// BufferedReader error = new BufferedReader(new InputStreamReader(process.getErrorStream()));
// while ((tmp = error.readLine()) != null) {
// mes.append(tmp + "\n");
// }
// if (mes != null || !"".equals(mes)) {
// System.out.println("备份成功!==>" + mes);
// }
// error.close();
}
} }