文章内容

2016/12/14 10:31:22,作 者: 黄兵

程序与数据备份

一、程序的备份

我们首先在ComManage区域下面新建一个控制器BackupRestoreController 继承 BaseController

1.1 Index视图 我们列出所有的备份文件(包含程序文件和数据备份文件)

1.2 我们添加这个视图

1.3 获取文件列表的方法

 /// <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         }

 

1.4 程序备份,我们把备份的程序文件放到/App_Data/BackUp/ApplicationBackUp 目录下

我们用zip压缩和解压,注意的一点就是 我们压缩的文件是存储在App_Data目录下,所以压缩的时候要排除这个目录,不然正在写入这个目录同时压缩这个目录会冲突。

压缩方法 参考:

 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.ToStrin                   g("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.5 数据备份,备份的bak文件我们存在/App_Data/BackUp/DataBaseBackUp/目录下

/// <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.6 数据还原 程序文件的还原 就是解压文件 到指定目录 关于解压文件 大家参考:

数据库备份文件的还原

/// <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         }

转载自:http://yuangang.cnblogs.com

分享到:

发表评论

评论列表