Example #1
0
        /// <exception cref="System.InvalidOperationException">
        ///   Thrown when the previous backup operation has not finished yet.
        /// </exception>
        private BackupOperation CreateBackupOperation(Models.BackupPlan plan)
        {
            var dao = new BackupRepository();

            Models.Backup latest = dao.GetLatestByPlan(plan);
            MustResumeLastOperation = latest != null && latest.NeedsResume();

            if (MustResumeLastOperation && !Options.Resume)
            {
                string message = string.Format("The backup (#{0}) has not finished yet."
                                               + " If it's still running, please, wait until it finishes,"
                                               + " otherwise you should resume it manually.",
                                               latest.Id);
                throw new InvalidOperationException(message);
            }

            // Create new backup or resume the last unfinished one.
            BackupOperation obj = MustResumeLastOperation
                                ? new ResumeBackupOperation(latest) as BackupOperation
                                : new NewBackupOperation(plan) as BackupOperation;

            obj.Updated += (sender2, e2) => BackupUpdateStatsInfo(e2.Status, e2.TransferStatus);
            //obj.EventLog = ...
            //obj.TransferListControl = ...

            BackupUpdateStatsInfo(BackupOperationStatus.Unknown, TransferStatus.STOPPED);

            return(obj);
        }
Example #2
0
        public void OneTimeSetup()
        {
            var connectionFatory     = TestUtilities.GetIntegrationConnectionFactory();
            var generalSqlRepository = new GeneralSqlRepository();

            this.backupRepository = new BackupRepository(connectionFatory, generalSqlRepository);
        }
Example #3
0
        public OctopusAsyncRepository(IOctopusAsyncClient client, RepositoryScope repositoryScope = null)
        {
            Client                    = client;
            Scope                     = repositoryScope ?? RepositoryScope.Unspecified();
            Accounts                  = new AccountRepository(this);
            ActionTemplates           = new ActionTemplateRepository(this);
            Artifacts                 = new ArtifactRepository(this);
            Backups                   = new BackupRepository(this);
            BuiltInPackageRepository  = new BuiltInPackageRepositoryRepository(this);
            CertificateConfiguration  = new CertificateConfigurationRepository(this);
            Certificates              = new CertificateRepository(this);
            Channels                  = new ChannelRepository(this);
            CommunityActionTemplates  = new CommunityActionTemplateRepository(this);
            Configuration             = new ConfigurationRepository(this);
            DashboardConfigurations   = new DashboardConfigurationRepository(this);
            Dashboards                = new DashboardRepository(this);
            Defects                   = new DefectsRepository(this);
            DeploymentProcesses       = new DeploymentProcessRepository(this);
            Deployments               = new DeploymentRepository(this);
            Environments              = new EnvironmentRepository(this);
            Events                    = new EventRepository(this);
            FeaturesConfiguration     = new FeaturesConfigurationRepository(this);
            Feeds                     = new FeedRepository(this);
            Interruptions             = new InterruptionRepository(this);
            LibraryVariableSets       = new LibraryVariableSetRepository(this);
            Lifecycles                = new LifecyclesRepository(this);
            MachinePolicies           = new MachinePolicyRepository(this);
            MachineRoles              = new MachineRoleRepository(this);
            Machines                  = new MachineRepository(this);
            Migrations                = new MigrationRepository(this);
            OctopusServerNodes        = new OctopusServerNodeRepository(this);
            PerformanceConfiguration  = new PerformanceConfigurationRepository(this);
            PackageMetadataRepository = new PackageMetadataRepository(this);
            ProjectGroups             = new ProjectGroupRepository(this);
            Projects                  = new ProjectRepository(this);
            ProjectTriggers           = new ProjectTriggerRepository(this);
            Proxies                   = new ProxyRepository(this);
            Releases                  = new ReleaseRepository(this);
            RetentionPolicies         = new RetentionPolicyRepository(this);
            Schedulers                = new SchedulerRepository(this);
            ServerStatus              = new ServerStatusRepository(this);
            Spaces                    = new SpaceRepository(this);
            Subscriptions             = new SubscriptionRepository(this);
            TagSets                   = new TagSetRepository(this);
            Tasks                     = new TaskRepository(this);
            Teams                     = new TeamsRepository(this);
            Tenants                   = new TenantRepository(this);
            TenantVariables           = new TenantVariablesRepository(this);
            UserInvites               = new UserInvitesRepository(this);
            UserRoles                 = new UserRolesRepository(this);
            Users                     = new UserRepository(this);
            VariableSets              = new VariableSetRepository(this);
            Workers                   = new WorkerRepository(this);
            WorkerPools               = new WorkerPoolRepository(this);
            ScopedUserRoles           = new ScopedUserRoleRepository(this);
            UserPermissions           = new UserPermissionsRepository(this);

            loadRootResource      = new Lazy <Task <RootResource> >(LoadRootDocumentInner, true);
            loadSpaceRootResource = new Lazy <Task <SpaceRootResource> >(LoadSpaceRootDocumentInner, true);
        }
        public void ScheduleBackupTasks(BackupSchedulerService backupSchedulerService)
        {
            Log.DebugFormat("started to schedule backups");
            var backupsToSchedule = BackupRepository.GetBackupSchedules().Where(schedule => Schedule.IsToBeProcessed(schedule)).ToList();

            Log.DebugFormat("{0} backups are to schedule", backupsToSchedule.Count);
            foreach (var schedule in backupsToSchedule)
            {
                if (!backupSchedulerService.IsStarted)
                {
                    return;
                }
                try
                {
                    var tariff = PaymentManager.GetTariff(schedule.TenantId);
                    if (tariff.State < TariffState.Delay)
                    {
                        schedule.LastBackupTime = DateTime.UtcNow;
                        BackupRepository.SaveBackupSchedule(schedule);
                        Log.DebugFormat("Start scheduled backup: {0}, {1}, {2}, {3}", schedule.TenantId, schedule.BackupMail, schedule.StorageType, schedule.StorageBasePath);
                        BackupWorker.StartScheduledBackup(schedule);
                    }
                    else
                    {
                        Log.DebugFormat("Skip portal {0} not paid", schedule.TenantId);
                    }
                }
                catch (Exception error)
                {
                    Log.Error("error while scheduling backups: {0}", error);
                }
            }
        }
 public override void AddType(Type type)
 {
     SourceRepository.AddType(type);
     BackupRepository.AddType(type);
     ForEachWriteRepo(repo => repo.AddType(type));
     base.AddType(type);
 }
Example #6
0
        public IEnumerable <Backup> GetBackups()
        {
            var repository = new BackupRepository();
            var output     = repository.Get();

            return(output);
        }
Example #7
0
        public List <BackupHistoryRecord> GetBackupHistory(int tenantId)
        {
            var backupHistory = new List <BackupHistoryRecord>();

            foreach (var record in BackupRepository.GetBackupRecordsByTenantId(tenantId))
            {
                var storage = BackupStorageFactory.GetBackupStorage(record);
                if (storage == null)
                {
                    continue;
                }
                if (storage.IsExists(record.StoragePath))
                {
                    backupHistory.Add(new BackupHistoryRecord
                    {
                        Id          = record.Id,
                        FileName    = record.Name,
                        StorageType = record.StorageType,
                        CreatedOn   = record.CreatedOn,
                        ExpiresOn   = record.ExpiresOn
                    });
                }
                else
                {
                    BackupRepository.DeleteBackupRecord(record.Id);
                }
            }
            return(backupHistory);
        }
Example #8
0
        public void StartRestore(StartRestoreRequest request)
        {
            if ((BackupStorageType)request.StorageType == BackupStorageType.Local)
            {
                if (string.IsNullOrEmpty(request.FilePathOrId) || !File.Exists(request.FilePathOrId))
                {
                    throw new FileNotFoundException();
                }
            }

            if (!request.BackupId.Equals(Guid.Empty))
            {
                var backupRecord = BackupRepository.GetBackupRecord(request.BackupId);
                if (backupRecord == null)
                {
                    throw new FileNotFoundException();
                }

                request.FilePathOrId  = backupRecord.StoragePath;
                request.StorageType   = backupRecord.StorageType;
                request.StorageParams = JsonConvert.DeserializeObject <Dictionary <string, string> >(backupRecord.StorageParams);
            }

            var progress = BackupWorker.StartRestore(request);

            if (!string.IsNullOrEmpty(progress.Error))
            {
                throw new FaultException();
            }
        }
Example #9
0
 public void Undo()
 {
     if (Backup != null && !IsSaved)
     {
         BackupRepository daoBackup = new BackupRepository();
         daoBackup.Refresh(Backup);
     }
 }
Example #10
0
        internal void DeleteExpiredBackups(BackupCleanerService backupCleanerService)
        {
            log.Debug("started to clean expired backups");

            var backupsToRemove = BackupRepository.GetExpiredBackupRecords();

            log.DebugFormat("found {0} backups which are expired", backupsToRemove.Count);

            if (!backupCleanerService.IsStarted)
            {
                return;
            }
            foreach (var scheduledBackups in BackupRepository.GetScheduledBackupRecords().GroupBy(r => r.TenantId))
            {
                if (!backupCleanerService.IsStarted)
                {
                    return;
                }
                var schedule = BackupRepository.GetBackupSchedule(scheduledBackups.Key);
                if (schedule != null)
                {
                    var scheduledBackupsToRemove = scheduledBackups.OrderByDescending(r => r.CreatedOn).Skip(schedule.BackupsStored).ToList();
                    if (scheduledBackupsToRemove.Any())
                    {
                        log.DebugFormat("only last {0} scheduled backup records are to keep for tenant {1} so {2} records must be removed", schedule.BackupsStored, schedule.TenantId, scheduledBackupsToRemove.Count);
                        backupsToRemove.AddRange(scheduledBackupsToRemove);
                    }
                }
                else
                {
                    backupsToRemove.AddRange(scheduledBackups);
                }
            }

            foreach (var backupRecord in backupsToRemove)
            {
                if (!backupCleanerService.IsStarted)
                {
                    return;
                }
                try
                {
                    var backupStorage = BackupStorageFactory.GetBackupStorage(backupRecord);
                    if (backupStorage == null)
                    {
                        continue;
                    }

                    backupStorage.Delete(backupRecord.StoragePath);

                    BackupRepository.DeleteBackupRecord(backupRecord.Id);
                }
                catch (Exception error)
                {
                    log.Warn("can't remove backup record: {0}", error);
                }
            }
        }
Example #11
0
        public void DoBackup(string username)
        {
            var repository = new BackupRepository();

            var backupPath = ComicalConfiguration.BackupPath;
            var filename   = String.Format("Comical_{0}.bkp", DateTime.Now.ToString("yyyyMMdd_hhmmss"));

            repository.DoBackup(filename, username);
        }
Example #12
0
 public BackupCleanerHelperService(
     IOptionsMonitor <ILog> options,
     BackupRepository backupRepository,
     BackupStorageFactory backupStorageFactory)
 {
     log = options.CurrentValue;
     BackupRepository     = backupRepository;
     BackupStorageFactory = backupStorageFactory;
 }
Example #13
0
        static void Main(string[] args)
        {
            System.Console.WriteLine("Hello World!");
            var bs  = new BackupService(@"Data Source=.\SQLEXPRESS;Initial Catalog=SafetyDesign_Dev;Integrated Security=True", @"D:\_TEMP\DbBackupManager");
            var dbs = bs.GetAllUserDatabases().ToList();

            dbs.ForEach(db => System.Console.WriteLine(db));

            var br      = new BackupRepository(@"d:\_TEMP\DbBackupManager\_backups.json");
            var backups = br.GetAll();
        }
        public OctopusAsyncRepository(IOctopusAsyncClient client)
        {
            this.Client = client;

            Accounts                 = new AccountRepository(client);
            ActionTemplates          = new ActionTemplateRepository(client);
            Artifacts                = new ArtifactRepository(client);
            Backups                  = new BackupRepository(client);
            BuiltInPackageRepository = new BuiltInPackageRepositoryRepository(client);
            CertificateConfiguration = new CertificateConfigurationRepository(client);
            Certificates             = new CertificateRepository(client);
            Channels                 = new ChannelRepository(client);
            CommunityActionTemplates = new CommunityActionTemplateRepository(client);
            Configuration            = new ConfigurationRepository(client);
            DashboardConfigurations  = new DashboardConfigurationRepository(client);
            Dashboards               = new DashboardRepository(client);
            Defects                  = new DefectsRepository(client);
            DeploymentProcesses      = new DeploymentProcessRepository(client);
            Deployments              = new DeploymentRepository(client);
            Environments             = new EnvironmentRepository(client);
            Events = new EventRepository(client);
            FeaturesConfiguration = new FeaturesConfigurationRepository(client);
            Feeds                    = new FeedRepository(client);
            Interruptions            = new InterruptionRepository(client);
            LibraryVariableSets      = new LibraryVariableSetRepository(client);
            Lifecycles               = new LifecyclesRepository(client);
            MachinePolicies          = new MachinePolicyRepository(client);
            MachineRoles             = new MachineRoleRepository(client);
            Machines                 = new MachineRepository(client);
            Migrations               = new MigrationRepository(client);
            OctopusServerNodes       = new OctopusServerNodeRepository(client);
            PerformanceConfiguration = new PerformanceConfigurationRepository(client);
            ProjectGroups            = new ProjectGroupRepository(client);
            Projects                 = new ProjectRepository(client);
            ProjectTriggers          = new ProjectTriggerRepository(client);
            Proxies                  = new ProxyRepository(client);
            Releases                 = new ReleaseRepository(client);
            RetentionPolicies        = new RetentionPolicyRepository(client);
            Schedulers               = new SchedulerRepository(client);
            ServerStatus             = new ServerStatusRepository(client);
            Subscriptions            = new SubscriptionRepository(client);
            TagSets                  = new TagSetRepository(client);
            Tasks                    = new TaskRepository(client);
            Teams                    = new TeamsRepository(client);
            Tenants                  = new TenantRepository(client);
            TenantVariables          = new TenantVariablesRepository(client);
            UserRoles                = new UserRolesRepository(client);
            Users                    = new UserRepository(client);
            VariableSets             = new VariableSetRepository(client);
            Workers                  = new WorkerRepository(client);
            WorkerPools              = new WorkerPoolRepository(client);
        }
Example #15
0
 public BackupService(
     IOptionsMonitor <ILog> options,
     BackupStorageFactory backupStorageFactory,
     BackupWorker backupWorker,
     BackupRepository backupRepository,
     ConfigurationExtension configuration)
 {
     Log = options.CurrentValue;
     BackupStorageFactory = backupStorageFactory;
     BackupWorker         = backupWorker;
     BackupRepository     = backupRepository;
     Configuration        = configuration;
 }
Example #16
0
 public SaveApplicationDataCommand(
     TaskListViewModel taskListViewModel,
     DoneListViewModel doneListViewModel,
     SettingsViewModel settingsViewModel,
     FileDataRepository fileDataRepository,
     BackupRepository backupRepository)
 {
     _taskListViewModel  = taskListViewModel;
     _doneListViewModel  = doneListViewModel;
     _settingsViewModel  = settingsViewModel;
     _fileDataRepository = fileDataRepository;
     _backupRepository   = backupRepository;
 }
 public BackupSchedulerServiceHelper(
     IOptionsMonitor <ILog> options,
     PaymentManager paymentManager,
     BackupWorker backupWorker,
     BackupRepository backupRepository,
     Schedule schedule)
 {
     PaymentManager   = paymentManager;
     BackupWorker     = backupWorker;
     BackupRepository = backupRepository;
     Schedule         = schedule;
     Log = options.CurrentValue;
 }
Example #18
0
        private void SaveDtoToBackupRepository(object dtoInstance)
        {
            object existing = BackupRepository.Retrieve(dtoInstance.GetType(), dtoInstance.Property <string>("Uuid"));

            if (existing != null)
            {
                BackupRepository.Save(dtoInstance);
            }
            else
            {
                BackupRepository.Create(dtoInstance);
            }
        }
Example #19
0
        public void DeleteBackup(Guid id)
        {
            var backupRecord = BackupRepository.GetBackupRecord(id);

            BackupRepository.DeleteBackupRecord(backupRecord.Id);

            var storage = BackupStorageFactory.GetBackupStorage(backupRecord);

            if (storage == null)
            {
                return;
            }
            storage.Delete(backupRecord.StoragePath);
        }
Example #20
0
 public void CreateSchedule(CreateScheduleRequest request)
 {
     BackupRepository.SaveBackupSchedule(
         new BackupSchedule()
     {
         TenantId        = request.TenantId,
         Cron            = request.Cron,
         BackupMail      = request.BackupMail,
         BackupsStored   = request.NumberOfBackupsStored,
         StorageType     = request.StorageType,
         StorageBasePath = request.StorageBasePath,
         StorageParams   = JsonConvert.SerializeObject(request.StorageParams)
     });
 }
Example #21
0
        public async Task <FileVersionerResults> DoVersion(Models.Backup backup, LinkedList <string> filePaths, bool newVersion)
        {
            Assert.IsNotNull(backup);
            Assert.AreEqual(TransferStatus.RUNNING, backup.Status);
            Assert.IsNotNull(filePaths);

            Results.Reset();

            await ExecuteOnBackround(() =>
            {
                ISession session = NHibernateHelper.GetSession();
                try
                {
                    BackupRepository daoBackup = new BackupRepository(session);
                    BackupPlanFileRepository daoBackupPlanFile = new BackupPlanFileRepository(session);

                    Backup = daoBackup.Get(backup.Id);

                    IList <Models.BackupPlanFile> list = newVersion
                                                ? daoBackupPlanFile.GetAllByBackupPlan(Backup.BackupPlan)
                                                : daoBackupPlanFile.GetAllPendingByBackup(Backup);

                    AllFilesFromPlan = list.ToDictionary <Models.BackupPlanFile, string>(p => p.Path);

                    Execute(Backup, filePaths, newVersion);

                    Save(session);
                }
                catch (Exception ex)
                {
                    string message = string.Format("File versioning FAILED with an exception: {0}", ex.Message);

                    Results.OnError(this, message);
                    logger.Log(LogLevel.Error, ex, message);

                    throw ex;
                }
                finally
                {
                    //session.Close();
                    if (session.IsConnected)
                    {
                        session.Disconnect();
                    }
                }
            }, CancellationToken);

            return(Results);
        }
Example #22
0
        public async Task OneTimeSetup()
        {
            var generalSqlRepository = new GeneralSqlRepository();

            this.backupRepository = new BackupRepository(ConnectionFactorySetup.ConnectionFactory, generalSqlRepository);

            // Run full and diff backups
            await this.backupRepository.RunBackupAsync(BackupType.Full, Path.GetTempFileName());

            await this.backupRepository.RunBackupAsync(BackupType.Differential, Path.GetTempFileName());

            // Get the database name for queries (note: with localdb unit platform test implementation the database name probably has a random suffix)
            using (var connection = ConnectionFactorySetup.ConnectionFactory.GetEddsPerformanceConnection())
            {
                this.databaseName = new SqlConnectionStringBuilder(connection.ConnectionString).InitialCatalog;
            }
        }
 public BackupSchedulerServiceHelper(
     IOptionsMonitor <ILog> options,
     PaymentManager paymentManager,
     BackupWorker backupWorker,
     BackupRepository backupRepository,
     Schedule schedule,
     TenantManager tenantManager,
     CoreBaseSettings coreBaseSettings)
 {
     PaymentManager   = paymentManager;
     BackupWorker     = backupWorker;
     BackupRepository = backupRepository;
     Schedule         = schedule;
     TenantManager    = tenantManager;
     CoreBaseSettings = coreBaseSettings;
     Log = options.CurrentValue;
 }
Example #24
0
        private HashSet <OldToNewIdMapping> RestoreData(Database restoreTo, ILogger logger)
        {
            HashSet <OldToNewIdMapping> result = new HashSet <OldToNewIdMapping>();

            DatabaseToRestoreTo = restoreTo;
            DatabaseToRestoreTo.TryEnsureSchema(DaoTypes.First(), logger);

            Dictionary <string, Dao> tempForeignKeyTargets = InsertTempForeignKeyTargets();

            // for all the poco types load them all from the repo
            foreach (Type pocoType in BackupRepository.StorableTypes)
            {
                // copy the poco as a dao and save it into the restoreTo
                IEnumerable <object> all = BackupRepository.RetrieveAll(pocoType);
                Type daoType             = DaoTypes.FirstOrDefault(t => t.Name.Equals(pocoType.Name));
                Args.ThrowIf <InvalidOperationException>(daoType == null, "The Dto of type {0} didn't have a corresponding Dao type", pocoType.Name);

                foreach (object poco in all)
                {
                    string uuid = Meta.GetUuid(poco, true);
                    Dao    dao  = (Dao)poco.CopyAs(daoType);
                    dao.IdValue              = null;
                    dao.DataRow              = null;
                    dao.ForceInsert          = true;
                    dao.UniqueFilterProvider = () =>
                    {
                        return(Query.Where("Uuid") == uuid);
                    };
                    ForceUpdateIfExistsInTarget(uuid, dao);
                    SetTemporaryForeignKeys(dao, tempForeignKeyTargets);

                    dao.Save(DatabaseToRestoreTo);
                    OldToNewIdMapping idMapping = new OldToNewIdMapping
                    {
                        PocoType = pocoType,
                        DaoType  = daoType,
                        OldId    = (long)poco.Property("Id"),
                        NewId    = (long)dao.IdValue,
                        Uuid     = uuid
                    };

                    result.Add(idMapping);
                }
            }
            return(result);
        }
Example #25
0
 private void Backup(Database database, Dao dao)
 {
     if (database == SourceDatabase)
     {
         BackupTask = Task.Run(() =>
         {
             object dtoInstance = Dto.Copy(dao);
             object existing    = BackupRepository.Retrieve(dtoInstance.GetType(), dtoInstance.Property <string>("Uuid"));
             if (existing != null)
             {
                 BackupRepository.Save(dtoInstance);
             }
             else
             {
                 BackupRepository.Create(dtoInstance);
             }
         });
         BackupTask.ConfigureAwait(false);
     }
 }
Example #26
0
 public void DeleteAllBackups(int tenantId)
 {
     foreach (var backupRecord in BackupRepository.GetBackupRecordsByTenantId(tenantId))
     {
         try
         {
             BackupRepository.DeleteBackupRecord(backupRecord.Id);
             var storage = BackupStorageFactory.GetBackupStorage(backupRecord);
             if (storage == null)
             {
                 continue;
             }
             storage.Delete(backupRecord.StoragePath);
         }
         catch (Exception error)
         {
             Log.Warn("error while removing backup record: {0}", error);
         }
     }
 }
Example #27
0
        public ScheduleResponse GetSchedule(int tenantId)
        {
            var schedule = BackupRepository.GetBackupSchedule(tenantId);

            if (schedule != null)
            {
                var tmp = new ScheduleResponse
                {
                    StorageType           = schedule.StorageType,
                    StorageBasePath       = schedule.StorageBasePath,
                    BackupMail            = schedule.BackupMail,
                    NumberOfBackupsStored = schedule.BackupsStored,
                    Cron           = schedule.Cron,
                    LastBackupTime = schedule.LastBackupTime,
                    StorageParams  = JsonConvert.DeserializeObject <Dictionary <string, string> >(schedule.StorageParams)
                };
                return(tmp);
            }
            else
            {
                return(null);
            }
        }
Example #28
0
        private void Save(ISession session)
        {
            Assert.IsFalse(IsSaved);

            BatchProcessor               batchProcessor        = new BatchProcessor();
            BackupRepository             daoBackup             = new BackupRepository(session);
            BackupPlanFileRepository     daoBackupPlanFile     = new BackupPlanFileRepository(session);
            BackupedFileRepository       daoBackupedFile       = new BackupedFileRepository(session);
            BackupPlanPathNodeRepository daoBackupPlanPathNode = new BackupPlanPathNodeRepository(session);

#if false
            var FilesToTrack          = SuppliedFiles.Union(ChangeSet.DeletedFiles);
            var FilesToInsertOrUpdate =
                from f in FilesToTrack
                where
                // Keep it so we'll later add or update a `BackupedFile`.
                ((f.LastStatus == Models.BackupFileStatus.ADDED || f.LastStatus == Models.BackupFileStatus.MODIFIED))
                // Keep it if `LastStatus` is different from `PreviousLastStatus`.
                || ((f.LastStatus == Models.BackupFileStatus.REMOVED || f.LastStatus == Models.BackupFileStatus.DELETED) && (f.LastStatus != f.PreviousLastStatus))
                // Skip all UNCHANGED files.
                select f;
#else
            var FilesToTrack          = SuppliedFiles;
            var FilesToInsertOrUpdate =
                from f in FilesToTrack
                where
                // Keep it so we'll later add or update a `BackupedFile`.
                ((f.LastStatus == Models.BackupFileStatus.ADDED || f.LastStatus == Models.BackupFileStatus.MODIFIED))
                // Skip all UNCHANGED/DELETED/REMOVED files.
                select f;
#endif

            BlockPerfStats stats = new BlockPerfStats();

            using (ITransaction tx = session.BeginTransaction())
            {
                try
                {
                    // ------------------------------------------------------------------------------------

                    stats.Begin("STEP 1");

                    BackupPlanPathNodeCreator pathNodeCreator = new BackupPlanPathNodeCreator(daoBackupPlanPathNode, tx);

                    // 1 - Split path into its components and INSERT new path nodes if they don't exist yet.
                    foreach (Models.BackupPlanFile entry in FilesToInsertOrUpdate)
                    {
                        // Throw if the operation was canceled.
                        CancellationToken.ThrowIfCancellationRequested();

                        try
                        {
                            entry.PathNode = pathNodeCreator.CreateOrUpdatePathNodes(Backup.BackupPlan.StorageAccount, entry);
                        }
                        catch (Exception ex)
                        {
                            string message = string.Format("BUG: Failed to create/update {0} => {1}",
                                                           typeof(Models.BackupPlanPathNode).Name,
                                                           CustomJsonSerializer.SerializeObject(entry, 1));

                            Results.OnError(this, message);
                            logger.Log(LogLevel.Error, ex, message);

                            throw;
                        }

                        batchProcessor.ProcessBatch(session);
                    }

                    batchProcessor.ProcessBatch(session, true);
                    stats.End();

                    // ------------------------------------------------------------------------------------

                    stats.Begin("STEP 2");

                    // 2 - Insert/Update `BackupPlanFile`s as necessary.
                    foreach (Models.BackupPlanFile entry in FilesToInsertOrUpdate)
                    {
                        // Throw if the operation was canceled.
                        CancellationToken.ThrowIfCancellationRequested();

                        // IMPORTANT: It's important that we guarantee the referenced `BackupPlanFile` has a valid `Id`
                        // before we reference it elsewhere, otherwise NHibernate won't have a valid value to put on
                        // the `backup_plan_file_id` column.
                        try
                        {
                            daoBackupPlanFile.InsertOrUpdate(tx, entry);                             // Guarantee it's saved
                        }
                        catch (Exception ex)
                        {
                            string message = string.Format("BUG: Failed to insert/update {0} => {1}",
                                                           typeof(Models.BackupPlanFile).Name,
                                                           CustomJsonSerializer.SerializeObject(entry, 1));

                            Results.OnError(this, message);
                            logger.Log(LogLevel.Error, ex, message);

                            logger.Error("Dump of failed object: {0}", entry.DumpMe());
                            throw;
                        }

                        batchProcessor.ProcessBatch(session);
                    }

                    batchProcessor.ProcessBatch(session, true);
                    stats.End();

                    // ------------------------------------------------------------------------------------

                    stats.Begin("STEP 3");

                    // 3 - Insert/Update `BackupedFile`s as necessary and add them to the `Backup`.
                    //List<Models.BackupedFile> backupedFiles = new List<Models.BackupedFile>(FilesToInsertOrUpdate.Count());

                    foreach (Models.BackupPlanFile entry in FilesToInsertOrUpdate)
                    {
                        // Throw if the operation was canceled.
                        CancellationToken.ThrowIfCancellationRequested();

                        Models.BackupedFile backupedFile = daoBackupedFile.GetByBackupAndPath(Backup, entry.Path);
                        if (backupedFile == null)                         // If we're resuming, this should already exist.
                        {
                            // Create `BackupedFile`.
                            backupedFile = new Models.BackupedFile(Backup, entry);
                        }
                        backupedFile.FileSize          = entry.LastSize;
                        backupedFile.FileStatus        = entry.LastStatus;
                        backupedFile.FileLastWrittenAt = entry.LastWrittenAt;
                        backupedFile.FileLastChecksum  = entry.LastChecksum;
                        switch (entry.LastStatus)
                        {
                        default:
                            backupedFile.TransferStatus = default(TransferStatus);
                            break;

                        case Models.BackupFileStatus.REMOVED:
                        case Models.BackupFileStatus.DELETED:
                            backupedFile.TransferStatus = TransferStatus.COMPLETED;
                            break;
                        }
                        backupedFile.UpdatedAt = DateTime.UtcNow;

                        try
                        {
                            daoBackupedFile.InsertOrUpdate(tx, backupedFile);
                        }
                        catch (Exception ex)
                        {
                            logger.Log(LogLevel.Error, ex, "BUG: Failed to insert/update {0} => {1}",
                                       typeof(Models.BackupedFile).Name,
                                       CustomJsonSerializer.SerializeObject(backupedFile, 1));
                            throw;
                        }

                        //backupedFiles.Add(backupedFile);

                        batchProcessor.ProcessBatch(session);
                    }

                    batchProcessor.ProcessBatch(session, true);
                    stats.End();

                    // ------------------------------------------------------------------------------------

                    stats.Begin("STEP 4");

                    // 4 - Update all `BackupPlanFile`s that already exist for the backup plan associated with this backup operation.
                    {
                        var AllFilesFromPlanThatWerentUpdatedYet = AllFilesFromPlan.Values.Except(FilesToInsertOrUpdate);
                        foreach (Models.BackupPlanFile file in AllFilesFromPlanThatWerentUpdatedYet)
                        {
                            // Throw if the operation was canceled.
                            CancellationToken.ThrowIfCancellationRequested();

                            //Console.WriteLine("2: {0}", file.Path);
                            try
                            {
                                daoBackupPlanFile.Update(tx, file);
                            }
                            catch (Exception ex)
                            {
                                string message = string.Format("BUG: Failed to update {0} => {1} ",
                                                               typeof(Models.BackupPlanFile).Name,
                                                               CustomJsonSerializer.SerializeObject(file, 1));

                                Results.OnError(this, message);
                                logger.Log(LogLevel.Error, ex, message);

                                throw;
                            }

                            batchProcessor.ProcessBatch(session);
                        }
                    }

                    batchProcessor.ProcessBatch(session, true);
                    stats.End();

                    // ------------------------------------------------------------------------------------

                    stats.Begin("STEP 5");

                    // 5 - Insert/Update `Backup` and its `BackupedFile`s into the database, also saving
                    //     the `BackupPlanFile`s instances that may have been changed by step 2.
                    {
                        //foreach (var bf in backupedFiles)
                        //{
                        //	// Throw if the operation was canceled.
                        //	CancellationToken.ThrowIfCancellationRequested();
                        //
                        //	Backup.Files.Add(bf);
                        //
                        //	ProcessBatch(session);
                        //}

                        try
                        {
                            daoBackup.Update(tx, Backup);
                        }
                        catch (Exception ex)
                        {
                            string message = string.Format("BUG: Failed to update {0} => {1}",
                                                           typeof(Models.Backup).Name,
                                                           CustomJsonSerializer.SerializeObject(Backup, 1));

                            Results.OnError(this, message);
                            logger.Log(LogLevel.Error, ex, message);

                            throw;
                        }
                    }

                    batchProcessor.ProcessBatch(session, true);
                    stats.End();

                    // ------------------------------------------------------------------------------------

                    tx.Commit();
                }
                catch (OperationCanceledException)
                {
                    string message = "Operation cancelled";

                    Results.OnError(this, message);
                    logger.Warn(message);

                    tx.Rollback();                     // Rollback the transaction
                    throw;
                }
                catch (Exception ex)
                {
                    string message = string.Format("Caught Exception: {0}", ex.Message);

                    Results.OnError(this, message);
                    logger.Log(LogLevel.Error, ex, message);

                    tx.Rollback();                     // Rollback the transaction
                    throw;
                }
                finally
                {
                    // ...
                }
            }

            IsSaved = true;

            // 6 - Create versioned files and remove files that won't belong to this backup.
            TransferSet.Files = GetFilesToTransfer(Backup, SuppliedFiles);

            // Test to see if things are okay!
            {
                var transferCount = TransferSet.Files.Count();
                var filesCount    = ChangeSet.AddedFiles.Count() + ChangeSet.ModifiedFiles.Count();

                Assert.IsTrue(transferCount == filesCount, "TransferSet.Files must be equal (ChangeSet.AddedFiles + ChangeSet.ModifiedFiles)");
            }
        }
Example #29
0
 public BackupManager()
 {
     _backupRepository = new BackupRepository();
 }
Example #30
0
        public void DoRestore(int backupId)
        {
            var repository = new BackupRepository();

            repository.DoRestore(backupId);
        }