Beispiel #1
0
        /// <summary>
        /// 备份数据库
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool BackDb(Entity.SysDbBack entity, out string msg)
        {
            msg = "ok";
            if (entity == null)
            {
                msg = "实体为空";
                return(false);
            }
            var site_config = Tools.ConfigHelper.GetSiteModel();

            if (!System.IO.Directory.Exists(site_config.DbBackPath))
            {
                try
                {
                    System.IO.Directory.CreateDirectory(site_config.DbBackPath);
                }
                catch (Exception ex)
                {
                    msg = "创建数据库备份目录失败:" + ex.Message;
                }
            }
            string is_diff   = entity.BackType == Entity.SysDbBackType.diff ? " WITH DIFFERENTIAL " : "";
            string back_path = site_config.DbBackPath + entity.DbName + "_" + entity.BackName + ".bak";
            string backSql   = string.Format("BACKUP DATABASE {0} to DISK = '{1}' {2};", entity.DbName, back_path, is_diff);

            BLL.BaseBLL <Entity.SysDbBack> bll = new BaseBLL <Entity.SysDbBack>();
            entity.AddTime  = DateTime.Now;
            entity.FilePath = back_path;
            bll.Add(entity);
            using (var db = new DataCore.EFDBContext())
            {
                db.Database.ExecuteSqlCommand(System.Data.Entity.TransactionalBehavior.DoNotEnsureTransaction, backSql);
            }
            return(true);
        }
 public ActionResult Add(Entity.SysDbBack entity)
 {
     LoadType();
     if (entity.BackType == 0)
     {
         ModelState.AddModelError("BackType", "备份类别必选");
     }
     if (ModelState.IsValid)
     {
         string           msg = "";
         BLL.BLLSysDbBack bll = new BLL.BLLSysDbBack();
         entity.AddTime          = DateTime.Now;
         entity.LastUpdateTime   = DateTime.Now;
         entity.AddUserID        = WorkContext.UserInfo.ID;
         entity.LastUpdateUserID = WorkContext.UserInfo.ID;
         bool is_ok = bll.BackDb(entity, out msg);
         if (is_ok)
         {
             AddAdminLogs(Entity.SysLogMethodType.Add, "新增备份的数据库:" + entity.FilePath);
             return(PromptView("/admin/SysDbBack", "OK", "Success", "备份成功", 5));
         }
         else
         {
             return(PromptView("/admin/SysDbBack", "404", "Error", msg, 10));
         }
     }
     return(View(entity));
 }
 public ActionResult Add()
 {
     LoadType();
     Entity.SysDbBack entity = new Entity.SysDbBack();
     entity.BackName = DateTime.Now.ToString("yyyyMMdd") + WebHelper.GenerateRandomIntNumber(4);
     return(View(entity));
 }
Beispiel #4
0
 /// <summary>
 /// 还原数据库
 /// </summary>
 /// <param name="entity"></param>
 /// <param name="msg"></param>
 /// <returns></returns>
 public bool RestoreDb(Entity.SysDbBack entity, out string msg)
 {
     msg = "ok";
     if (entity == null)
     {
         msg = "实体为空";
         return(false);
     }
     try
     {
         using (var db = new DataCore.EFDBContext())
         {
             string restore_sql = string.Format("use master;  ALTER DATABASE [{0}] SET OFFLINE WITH ROLLBACK IMMEDIATE;RESTORE DATABASE {1} FROM DISK='{2}'WITH replace", entity.DbName, entity.DbName, entity.FilePath);
             db.Database.ExecuteSqlCommand(System.Data.Entity.TransactionalBehavior.DoNotEnsureTransaction, restore_sql);
         }
     }
     catch (Exception ex)
     {
         msg = "还原数据库出错:" + ex.Message;
         return(false);
     }
     return(true);
 }