public bool UpdateDeletedFlagBackupHistory(int backupID, string userName) { try { BackupHistory backupData = dbContext.BackupHistories.Find(backupID); backupData.Deleted = true; backupData.DeleteDate = DateTime.Now; backupData.DeleteBy = userName; try { dbContext.SaveChanges(); return(true); } catch (Exception ex) { log.Error(String.Format("Exception : {0}", ex.StackTrace)); return(false); } } catch (Exception ex) { log.Error(String.Format("Exception : {0}", ex.StackTrace)); return(false); } }
private async Task WriteHistoryAsync(VirtualMachine vm, BackupState bs) { var history = await _context.History.FirstOrDefaultAsync(h => h.VirtualMachine == vm && h.BackupDateStart == bs.BackupStartDate); if (history == null) { history = new BackupHistory(); _context.History.Add(history); } history.VirtualMachine = vm; history.LastKnownStatus = bs.Status.ToString(); history.ExportedToFolder = bs.ExportedToFolder; history.ArchivedToFile = bs.ArchivedToFile; if (bs.Status == BackupJobStatus.Completed) { history.Success = true; } else { history.Success = false; } if (bs.BackupStartDate.HasValue) { history.BackupDateStart = bs.BackupStartDate.Value; } history.BackupDateEnd = bs.BackupEndDate; }
public int AddBackupHistory(BackupHistoryModel backupModel) { int backupID = 0; try { BackupHistory backupData = new BackupHistory(); backupData.BackupName = backupModel.BackupName; backupData.CreateBy = backupModel.BackupBy; backupData.CreateDate = backupModel.BackupDate; dbContext.BackupHistories.Add(backupData); try { dbContext.SaveChanges(); backupID = backupData.BackupID; } catch (Exception ex) { log.Error(String.Format("Exception : {0}", ex.StackTrace)); } return(backupID); } catch (Exception ex) { log.Error(String.Format("Exception : {0}", ex.StackTrace)); return(backupID); } }
/// <summary> /// Backup deletion logic, deletes a database backup represented as a <see cref="BackupHistory"/>. /// Deletes the recorded archive file then marks the <see cref="BackupHistory"/> entity for deletion in <see cref="ddb"/> /// </summary> /// <param name="ddb">An instance of <see cref="SSBTDbContext"/></param> /// <param name="b">The <see cref="BackupHistory"/> instance to delete</param> /// <param name="logger">Optional instance of a <see cref="Logger"/></param> /// <returns>true if everything went good, false otherwise.</returns> /// <remarks>If the database backup archive fails to be deleted for any reason, a log will be written but the <see cref="b"/> won't be marked for deletion.</remarks> public static bool DeleteBackup([NotNull] SSBTDbContext ddb, BackupHistory b, [CanBeNull] Logger logger) { if (ddb == null) { throw new ArgumentNullException("ddb"); } try { File.Delete(b.Path); } catch (Exception ex) { if (logger != null) { logger.ErrorException(String.Format("During backup file deletion '{0}'", b.Path), ex); } return(false); } ddb.History.Remove(b); return(true); }
/// <summary> /// Backup implementation logic, asks SQLServer to make a backup, of <see cref="dbName"/>, creates a Zip archive on success, tries to delete the original backup file. /// </summary> /// <param name="coString">Connection string to use to instruct SQLServer to make the backup</param> /// <param name="dbName">Name of the database to backup</param> /// <param name="principal"></param> /// <param name="logger">Optional instance of a <see cref="Logger"/></param> /// <returns>An instance of <see cref="BackupHistory"/> on success, null otherwise</returns> /// <remarks>If deletion of the original backup file fails for any reason, only a log entry will be done, the backup won't be considered as failed. /// This methods needs to be run in a valid <see cref="HttpContext"/>. /// </remarks> public static async Task <BackupHistory> BackupDatabase([NotNull] string coString, [NotNull] string dbName, [CanBeNull] Logger logger) { if (string.IsNullOrWhiteSpace(coString)) { throw new ArgumentNullException("coString"); } if (string.IsNullOrWhiteSpace(dbName)) { throw new ArgumentNullException("dbName"); } var httpContext = HttpContext.Current; if (httpContext == null) { throw new Exception("HttpContext.Current returned null"); } var server = httpContext.Server; var user = httpContext.User; using (var bak = new SqlServerBackupProvider(coString)) { await bak.OpenAsync(); var ts = DateTime.Now; var backupsPath = server.MapPath("~/Backups"); var fNameBase = Utils.GenerateBackupBaseName(dbName, ts); var fullBackupPath = Path.Combine(backupsPath, string.Format("{0}.bak", fNameBase)); try { await bak.BackupDatabaseAsync(dbName, fullBackupPath, ts); } catch (Exception ex) { if (logger != null) { logger.ErrorException("During database backup", ex); } return(null); } var fullZipPath = Path.Combine(backupsPath, string.Format("{0}.zip", fNameBase)); try { using (var z = new ZipFile(fullZipPath)) { z.AddFile(fullBackupPath, string.Empty); await Task.Run(() => z.Save()); } } catch (Exception ex) { if (logger != null) { logger.ErrorException("During zip file creation", ex); } return(null); } try { File.Delete(fullBackupPath); } catch (Exception ex) { if (logger != null) { logger.ErrorException("During original file deletion", ex); } } var fInfo = new FileInfo(fullZipPath); var h = new BackupHistory { Path = fullZipPath, Database = dbName, Url = string.Format("~/{0}", fullZipPath.Replace(server.MapPath("~/"), string.Empty).Replace('\\', '/')), Expires = DateTime.Now.AddDays(1), Username = user.Identity.Name, Size = fInfo.Length, }; using (var ddb = new SSBTDbContext()) { ddb.History.Add(h); ddb.SaveChanges(); } return(h); } }
private void backupHistoryToolStripMenuItem2_Click(object sender, EventArgs e) { BackupHistory BackupHistory = new BackupHistory(); BackupHistory.Show(); }
private void ViewLogs_Click(object sender, EventArgs e) { BackupHistory BackupHistory = new BackupHistory(); BackupHistory.Show(); }