Backups are deleted due to a bad condition on the limit

Created on 5 January 2024, 6 months ago
Updated 22 January 2024, 5 months ago

Problem/Motivation

The problem became to try to remove first the older backups, the foreach removes all backups until reaches the limit, but it should remove only the backups in this way if the number of backups is bigger than the limit and the limit should be calculated taking into account the current number of backups.

  case 'number_to_keep':
 
    // Sort backups by key in ascending order (older first).
    ksort($backup_list, SORT_NUMERIC);
 
    $count = 0;
    foreach ($backup_list as $uuid => $backup_time) {
      if ($count < $limit) {
        $backups_to_delete[$uuid]['date'] = date(\DateTimeInterface::ATOM, $backup_time);
      }
      $count++;
    }
    break;
} 

Proposed resolution

Solution, do not remove any backup if the list of backups is lower than the limit, and calculate the number of backups to delete (current backup count minus limit).

      case 'number_to_keep':
        // Do not delete backups if the count of backups is minus than
        // the limit.
        if ($limit < 1 || count($backup_list) < $limit) {
          return [];
        }

        // Calculate the number of backups to delete.
        $number_backups_to_delete = count($backup_list) - $limit;

        // Sort backups by key in ascending order (older first).
        ksort($backup_list, SORT_NUMERIC);

        $count = 0;

        foreach ($backup_list as $uuid => $backup_time) {
          if ($count < $number_backups_to_delete) {
            $backups_to_delete[$uuid]['date'] = date(\DateTimeInterface::ATOM, $backup_time);
          }
          $count++;
        }
        break;
    }
πŸ› Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

πŸ‡ͺπŸ‡ΈSpain Eduardo Morales Alberti Spain, πŸ‡ͺπŸ‡Ί

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.69.0 2024