- 2023-01-23 08:59:39
- 7243 热度
- 0 评论
核心是mysqldump和Runtime
操作其实并不是很困难,创建一个进行备份操作的类,接收到备份调用后,标记该表正在备份,然后创建一个子线程进行备份操作。所需的配置信息是从配置文件读取的,也要注意在Windows和linux下路径问题。
配置文件如下:
# 数据库地址 dbAddress=localhost # 要备份的数据库名称 databaseName=nms # 数据库用户名 username = root # 数据库密码 password = root # mysqldump 路径 Linux mysqlpath = /usr/bin/ # 备份文件存放位置 Linux sqlFilePath =/MySQlBack/ # mysqldump 路径 Windows #mysqlpath = C\://Program Files//MySQL//MySQL Server 5.5//bin// # 备份文件存放位置 Windows #sqlFilePath =C\://MySQl//
执行功能的代码类如下:
package com.nms.common.db; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.Map; import java.util.Properties; import java.util.concurrent.ConcurrentHashMap; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * 用于数据库备份操作 */ public class DbBackUpMethod { private static Log logger = LogFactory.getLog(DbBackUpMethod.class); private static Properties pros = getPprVue("db.properties"); public static Map<String, String> backUpTableList = new ConcurrentHashMap<String, String>(); private static DbBackUpMethod backObj = new DbBackUpMethod(); public static DbBackUpMethod getDbBackUpMethod(){ return backObj; } public void backup(String tableName) { if(null != backUpTableList.get(tableName)) return ; backUpTableList.put(tableName, tableName); // 标记已经用于备份 new Thread(new DbBackUpThread(tableName)).start(); } /** * 用于执行某表的备份 */ class DbBackUpThread implements Runnable { String tableName = null; public DbBackUpThread(String tableName){ this.tableName = tableName; } @Override public void run() { try { String username = pros.getProperty("username"); String password = pros.getProperty("password"); String mysqlpaths = pros.getProperty("mysqlpath"); String address = pros.getProperty("dbAddress"); String databaseName = pros.getProperty("databaseName"); String sqlpath = pros.getProperty("sqlFilePath"); File backupath = new File(sqlpath); if (!backupath.exists()) { backupath.mkdir(); } StringBuffer sb = new StringBuffer(); sb.append(mysqlpaths); sb.append("mysqldump "); sb.append("--opt "); sb.append("-h "); sb.append(address); sb.append(" "); sb.append("--user="); sb.append(username); sb.append(" "); sb.append("--password="); sb.append(password); sb.append(" "); sb.append("--lock-all-tables=true "); sb.append("--result-file="); sb.append(sqlpath); sb.append(tableName+".sql"); sb.append(" "); sb.append("--default-character-set=utf8 "); sb.append(databaseName); sb.append(" "); sb.append(tableName); Runtime cmd = Runtime.getRuntime(); Process p = cmd.exec(sb.toString()); p.waitFor(); // 该语句用于标记,如果备份没有完成,则该线程持续等待 } catch (Exception e) { logger.error("备份操作出现问题", e); }finally{ backUpTableList.remove(tableName); // 最终都将解除 } } } public static Properties getPprVue(String properName) { InputStream inputStream = DbBackUpMethod.class.getClassLoader().getResourceAsStream(properName); Properties p = new Properties(); try { p.load(inputStream); inputStream.close(); } catch (IOException e) { logger.error("无法读取用于备份数据的配置文件", e); } return p; } }
在Action中,可以直接调用备份操作方法:
DbBackUpMethod.getDbBackUpMethod().backup(tableName); // 调用备份
同时,如果页面有删除该表的操作,在操作前应该判断该表是否在进行备份
if(null != DbBackUpMethod.backUpTableList.get(tableName))
然后页面JSP调用时,可以给予响应的提示,我的判断是只能删除一张表:
function deleteTableByTableName(){ var pk = table.getSelectedKeys(); if(""==pk){ alert("请选择一条记录!"); return false; } if(pk.length > 1){ alert("请选择一条记录!"); return false; } var rows = table.get(pk); var tableName=rows.tableName; if(confirm("你确认要删除该表吗?")) { if(confirm("删除该表前,你需要备份操作吗?\n\n选择备份后,系统将后台进行相关操作! \n在此期间,您不能删除该表!\n备份操作可能将持续数小时时间!请知晓!")) { document.form1.action="backUpTable.action?tableName=" + tableName; document.form1.submit(); }else{ if(confirm("你确认提交吗?该表将删除!")) { document.form1.action="del.action?tableName=" + tableName; document.form1.submit(); } } } }
请各位提出意见。
0 评论
留下评论
热门标签
- Spring(403)
- Boot(208)
- Spring Boot(187)
- Spring Cloud(82)
- Java(82)
- Cloud(82)
- Security(60)
- Spring Security(54)
- Boot2(51)
- Spring Boot2(51)
- Redis(31)
- SQL(29)
- Mysql(25)
- Dalston(24)
- IDE(24)
- mongoDB(22)
- MVC(22)
- JDBC(22)
- IDEA(22)
- Web(21)
- CLI(20)
- Alibaba(19)
- SpringMVC(19)
- Docker(17)
- SpringBoot(17)
- Git(16)
- Eclipse(16)
- Vue(16)
- JPA(15)
- Apache(15)
- ORA(15)
- Tomcat(14)
- Linux(14)
- HTTP(14)
- Mybatis(14)
- Oracle(14)
- jdk(14)
- OAuth(13)
- Nacos(13)
- Pro(13)
- XML(13)
- JdbcTemplate(13)
- JSON(12)
- OAuth2(12)
- Data(12)
- int(11)
- Myeclipse(11)
- stream(11)
- not(10)
- Bug(10)
- Hystrix(9)
- ast(9)
- maven(9)
- Map(9)
- Swagger(8)
- APP(8)
- Bit(8)
- API(8)
- session(8)
- Window(8)
- windows(7)
- too(7)
- HTML(7)
- Github(7)
- JavaMail(7)
- Cache(7)
- File(7)
- IntelliJ(7)
- mail(7)
- Server(6)
- nginx(6)
- jar(6)
- ueditor(6)
- ehcache(6)
- UDP(6)
- RabbitMQ(6)
- and(6)
- star(6)
- Excel(6)
- Log4J(6)
- pushlet(6)
- apt(6)
- Freemarker(6)
- read(6)
- WebFlux(6)
- JSP(6)
- Bean(6)
- error(6)
- are(5)
- SVN(5)
- for(5)
- DOM(5)
- Sentinel(5)
- the(5)
- JWT(5)
- rdquo(5)
- PHP(5)
- Struts(5)
- string(5)
- script(5)