From 6211a3dc793d7bec99243ffefe6454e1d4e5012a Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Fri, 19 Jul 2024 19:44:38 +0200 Subject: [PATCH 1/3] key export: move examples to docs --- docs/usage/key.rst | 12 ++++++++++++ src/borg/archiver/key_cmds.py | 12 ------------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/docs/usage/key.rst b/docs/usage/key.rst index 07b517a43..728db27eb 100644 --- a/docs/usage/key.rst +++ b/docs/usage/key.rst @@ -45,4 +45,16 @@ Fully automated using environment variables: .. include:: key_export.rst.inc +Examples +~~~~~~~~ +:: + + borg key export > encrypted-key-backup + borg key export --paper > encrypted-key-backup.txt + borg key export --qr-html > encrypted-key-backup.html + # Or pass the output file as an argument instead of redirecting stdout: + borg key export encrypted-key-backup + borg key export --paper encrypted-key-backup.txt + borg key export --qr-html encrypted-key-backup.html + .. include:: key_import.rst.inc diff --git a/src/borg/archiver/key_cmds.py b/src/borg/archiver/key_cmds.py index c3b4e457d..1afa666a3 100644 --- a/src/borg/archiver/key_cmds.py +++ b/src/borg/archiver/key_cmds.py @@ -160,18 +160,6 @@ class KeysMixIn: repository in the config file. A backup is thus not strictly needed, but guards against the repository becoming inaccessible if the file is damaged for some reason. - - Examples:: - - borg key export /path/to/repo > encrypted-key-backup - borg key export --paper /path/to/repo > encrypted-key-backup.txt - borg key export --qr-html /path/to/repo > encrypted-key-backup.html - # Or pass the output file as an argument instead of redirecting stdout: - borg key export /path/to/repo encrypted-key-backup - borg key export --paper /path/to/repo encrypted-key-backup.txt - borg key export --qr-html /path/to/repo encrypted-key-backup.html - - """ ) subparser = key_parsers.add_parser( From 5eecdfa13363a6bcbb94a8181818f389f41619e2 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Fri, 19 Jul 2024 19:45:57 +0200 Subject: [PATCH 2/3] key export: fix crash when no path is given In this case, the export functions output to stdout. Also: add a note why this code is needed. --- src/borg/archiver/key_cmds.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/borg/archiver/key_cmds.py b/src/borg/archiver/key_cmds.py index 1afa666a3..9dec5a5d2 100644 --- a/src/borg/archiver/key_cmds.py +++ b/src/borg/archiver/key_cmds.py @@ -97,7 +97,9 @@ class KeysMixIn: manager.export_paperkey(args.path) else: try: - if os.path.isdir(args.path): + if args.path is not None and os.path.isdir(args.path): + # on Windows, Python raises PermissionError instead of IsADirectoryError + # (like on Unix) if the file to open is actually a directory. raise IsADirectoryError if args.qr: manager.export_qr(args.path) From 4e6238e7d35f86ef46cdde5eb2351819e4e6d105 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Fri, 19 Jul 2024 19:55:14 +0200 Subject: [PATCH 3/3] key export: fix exception handling export_paperkey also must not get an already existing directory. --- src/borg/archiver/key_cmds.py | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/borg/archiver/key_cmds.py b/src/borg/archiver/key_cmds.py index 9dec5a5d2..1a7b4769a 100644 --- a/src/borg/archiver/key_cmds.py +++ b/src/borg/archiver/key_cmds.py @@ -93,20 +93,19 @@ class KeysMixIn: """Export the repository key for backup""" manager = KeyManager(repository) manager.load_keyblob() - if args.paper: - manager.export_paperkey(args.path) - else: - try: - if args.path is not None and os.path.isdir(args.path): - # on Windows, Python raises PermissionError instead of IsADirectoryError - # (like on Unix) if the file to open is actually a directory. - raise IsADirectoryError - if args.qr: - manager.export_qr(args.path) - else: - manager.export(args.path) - except IsADirectoryError: - raise CommandError(f"'{args.path}' must be a file, not a directory") + try: + if args.path is not None and os.path.isdir(args.path): + # on Windows, Python raises PermissionError instead of IsADirectoryError + # (like on Unix) if the file to open is actually a directory. + raise IsADirectoryError + if args.paper: + manager.export_paperkey(args.path) + elif args.qr: + manager.export_qr(args.path) + else: + manager.export(args.path) + except IsADirectoryError: + raise CommandError(f"'{args.path}' must be a file, not a directory") @with_repository(lock=False, exclusive=False, manifest=False, cache=False) def do_key_import(self, args, repository):