Skip to content

Commit

Permalink
Add a --list switch to borg delete (#5503)
Browse files Browse the repository at this point in the history
add a --list switch to borg delete, fixes #5116
  • Loading branch information
Gu1nness authored Dec 11, 2020
1 parent c2118f1 commit 5017bc4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/usage/delete.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Examples
$ borg delete --glob-archives '*-2012-*' /path/to/repo

# see what would be deleted if delete was run without --dry-run
$ borg delete -v --dry-run -a '*-May-*' /path/to/repo
$ borg delete --list --dry-run -a '*-May-*' /path/to/repo

# delete the whole repository and the related local cache:
$ borg delete /path/to/repo
Expand Down
29 changes: 22 additions & 7 deletions src/borg/archiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,7 @@ def do_delete(self, args, repository):
"""Delete an existing repository or archives"""
archive_filter_specified = any((args.first, args.last, args.prefix is not None, args.glob_archives))
explicit_archives_specified = args.location.archive or args.archives
self.output_list = args.output_list
if archive_filter_specified and explicit_archives_specified:
self.print_error('Mixing archive filters and explicitly named archives is not supported.')
return self.exit_code
Expand Down Expand Up @@ -1210,6 +1211,7 @@ def _delete_archives(self, args, repository):

if args.forced == 2:
deleted = False
logger_list = logging.getLogger('borg.output.list')
for i, archive_name in enumerate(archive_names, 1):
try:
current_archive = manifest.archives.pop(archive_name)
Expand All @@ -1218,8 +1220,10 @@ def _delete_archives(self, args, repository):
logger.warning('Archive {} not found ({}/{}).'.format(archive_name, i, len(archive_names)))
else:
deleted = True
msg = 'Would delete: {} ({}/{})' if dry_run else 'Deleted archive: {} ({}/{})'
logger.info(msg.format(format_archive(current_archive), i, len(archive_names)))
if self.output_list:
msg = 'Would delete: {} ({}/{})' if dry_run else 'Deleted archive: {} ({}/{})'
logger_list.info(msg.format(format_archive(current_archive),
i, len(archive_names)))
if dry_run:
logger.info('Finished dry-run.')
elif deleted:
Expand All @@ -1235,13 +1239,16 @@ def _delete_archives(self, args, repository):
with Cache(repository, key, manifest, progress=args.progress, lock_wait=self.lock_wait) as cache:
msg_delete = 'Would delete archive: {} ({}/{})' if dry_run else 'Deleting archive: {} ({}/{})'
msg_not_found = 'Archive {} not found ({}/{}).'
logger_list = logging.getLogger('borg.output.list')
for i, archive_name in enumerate(archive_names, 1):
try:
archive_info = manifest.archives[archive_name]
except KeyError:
logger.warning(msg_not_found.format(archive_name, i, len(archive_names)))
else:
logger.info(msg_delete.format(format_archive(archive_info), i, len(archive_names)))
if self.output_list:
logger_list.info(msg_delete.format(format_archive(archive_info), i, len(archive_names)))

if not dry_run:
archive = Archive(repository, key, manifest, archive_name, cache=cache,
consider_part_files=args.consider_part_files)
Expand Down Expand Up @@ -1275,10 +1282,14 @@ def _delete_repository(self, args, repository):
msg.append("This repository seems to have no manifest, so we can't tell anything about its "
"contents.")
else:
msg.append("You requested to completely DELETE the repository *including* all archives it "
"contains:")
for archive_info in manifest.archives.list(sort_by=['ts']):
msg.append(format_archive(archive_info))
if self.output_list:
msg.append("You requested to completely DELETE the repository *including* all archives it "
"contains:")
for archive_info in manifest.archives.list(sort_by=['ts']):
msg.append(format_archive(archive_info))
else:
msg.append("You requested to completely DELETE the repository *including* %d archives it contains."
% len(manifest.archives))
msg.append("Type 'YES' if you understand this and want to continue: ")
msg = '\n'.join(msg)
if not yes(msg, false_msg="Aborting.", invalid_msg='Invalid answer, aborting.', truish=('YES',),
Expand Down Expand Up @@ -3564,6 +3575,8 @@ def define_borg_mount(parser):
with the ``--cache-only`` option, or keep the security info with the
``--keep-security-info`` option.
When in doubt, use ``--dry-run --list`` to see what would be deleted.
When using ``--stats``, you will get some statistics about how much data was
deleted - the "Deleted data" deduplicated size there is most interesting as
that is how much your repository will shrink.
Expand All @@ -3587,6 +3600,8 @@ def define_borg_mount(parser):
subparser.set_defaults(func=self.do_delete)
subparser.add_argument('-n', '--dry-run', dest='dry_run', action='store_true',
help='do not change repository')
subparser.add_argument('--list', dest='output_list', action='store_true',
help='output verbose list of archives')
subparser.add_argument('-s', '--stats', dest='stats', action='store_true',
help='print statistics for the deleted archive')
subparser.add_argument('--cache-only', dest='cache_only', action='store_true',
Expand Down

0 comments on commit 5017bc4

Please sign in to comment.