diff --git a/tibi_hardlinks/__init__.py b/tibi_hardlinks/__init__.py index f2f5ed4..1dad76b 100644 --- a/tibi_hardlinks/__init__.py +++ b/tibi_hardlinks/__init__.py @@ -36,6 +36,25 @@ def hardlink_all_files(files, dry=False): backup.hardlink_to(dest_dir, dry) +def clean_out(backups: list[Backup], before_date, target_size): + total_size = sum(backup.size for backup in backups) + target_removal_size = target_size - total_size + if target_removal_size <= 0: + return () + backups_eligable = ( + backup for backup in backups if backup.properties["backup_time"] <= before_date + ) + sorted_backups_eligable = sorted( + backups_eligable, key=lambda backup: backup.size, reverse=True + ) + to_remove = list() + for backup in sorted_backups_eligable: + to_remove.append(backup) + target_removal_size -= backup.size + if target_removal_size <= 0: + return to_remove + + def main(): parser = ArgumentParser() parser.add_argument("-d", "--dry", action="store_true", help="Don't do anything") diff --git a/tibi_hardlinks/backups.py b/tibi_hardlinks/backups.py index c17b2ff..07764f0 100644 --- a/tibi_hardlinks/backups.py +++ b/tibi_hardlinks/backups.py @@ -98,6 +98,13 @@ class Backup: self.backup_info = read_backup_properties(property_file) self.related_files = find_backup_data(self.property_file, self.backup_info) + @property + def size(self): + total = 0 + for f in self.related_files.values(): + total += f.stat().st_size + return total + def hardlink_to(self, dest_dir, dry): for filepath in self.related_files.values(): if filepath: