mirror of
https://github.com/borgbackup/borg.git
synced 2024-12-25 09:19:31 +00:00
More purge improvements
This commit is contained in:
parent
86a4353d32
commit
1c207d5686
2 changed files with 33 additions and 52 deletions
|
@ -1,5 +1,5 @@
|
||||||
import argparse
|
import argparse
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime
|
||||||
from operator import attrgetter
|
from operator import attrgetter
|
||||||
import os
|
import os
|
||||||
import stat
|
import stat
|
||||||
|
@ -11,7 +11,7 @@
|
||||||
from .key import Key
|
from .key import Key
|
||||||
from .helpers import location_validator, format_time, \
|
from .helpers import location_validator, format_time, \
|
||||||
format_file_mode, IncludePattern, ExcludePattern, exclude_path, to_localtime, \
|
format_file_mode, IncludePattern, ExcludePattern, exclude_path, to_localtime, \
|
||||||
get_cache_dir, format_timedelta, Purger
|
get_cache_dir, format_timedelta, purge_split
|
||||||
from .remote import StoreServer, RemoteStore
|
from .remote import StoreServer, RemoteStore
|
||||||
|
|
||||||
class Archiver(object):
|
class Archiver(object):
|
||||||
|
@ -246,38 +246,24 @@ def do_purge(self, args):
|
||||||
|
|
||||||
if args.prefix:
|
if args.prefix:
|
||||||
archives = [archive for archive in archives if archive.name.startswith(args.prefix)]
|
archives = [archive for archive in archives if archive.name.startswith(args.prefix)]
|
||||||
purger = Purger()
|
|
||||||
for archive in archives:
|
|
||||||
purger.insert(to_localtime(archive.ts).date(), archive)
|
|
||||||
archives, to_delete = purger.purge(len(purger.items))
|
|
||||||
if args.yearly:
|
|
||||||
purger = Purger()
|
|
||||||
for archive in archives:
|
|
||||||
purger.insert(to_localtime(archive.ts).strftime('%Y'), archive)
|
|
||||||
yearly, archives = purger.purge(args.yearly)
|
|
||||||
if args.monthly:
|
|
||||||
purger = Purger()
|
|
||||||
for archive in archives:
|
|
||||||
purger.insert(to_localtime(archive.ts).strftime('%Y-%m'), archive)
|
|
||||||
monthly, archives = purger.purge(args.monthly)
|
|
||||||
if args.weekly:
|
|
||||||
purger = Purger()
|
|
||||||
for archive in archives:
|
|
||||||
purger.insert(to_localtime(archive.ts).strftime('%Y-%V'), archive)
|
|
||||||
weekly, archives = purger.purge(args.weekly)
|
|
||||||
if args.daily:
|
if args.daily:
|
||||||
daily = archives[-args.daily:]
|
daily, archives = purge_split(archives, '%Y-%m-%d', args.daily, reverse=True)
|
||||||
archives = archives[:-args.daily]
|
if args.weekly:
|
||||||
to_delete += archives
|
weekly, archives = purge_split(archives, '%Y-%V', args.weekly, reverse=True)
|
||||||
|
if args.monthly:
|
||||||
|
monthly, archives = purge_split(archives, '%Y-%m', args.monthly, reverse=True)
|
||||||
|
if args.yearly:
|
||||||
|
yearly, archives = purge_split(archives, '%Y', args.weekly, reverse=True)
|
||||||
|
to_delete = archives
|
||||||
|
|
||||||
for i, archive in enumerate(yearly):
|
|
||||||
self.print_verbose('Keeping "%s" as yearly archive %d' % (archive.name, i + 1))
|
|
||||||
for i, archive in enumerate(monthly):
|
|
||||||
self.print_verbose('Keeping "%s" as monthly archive %d' % (archive.name, i + 1))
|
|
||||||
for i, archive in enumerate(weekly):
|
|
||||||
self.print_verbose('Keeping "%s" as weekly archive %d' % (archive.name, i + 1))
|
|
||||||
for i, archive in enumerate(daily):
|
for i, archive in enumerate(daily):
|
||||||
self.print_verbose('Keeping "%s" as daily archive %d' % (archive.name, i + 1))
|
self.print_verbose('Keeping "%s" as daily archive %d' % (archive.name, i + 1))
|
||||||
|
for i, archive in enumerate(weekly):
|
||||||
|
self.print_verbose('Keeping "%s" as weekly archive %d' % (archive.name, i + 1))
|
||||||
|
for i, archive in enumerate(monthly):
|
||||||
|
self.print_verbose('Keeping "%s" as monthly archive %d' % (archive.name, i + 1))
|
||||||
|
for i, archive in enumerate(yearly):
|
||||||
|
self.print_verbose('Keeping "%s" as yearly archive %d' % (archive.name, i + 1))
|
||||||
for archive in to_delete:
|
for archive in to_delete:
|
||||||
if args.really:
|
if args.really:
|
||||||
self.print_verbose('Purging archive "%s"', archive.name)
|
self.print_verbose('Purging archive "%s"', archive.name)
|
||||||
|
|
|
@ -14,28 +14,23 @@
|
||||||
import urllib
|
import urllib
|
||||||
|
|
||||||
|
|
||||||
class Purger(object):
|
def purge_split(archives, pattern, n, reverse=False):
|
||||||
"""Purging helper"""
|
items = {}
|
||||||
|
keep = []
|
||||||
def __init__(self):
|
delete = []
|
||||||
self.items = {}
|
for a in archives:
|
||||||
|
key = to_localtime(a.ts).strftime(pattern)
|
||||||
def insert(self, key, value):
|
items.setdefault(key, [])
|
||||||
self.items.setdefault(key, [])
|
items[key].append(a)
|
||||||
self.items[key].append(value)
|
for key, values in sorted(items.items(), reverse=reverse):
|
||||||
|
if n:
|
||||||
def purge(self, n, reverse=False):
|
values.sort(key=attrgetter('ts'), reverse=reverse)
|
||||||
keep = []
|
keep.append(values[0])
|
||||||
delete = []
|
delete += values[1:]
|
||||||
for key, values in sorted(self.items.items(), reverse=reverse):
|
n -= 1
|
||||||
if n:
|
else:
|
||||||
values.sort(key=attrgetter('ts'), reverse=reverse)
|
delete += values
|
||||||
keep.append(values[0])
|
return keep, delete
|
||||||
delete += values[1:]
|
|
||||||
n -= 1
|
|
||||||
else:
|
|
||||||
delete += values
|
|
||||||
return keep, delete
|
|
||||||
|
|
||||||
|
|
||||||
class Statistics(object):
|
class Statistics(object):
|
||||||
|
|
Loading…
Reference in a new issue