static void Main(string[] args) { System.Diagnostics.Debugger.Launch(); var options = new Options(); Parser.Default.ParseArgumentsStrict(args, options); if (!string.IsNullOrWhiteSpace(options.Restore)) { var webconfig = ToAbsolute(options.WebConfigPath ?? Path.Combine("..", "..", "WebStudio", "web.config")); var backupfile = ToAbsolute(options.Restore); var backuper = new BackupManager(backupfile, webconfig); backuper.ProgressChanged += (s, e) => { var pos = System.Console.CursorTop; System.Console.SetCursorPosition(0, System.Console.CursorTop); System.Console.Write(new string(' ', System.Console.WindowWidth)); System.Console.SetCursorPosition(0, pos); System.Console.Write("{0}... {1}%", e.Status, e.Progress); }; System.Console.WriteLine(); backuper.Load(); System.Console.WriteLine(); } else { options.WebConfigPath = ToAbsolute(options.WebConfigPath); options.BackupDirectory = ToAbsolute(options.BackupDirectory); var log = LogManager.GetLogger("ASC"); if (!Path.HasExtension(options.WebConfigPath)) { options.WebConfigPath = Path.Combine(options.WebConfigPath, "web.config"); } if (!File.Exists(options.WebConfigPath)) { log.Error("Configuration file not found."); return; } if (!Directory.Exists(options.BackupDirectory)) { Directory.CreateDirectory(options.BackupDirectory); } foreach (var tenant in options.TenantIds.Select(tenantId => CoreContext.TenantManager.GetTenant(tenantId)).Where(tenant => tenant != null)) { var backupFileName = string.Format("{0}-{1:yyyyMMddHHmmss}.zip", tenant.TenantAlias, DateTime.UtcNow); var backupFilePath = Path.Combine(options.BackupDirectory, backupFileName); var task = new BackupPortalTask(log, tenant.TenantId, options.WebConfigPath, backupFilePath, 100); task.RunJob(); } } }
public void RunJob() { if (ThreadPriority.BelowNormal < Thread.CurrentThread.Priority) { Thread.CurrentThread.Priority = ThreadPriority.BelowNormal; } var backupName = string.Format("{0}_{1:yyyy-MM-dd_HH-mm-ss}.{2}", CoreContext.TenantManager.GetTenant(TenantId).TenantAlias, DateTime.UtcNow, ArchiveFormat); var tempFile = Path.Combine(tempFolder, backupName); try { var backupTask = new BackupPortalTask(Log, TenantId, configPaths[currentRegion], tempFile); if (!BackupMail) { backupTask.IgnoreModule(ModuleName.Mail); } backupTask.IgnoreTable("tenants_tariff"); backupTask.ProgressChanged += (sender, args) => Percentage = 0.9 * args.Progress; backupTask.RunJob(); var backupStorage = BackupStorageFactory.GetBackupStorage(StorageType, TenantId); var storagePath = backupStorage.Upload(StorageBasePath, tempFile, UserId); Link = backupStorage.GetPublicLink(storagePath); var repo = BackupStorageFactory.GetBackupRepository(); repo.SaveBackupRecord( new BackupRecord { Id = (Guid)Id, TenantId = TenantId, IsScheduled = IsScheduled, FileName = Path.GetFileName(tempFile), StorageType = StorageType, StorageBasePath = StorageBasePath, StoragePath = storagePath, CreatedOn = DateTime.UtcNow, ExpiresOn = StorageType == BackupStorageType.DataStore ? DateTime.UtcNow.AddDays(1) : DateTime.MinValue }); Percentage = 100; if (UserId != Guid.Empty && !IsScheduled) { NotifyHelper.SendAboutBackupCompleted(TenantId, UserId, Link); } IsCompleted = true; } catch (Exception error) { Log.Error("RunJob - Params: {0}, Error = {1}", new { Id = Id, Tenant = TenantId, File = tempFile, BasePath = StorageBasePath, }, error); Error = error; IsCompleted = true; } finally { try { File.Delete(tempFile); } catch (Exception error) { Log.Error("can't delete file: {0}", error); } } }