Springboot 實現(xiàn)數(shù)據(jù)庫備份還原
點擊上方藍色字體,選擇“標(biāo)星公眾號”
優(yōu)質(zhì)文章,第一時間送達
? 作者?|??Maolic
來源 |? urlify.cn/ma6nqm
備份數(shù)據(jù)庫
備份通過命令行對數(shù)據(jù)庫導(dǎo)出到指定目錄即可。我這里是一個Get請求,頁面需要展示備份文件名稱、大小和備份時間,代碼中使用的log是Slf4j,最終界面效果如圖:

代碼對我的原代碼有所改動,關(guān)于備份文件的存放目錄,我配置在了application.properties配置文件中,通過一個配置類ProjectUrlConfig去獲取,代碼中的?projectUrlConfig.getBackPath()?即為文件目錄,與fileName拼接成完整的路徑。
/*?備份數(shù)據(jù)庫?*/
????@GetMapping("backupSQL")
????public?ModelAndView?backupSQL(Map?map){
????????String?fileName?=?"backup_"?+?new?Date().getTime()?+?".sql";
????????String?cmd?=??"mysqldump?-uroot?-p123456?dbName?>?"?+ projectUrlConfig.getBackPath()?+ fileName;?//-u后的root為mysql數(shù)據(jù)庫用戶名,-p后接的123456為該用戶密碼,注意不要有空格;dbName填寫需要備份數(shù)據(jù)的數(shù)據(jù)庫名稱,大于號后接生成文件路徑
????????try?{
????????????Runtime.getRuntime().exec(cmd);
????????}catch?(Exception?e){
????????????log.error("【備份數(shù)據(jù)庫】失?。簕}",?e.getMessage());
????????????map.put("msg",?e.getMessage());
????????????return?new?ModelAndView("common/error",?map);
????????}
????????log.info("【備份數(shù)據(jù)庫】成功,SQL文件:{}",?fileName);
????????map.put("msg","備份數(shù)據(jù)庫成功");return?new?ModelAndView("common/success",?map);
????} 恢復(fù)數(shù)據(jù)庫
備份雖然在cmd命令行中使用 “mysql -uroot -p123456 dbName < SQL文件路徑?” 可以對數(shù)據(jù)庫還原,嘗試使用時沒有發(fā)現(xiàn)報錯但數(shù)據(jù)庫并未還原,最后通過?OutputStreamWriter?來實現(xiàn)。
@GetMapping("rollback")
????public?ModelAndView?rollback(@RequestParam("filename")?String?fileName,?Map?map){
????????String?path?=?projectUrlConfig.getBackPath()?+?fileName;????????try?{
????????????Runtime?runtime?=?Runtime.getRuntime();
????????????Process?process?=?runtime.exec("mysql?-uroot?-p123456?--default-character-set=utf8?dbName");????????????OutputStream?outputStream?=?process.getOutputStream();
????????????FileInputStream?fis?=?new?FileInputStream(path);
????????????InputStreamReader?isr?=?new?InputStreamReader(fis,?"utf-8");
????????????BufferedReader?br?=?new?BufferedReader(isr);
????????????String?str?=?null;
????????????StringBuffer?sb?=?new?StringBuffer();
????????????while?((str?=?br.readLine())?!=?null)?{
????????????????sb.append(str?+?"\r\n");
????????????}
????????????str?=?sb.toString();
????????????OutputStreamWriter?writer?=?new?OutputStreamWriter(outputStream,"utf-8");
????????????writer.write(str);
????????????writer.flush();
????????????if(writer!=null){
????????????????writer.close();
????????????}
????????????if(br!=null){
????????????????br.close();
????????????}
????????????if(isr!=null){
????????????????isr.close();
????????????}
????????????if(fis!=null){
????????????????fis.close();
????????????}
????????????if(outputStream!=null){
????????????????outputStream.close();
????????????}
????????}catch?(Exception?e){
????????????log.error("【還原數(shù)據(jù)庫】失敗:{}",?e.getMessage());
????????????map.put("msg",?e.getMessage());
????????????return?new?ModelAndView("common/error",?map);
????????}
????????log.info("【還原數(shù)據(jù)庫】成功,還原文件:{}",?fileName);
????????map.put("msg","還原數(shù)據(jù)庫成功");return?new?ModelAndView("common/success",?map);
????}? 以上即可對數(shù)據(jù)庫進行備份與恢復(fù),但是也只是適用于較小的數(shù)據(jù)庫。
粉絲福利:108本java從入門到大神精選電子書領(lǐng)取
???
?長按上方鋒哥微信二維碼?2 秒 備注「1234」即可獲取資料以及 可以進入java1234官方微信群
感謝點贊支持下哈?
評論
圖片
表情
