示例#1
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);
                }
            }
        }