Beispiel #1
0
        public static bool TryExtractDateFromFileName(string filePath, out DateTime lastModified)
        {
            // file name format: 2017-06-01-00-00-00
            // legacy incremental backup format: 2017-06-01-00-00-00-0

            var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath);
            var match = FileNameRegex.Match(fileNameWithoutExtension);

            if (match.Success)
            {
                fileNameWithoutExtension = match.Value;
            }

            if (DateTime.TryParseExact(
                    fileNameWithoutExtension,
                    BackupTask.GetDateTimeFormat(fileNameWithoutExtension),
                    CultureInfo.InvariantCulture,
                    DateTimeStyles.None,
                    out lastModified) == false)
            {
                return(false);
            }

            return(true);
        }
        private bool UpdateFoldersToDelete(GetFoldersResult folders, DateTime now, List <string> foldersToDelete)
        {
            var firstDateInRetentionRange = now - _retentionPolicy.MinimumBackupAgeToKeep.Value;

            foreach (var folder in folders.List)
            {
                CancellationToken.ThrowIfCancellationRequested();

                var folderName    = GetFolderName(folder);
                var folderDetails = RestorePointsBase.ParseFolderName(folderName);
                if (folderDetails.BackupTimeAsString == null)
                {
                    if (Logger.IsInfoEnabled)
                    {
                        Logger.Info($"Failed to get backup date time for folder: {folder}");
                    }
                    continue;
                }

                if (DateTime.TryParseExact(
                        folderDetails.BackupTimeAsString,
                        BackupTask.GetDateTimeFormat(folderDetails.BackupTimeAsString),
                        CultureInfo.InvariantCulture,
                        DateTimeStyles.None,
                        out var backupTime) == false)
                {
                    if (Logger.IsInfoEnabled)
                    {
                        Logger.Info($"Failed to parse backup date time for folder: {folder}");
                    }
                    continue;
                }

                if (now - backupTime < _retentionPolicy.MinimumBackupAgeToKeep)
                {
                    // all backups are sorted by date
                    return(false);
                }

                if (string.Equals(folderDetails.DatabaseName, _databaseName, StringComparison.OrdinalIgnoreCase) == false)
                {
                    continue; // a backup for a different database
                }
                var backupFiles = GetBackupFilesInFolder(folder, firstDateInRetentionRange);
                if (backupFiles == null)
                {
                    continue; // folder is empty
                }
                var hasFullBackupOrSnapshot = BackupUtils.IsFullBackupOrSnapshot(backupFiles.FirstFile);
                if (hasFullBackupOrSnapshot == false)
                {
                    continue; // no snapshot or full backup
                }
                if (GotFreshIncrementalBackup(backupFiles, now))
                {
                    continue;
                }

                foldersToDelete.Add(folder);
            }

            return(true);
        }