From 66340bc2a886c7ede75afa1f2ce33aafd016a896 Mon Sep 17 00:00:00 2001 From: yfprojects <62463991+real-yfprojects@users.noreply.github.com> Date: Wed, 18 Jan 2023 15:57:23 +0000 Subject: [PATCH] Add support for `--paths-from-command` to extra borg arguments. (#1538) Backups will run although no sources are specified if `--paths-from-command` is supplied. Also arguments after `--` will be appended to the end of the command after all other arguments. Closes #1537. * src/vorta/borg/create.py * tests/test_create.py : Add test for using `--path-from-commands`. Co-authored-by: real-yfprojects --- src/vorta/borg/create.py | 19 +++++++++++---- tests/test_create.py | 51 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 tests/test_create.py diff --git a/src/vorta/borg/create.py b/src/vorta/borg/create.py index 52e0884f..f54f70d5 100644 --- a/src/vorta/borg/create.py +++ b/src/vorta/borg/create.py @@ -77,7 +77,18 @@ class BorgCreateJob(BorgJob): ret['ok'] = False # Set back to False, so we can do our own checks here. n_backup_folders = SourceFileModel.select().count() - if n_backup_folders == 0: + + # cmd options like `--paths-from-command` require a command + # that is appended to the arguments + # $ borg create --paths-from-command repo::archive1 -- find /home/user -type f -size -76M + extra_cmd_options = [] + suffix_command = [] + if profile.repo.create_backup_cmd: + s1, sep, s2 = profile.repo.create_backup_cmd.partition('-- ') + extra_cmd_options = s1.split() + suffix_command = (sep + s2).split() + + if n_backup_folders == 0 and '--paths-from-command' not in extra_cmd_options: ret['message'] = trans_late('messages', 'Add some folders to back up first.') return ret @@ -132,9 +143,7 @@ class BorgCreateJob(BorgJob): '-C', profile.compression, ] - - if profile.repo.create_backup_cmd: - cmd.extend(profile.repo.create_backup_cmd.split(' ')) + cmd += extra_cmd_options # Add excludes # Partly inspired by borgmatic/borgmatic/borg/create.py @@ -164,6 +173,8 @@ class BorgCreateJob(BorgJob): for f in SourceFileModel.select().where(SourceFileModel.profile == profile.id): cmd.append(f.dir) + cmd += suffix_command + ret['message'] = trans_late('messages', 'Starting backup…') ret['ok'] = True ret['cmd'] = cmd diff --git a/tests/test_create.py b/tests/test_create.py new file mode 100644 index 00000000..aac6bcbc --- /dev/null +++ b/tests/test_create.py @@ -0,0 +1,51 @@ +from vorta.borg.create import BorgCreateJob +from vorta.store.models import BackupProfileModel, SourceFileModel + + +def test_create_paths_from_command(): + default_profile = BackupProfileModel.get() + default_profile.new_archive_name = 'a1' + + default_profile.repo.create_backup_cmd = '--one-file-system' + result = BorgCreateJob.prepare(default_profile) + + assert 'cmd' in result + assert result['cmd'] == [ + 'borg', + 'create', + '--list', + '--progress', + '--info', + '--log-json', + '--json', + '--filter=AM', + '-C', + 'lz4', + '--one-file-system', + 'i0fi93@i593.repo.borgbase.com:repo::a1', + '/tmp/another', + ] + + default_profile.repo.create_backup_cmd = '--paths-from-command -- echo /tmp/another' + SourceFileModel.delete().execute() + + result = BorgCreateJob.prepare(default_profile) + + assert 'cmd' in result + assert result['cmd'] == [ + 'borg', + 'create', + '--list', + '--progress', + '--info', + '--log-json', + '--json', + '--filter=AM', + '-C', + 'lz4', + '--paths-from-command', + 'i0fi93@i593.repo.borgbase.com:repo::a1', + '--', + 'echo', + '/tmp/another', + ]