1
0
Fork 0
mirror of https://github.com/borgbackup/borg.git synced 2024-12-24 08:45:13 +00:00

attic delete: add repository + local cache deletion

This commit is contained in:
Thomas Waldmann 2015-03-09 16:02:06 +01:00
parent d3fe74d4c0
commit 6e6819e626
4 changed files with 45 additions and 17 deletions

View file

@ -224,18 +224,28 @@ def do_extract(self, args):
return self.exit_code
def do_delete(self, args):
"""Delete an existing archive"""
repository = self.open_repository(args.archive, exclusive=True)
"""Delete an existing repository or archive"""
repository = self.open_repository(args.target, exclusive=True)
manifest, key = Manifest.load(repository)
cache = Cache(repository, key, manifest)
archive = Archive(repository, key, manifest, args.archive.archive, cache=cache)
stats = Statistics()
archive.delete(stats)
manifest.write()
repository.commit()
cache.commit()
if args.stats:
stats.print_('Deleted data:', cache)
if args.target.archive:
archive = Archive(repository, key, manifest, args.target.archive, cache=cache)
stats = Statistics()
archive.delete(stats)
manifest.write()
repository.commit()
cache.commit()
if args.stats:
stats.print_('Deleted data:', cache)
else:
print("You requested to completely DELETE the repository *including* all archives it contains:")
for archive in sorted(Archive.list_archives(repository, key, manifest), key=attrgetter('ts')):
print(format_archive(archive))
print("""Type "YES" if you understand this and want to continue.\n""")
if input('Do you want to continue? ') == 'YES':
repository.destroy()
cache.destroy()
print("Repository and corresponding cache were deleted.")
return self.exit_code
def do_mount(self, args):
@ -591,8 +601,9 @@ def run(self, args=None):
help='paths to extract')
delete_epilog = textwrap.dedent("""
This command deletes an archive from the repository. Any disk space not
shared with any other existing archive is also reclaimed.
This command deletes an archive from the repository or the complete repository.
Disk space is reclaimed accordingly. If you delete the complete repository, the
local cache for it (if any) is also deleted.
""")
subparser = subparsers.add_parser('delete', parents=[common_parser],
description=self.do_delete.__doc__,
@ -602,9 +613,9 @@ def run(self, args=None):
subparser.add_argument('-s', '--stats', dest='stats',
action='store_true', default=False,
help='print statistics for the deleted archive')
subparser.add_argument('archive', metavar='ARCHIVE',
type=location_validator(archive=True),
help='archive to delete')
subparser.add_argument('target', metavar='TARGET',
type=location_validator(),
help='archive or repository to delete')
list_epilog = textwrap.dedent("""
This command lists the contents of a repository or an archive.

View file

@ -38,7 +38,7 @@ def __del__(self):
self.close()
def create(self):
"""Create a new empty cache at `path`
"""Create a new empty cache at `self.path`
"""
os.makedirs(self.path)
with open(os.path.join(self.path, 'README'), 'w') as fd:
@ -54,6 +54,13 @@ def create(self):
with open(os.path.join(self.path, 'files'), 'w') as fd:
pass # empty file
def destroy(self):
"""destroy the cache at `self.path`
"""
self.close()
os.remove(os.path.join(self.path, 'config')) # kill config first
shutil.rmtree(self.path)
def open(self):
if not os.path.isdir(self.path):
raise Exception('%s Does not look like an Attic cache' % self.path)

View file

@ -218,6 +218,9 @@ def commit(self, *args):
def rollback(self, *args):
return self.call('rollback')
def destroy(self):
return self.call('destroy')
def __len__(self):
return self.call('__len__')
@ -312,4 +315,4 @@ def get_many(self, keys):
def cache_if_remote(repository):
if isinstance(repository, RemoteRepository):
return RepositoryCache(repository)
return repository
return repository

View file

@ -79,6 +79,13 @@ def create(self, path):
with open(os.path.join(path, 'config'), 'w') as fd:
config.write(fd)
def destroy(self):
"""Destroy the repository at `self.path`
"""
self.close()
os.remove(os.path.join(self.path, 'config')) # kill config first
shutil.rmtree(self.path)
def get_index_transaction_id(self):
indicies = sorted((int(name[6:]) for name in os.listdir(self.path) if name.startswith('index.') and name[6:].isdigit()))
if indicies: