MySQL基于java實(shí)現(xiàn)備份表操作
核心是mysqldump和Runtime操作其實(shí)并不是很困難,創(chuàng)建一個(gè)進(jìn)行備份操作的類,接收到備份調(diào)用后,標(biāo)記該表正在備份,然后創(chuàng)建一個(gè)子線程進(jìn)行備份操作。所需的配置信息是從配置文件讀取的,也要注意在Windows和linux下路徑問題。
配置文件如下:
Java代碼 收藏代碼# 數(shù)據(jù)庫地址 dbAddress=localhost # 要備份的數(shù)據(jù)庫名稱 databaseName=nms # 數(shù)據(jù)庫用戶名 username = root # 數(shù)據(jù)庫密碼 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//
執(zhí)行功能的代碼類如下:
Java代碼 收藏代碼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; /** * 用于數(shù)據(jù)庫備份操作 */ 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); // 標(biāo)記已經(jīng)用于備份 new Thread(new DbBackUpThread(tableName)).start(); } /** * 用于執(zhí)行某表的備份 */ 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(); // 該語句用于標(biāo)記,如果備份沒有完成,則該線程持續(xù)等待 } catch (Exception e) { logger.error('備份操作出現(xiàn)問題', 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('無法讀取用于備份數(shù)據(jù)的配置文件', e); } return p; } }
在Action中,可以直接調(diào)用備份操作方法:
Java代碼 收藏代碼DbBackUpMethod.getDbBackUpMethod().backup(tableName); // 調(diào)用備份 同時(shí),如果頁面有刪除該表的操作,在操作前應(yīng)該判斷該表是否在進(jìn)行備份
Java代碼 收藏代碼if(null != DbBackUpMethod.backUpTableList.get(tableName))
然后頁面JSP調(diào)用時(shí),可以給予響應(yīng)的提示,我的判斷是只能刪除一張表:
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('你確認(rèn)要?jiǎng)h除該表嗎?')) {if(confirm('刪除該表前,你需要備份操作嗎?nn選擇備份后,系統(tǒng)將后臺(tái)進(jìn)行相關(guān)操作!n在此期間,您不能刪除該表!n備份操作可能將持續(xù)數(shù)小時(shí)時(shí)間!請知曉!')) {document.form1.action='backUpTable.action?tableName=' + tableName;document.form1.submit();}else{if(confirm('你確認(rèn)提交嗎?該表將刪除!')) {document.form1.action='del.action?tableName=' + tableName;document.form1.submit();}}}}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. JavaWeb Servlet中url-pattern的使用2. jsp中sitemesh修改tagRule技術(shù)分享3. asp(vbscript)中自定義函數(shù)的默認(rèn)參數(shù)實(shí)現(xiàn)代碼4. React優(yōu)雅的封裝SvgIcon組件示例5. 輕松學(xué)習(xí)XML教程6. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究7. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)8. JSP servlet實(shí)現(xiàn)文件上傳下載和刪除9. ASP基礎(chǔ)知識(shí)VBScript基本元素講解10. 詳解瀏覽器的緩存機(jī)制
