Skip to content

Commit

Permalink
borg check: give a named single archive to , fixes #139
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasWaldmann committed Aug 8, 2015
1 parent 149dd0d commit 3f352df
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
23 changes: 15 additions & 8 deletions borg/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ def __init__(self):
self.error_found = False
self.possibly_superseded = set()

def check(self, repository, repair=False, last=None):
def check(self, repository, repair=False, archive=None, last=None):
self.report_progress('Starting archive consistency check...')
self.repair = repair
self.repository = repository
Expand All @@ -619,8 +619,8 @@ def check(self, repository, repair=False, last=None):
self.manifest = self.rebuild_manifest()
else:
self.manifest, _ = Manifest.load(repository, key=self.key)
self.rebuild_refcounts(last=last)
if last is None:
self.rebuild_refcounts(archive=archive, last=last)
if last is None and archive is None:
self.verify_chunks()
else:
self.report_progress('Orphaned objects check skipped (needs all archives checked)')
Expand Down Expand Up @@ -680,7 +680,7 @@ def rebuild_manifest(self):
self.report_progress('Manifest rebuild complete', error=True)
return manifest

def rebuild_refcounts(self, last=None):
def rebuild_refcounts(self, archive=None, last=None):
"""Rebuild object reference counts by walking the metadata
Missing and/or incorrect data is repaired when detected
Expand Down Expand Up @@ -762,10 +762,17 @@ def missing_chunk_detector(chunk_id):
yield item

repository = cache_if_remote(self.repository)
num_archives = len(self.manifest.archives)
archive_items = sorted(self.manifest.archives.items(), reverse=True,
key=lambda name_info: name_info[1][b'time'])
end = None if last is None else min(num_archives, last)
if archive is None:
# we need last N or all archives
archive_items = sorted(self.manifest.archives.items(), reverse=True,
key=lambda name_info: name_info[1][b'time'])
num_archives = len(self.manifest.archives)
end = None if last is None else min(num_archives, last)
else:
# we only want one specific archive
archive_items = [item for item in self.manifest.archives.items() if item[0] == archive]
num_archives = 1
end = 1
for i, (name, info) in enumerate(archive_items[:end]):
self.report_progress('Analyzing archive {} ({}/{})'.format(name, num_archives - i, num_archives))
archive_id = info[b'id']
Expand Down
13 changes: 8 additions & 5 deletions borg/archiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ def do_check(self, args):
print('Repository check complete, no problems found.')
else:
return 1
if not args.repo_only and not ArchiveChecker().check(repository, repair=args.repair, last=args.last):
return 1
if not args.repo_only and not ArchiveChecker().check(
repository, repair=args.repair, archive=args.repository.archive, last=args.last):
return 1
return 0

def do_change_passphrase(self, args):
Expand Down Expand Up @@ -554,6 +555,8 @@ def run(self, args=None):
and other types of damage. After that the consistency and correctness of the archive
metadata is verified.
By giving an archive name, you can specifically check that archive.
The archive metadata checks can be time consuming and requires access to the key
file and/or passphrase if encryption is enabled. These checks can be skipped using
the --repository-only option.
Expand All @@ -563,9 +566,9 @@ def run(self, args=None):
epilog=check_epilog,
formatter_class=argparse.RawDescriptionHelpFormatter)
subparser.set_defaults(func=self.do_check)
subparser.add_argument('repository', metavar='REPOSITORY',
type=location_validator(archive=False),
help='repository to check consistency of')
subparser.add_argument('repository', metavar='REPOSITORY_OR_ARCHIVE',
type=location_validator(),
help='repository or archive to check consistency of')
subparser.add_argument('--repository-only', dest='repo_only', action='store_true',
default=False,
help='only perform repository checks')
Expand Down

0 comments on commit 3f352df

Please sign in to comment.