Repository: borgbackup/borg

Issue:
Incorrect borg prune --stats output with --dry-run
With `borg 1.1.7` on `ubuntu 18.10`:
```
borg prune <borg_dir> --dry-run --list --stats --keep-last 1 
...
                       Original size      Compressed size    Deduplicated size
Deleted data:                    0 B                  0 B                  0 B
All archives:              981.25 GB            793.40 GB            112.32 GB

                       Unique chunks         Total chunks
Chunk index:                  250054              2164226
```
```
borg prune <borg_dir> --list --stats --keep-last 1 
...
                       Original size      Compressed size    Deduplicated size
Deleted data:             -820.45 GB           -669.72 GB             -9.60 GB
All archives:              160.80 GB            123.69 GB            102.72 GB

                       Unique chunks         Total chunks
Chunk index:                  237044               371408
```
Pruning worked as I wanted it to, but the dry run stats do not give an accurate preview.


Discussion:
Likely, that is simply not implemented (just guessing, needs to be checked in the src).
I had the look at the src:
- the intended purpose of `borg prune --dry-run` is to see what archives would be deleted (pruned) and what would be kept. this is mostly to avoid calling it with wrong arguments and losing backups (if called w/o `-dry-run`).
- as a dry-run does not even call `archive.delete()` (and stats are updated there, for real runs), there is no way it can show accurate stats with `--dry-run`.
- due to deduplication, it is non-trivial and not fast to give accurate stats. it would have to emulate all the refcounting without actually deleting anything.

Proposed fix:
emit an error message saying `--dry-run` and `--stats` can't be used together.
If this is still available, could i work on this issue.
sure, go for it, make a PR.
@4rund3v did you have a look?
@ThomasWaldmann 

some tests do use --dry-run and --stats together

i think we could add a simple check in archiver.run would that be appropriate ?
@user062 yes. there already some such checks.
Rather than exiting when someone uses --dry-run and --stats together, could we instead give then a warning and continue to run the dry-run while ignoring the stats?

My reasoning is that this current change breaks my wrapper script's current succinct of handling real/dry purges ( https://notabug.org/demure/scripts/src/master/borg_wrap.sh#L138-L150 ).

If the decision is to leave this as is, I'll just make my wrapper list two full commands I suppose, but this feel like an unintended roadblock so I figured I'd point it out.

ps. sorry I didn't notice this sooner, but I haven't pruned in a long time.
OK, guess we can do that, reopening...

Resolving patch:
diff --git a/src/borg/archiver.py b/src/borg/archiver.py
--- a/src/borg/archiver.py
+++ b/src/borg/archiver.py
@@ -4415,8 +4415,10 @@ def run(self, args):
         self._setup_implied_logging(vars(args))
         self._setup_topic_debugging(args)
         if getattr(args, 'stats', False) and getattr(args, 'dry_run', False):
-            logger.error("--stats does not work with --dry-run.")
-            return self.exit_code
+            # the data needed for --stats is not computed when using --dry-run, so we can't do it.
+            # for ease of scripting, we just ignore --stats when given with --dry-run.
+            logger.warning("Ignoring --stats. It is not supported when using --dry-run.")
+            args.stats = False
         if args.show_version:
             logging.getLogger('borg.output.show-version').info('borgbackup version %s' % __version__)
         self.prerun_checks(logger, is_serve)


Test patch: