索引html
簡述數據庫
程序文件備份與數據備份json
項目準備服務器
咱們用的工具是:VS 2013 + SqlServer 2012 + IIS7.5mvc
但願你們對ASP.NET MVC有一個初步的理解,理論性的東西咱們不作過多解釋,有些地方不理解也不要緊,會用就好了,用的多了,用的久了,天然就理解了。框架
1 /// <summary> 2 /// 獲取備份文件信息 3 /// </summary> 4 /// <returns></returns> 5 public ActionResult GetBackUpData() 6 { 7 string fileExt = Request.Form["fileExt"]; 8 string path = "/App_Data/BackUp/"; 9 var jsonM = new JsonHelper() { Status = "y", Msg = "success" }; 10 try 11 { 12 if (!FileHelper.IsExistDirectory(Server.MapPath(path))) 13 { 14 jsonM.Status = "n"; 15 jsonM.Msg = "目錄不存在!"; 16 } 17 else if (FileHelper.IsEmptyDirectory(Server.MapPath(path))) 18 { 19 jsonM.Status = "empty"; 20 } 21 else 22 { 23 if (fileExt == "*" || string.IsNullOrEmpty(fileExt)) 24 { 25 jsonM.Data = Common.Utils.DataTableToList<FileModel>(FileHelper.GetAllFileTable(Server.MapPath(path))).OrderByDescending(p => p.time).ToList(); 26 } 27 else 28 { 29 jsonM.Data = Common.Utils.DataTableToList<FileModel>(FileHelper.GetAllFileTable(Server.MapPath(path))).OrderByDescending(p => p.time).Where(p => p.ext == fileExt).ToList(); 30 } 31 32 } 33 34 } 35 catch (Exception) 36 { 37 jsonM.Status = "err"; 38 jsonM.Msg = "獲取文件失敗!"; 39 } 40 return Content(JsonConverter.Serialize(jsonM, true)); 41 }
咱們用zip壓縮和解壓,注意的一點就是 咱們壓縮的文件是存儲在App_Data目錄下,因此壓縮的時候要排除這個目錄,否則正在寫入這個目錄同時壓縮這個目錄會衝突。工具
壓縮方法 參考:post
1 /// <summary> 2 /// 備份程序文件 3 /// </summary> 4 /// <returns></returns> 5 [UserAuthorizeAttribute(ModuleAlias = "Backup", OperaAction = "BackUpApplication")] 6 public ActionResult BackUpFiles() 7 { 8 var json = new JsonHelper() { Msg = "程序備份完成", Status = "n" }; 9 10 try 11 { 12 //檢查上傳的物理路徑是否存在,不存在則建立 13 if (!Directory.Exists(Server.MapPath("/App_Data/BackUp/ApplicationBackUp/"))) 14 { 15 Directory.CreateDirectory(Server.MapPath("/App_Data/BackUp/ApplicationBackUp/")); 16 } 17 18 ZipHelper.ZipDirectory(Server.MapPath("/"), Server.MapPath("/App_Data/BackUp/ApplicationBackUp/"), "App_" + this.CurrentUser.PinYin + "_" + DateTime.Now.ToString("yyyyMMddHHmmss"), true, new List<string>() { Server.MapPath("/App_Data/") }); 19 WriteLog(Common.Enums.enumOperator.None, "程序備份:" + json.Msg, Common.Enums.enumLog4net.WARN); 20 json.Status = "y"; 21 } 22 catch (Exception e) 23 { 24 json.Msg = "程序備份失敗!"; 25 WriteLog(Common.Enums.enumOperator.None, "程序備份:", e); 26 } 27 28 return Json(json); 29 }
咱們來測試一下:學習
1 /// <summary> 2 /// 備份數據 3 /// </summary> 4 /// <returns></returns> 5 [UserAuthorizeAttribute(ModuleAlias = "Backup", OperaAction = "BackUpDataBase")] 6 public ActionResult BackUpData() 7 { 8 var json = new JsonHelper() { Msg = "數據備份完成", Status = "n" }; 9 10 try 11 { 12 //檢查上傳的物理路徑是否存在,不存在則建立 13 if (!Directory.Exists(Server.MapPath("/App_Data/BackUp/DataBaseBackUp/"))) 14 { 15 Directory.CreateDirectory(Server.MapPath("/App_Data/BackUp/DataBaseBackUp/")); 16 } 17 //備份數據庫 18 using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString)) 19 { 20 var bakPath = Server.MapPath("/App_Data/BackUp/DataBaseBackUp/"); 21 using (SqlCommand cmd = new SqlCommand("backup database wkmvc_comnwes to disk='" + bakPath + "Data_" + this.CurrentUser.PinYin + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".bak'", conn)) 22 { 23 try 24 { 25 conn.Open(); 26 cmd.CommandTimeout = 0; 27 cmd.ExecuteNonQuery(); 28 } 29 catch (Exception e) 30 { 31 throw e; 32 } 33 finally 34 { 35 conn.Close(); 36 cmd.Dispose(); 37 } 38 } 39 } 40 41 WriteLog(Common.Enums.enumOperator.None, "數據備份:" + json.Msg, Common.Enums.enumLog4net.WARN); 42 json.Status = "y"; 43 } 44 catch (Exception e) 45 { 46 json.Msg = "數據備份失敗!"; 47 WriteLog(Common.Enums.enumOperator.None, "數據備份:", e); 48 } 49 50 return Json(json); 51 }
1 /// <summary> 2 /// 還原數據 3 /// </summary> 4 /// <returns></returns> 5 [UserAuthorizeAttribute(ModuleAlias = "Restore", OperaAction = "RestoreData")] 6 public ActionResult RestoreData() 7 { 8 var json = new JsonHelper() { Msg = "數據還原完成", Status = "n" }; 9 10 var path = Request.Form["path"]; 11 12 try 13 { 14 //檢查還原備份的物理路徑是否存在 15 if (!System.IO.File.Exists(Server.MapPath(path))) 16 { 17 json.Msg = "還原數據失敗,備份文件不存在或已損壞!"; 18 return Json(json); 19 } 20 //還原數據庫 21 using (SqlConnection Con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString)) 22 { 23 24 try 25 { 26 Con.Open(); 27 SqlCommand Com = new SqlCommand("use master restore database wkmvc_comnwes from disk='" + Server.MapPath(path) + "'", Con); 28 Com.ExecuteNonQuery(); 29 } 30 catch (Exception e) 31 { 32 throw e; 33 } 34 } 35 36 WriteLog(Common.Enums.enumOperator.None, "數據還原:" + json.Msg, Common.Enums.enumLog4net.WARN); 37 json.Status = "y"; 38 } 39 catch (Exception e) 40 { 41 json.Msg = "數據還原失敗!"; 42 WriteLog(Common.Enums.enumOperator.None, "數據還原:", e); 43 } 44 45 return Json(json); 46 }
5.2 Demo 下載:百度網盤測試
原創文章 轉載請尊重勞動成果 http://yuangang.cnblogs.com