Kaynağa Gözat

add recreate action

Vandal 2 ay önce
ebeveyn
işleme
a750d58a2d

+ 46 - 0
borgmatic/actions/recreate.py

@@ -0,0 +1,46 @@
+import logging
+
+import borgmatic.borg.recreate
+import borgmatic.config.validate
+
+logger = logging.getLogger(__name__)
+
+
+def run_recreate(
+    repository,
+    config,
+    local_borg_version,
+    recreate_arguments,
+    global_arguments,
+    local_path,
+    remote_path,
+):
+    '''
+    Run the "recreate" action for the given repository.
+    '''
+    if recreate_arguments.repository is None or borgmatic.config.validate.repositories_match(
+        repository, recreate_arguments.repository
+    ):
+        if recreate_arguments.archive:
+            logger.info(f'Recreating archive {recreate_arguments.archive}')
+        else:
+            logger.info('Recreating repository')
+
+        borgmatic.borg.recreate.recreate_archive(
+            repository['path'],
+            borgmatic.borg.repo_list.resolve_archive_name(
+                repository['path'],
+                recreate_arguments.archive,
+                config,
+                local_borg_version,
+                global_arguments,
+                local_path,
+                remote_path,
+            ),
+            config,
+            local_borg_version,
+            recreate_arguments,
+            global_arguments,
+            local_path=local_path,
+            remote_path=remote_path,
+        )

+ 15 - 46
borgmatic/borg/recreate.py

@@ -9,7 +9,7 @@ from borgmatic.borg.flags import make_flags_from_arguments, make_repository_arch
 logger = logging.getLogger(__name__)
 
 
-def make_recreate_command(
+def recreate_archive(
     repository,
     archive,
     config,
@@ -24,61 +24,30 @@ def make_recreate_command(
     the local Borg version string, an argparse.Namespace of recreate arguments,
     an argparse.Namespace of global arguments, optional local and remote Borg paths.
 
-    Returns the recreate command as a tuple of strings ready for execution.
+    Executes the recreate command with the given arguments.
     '''
-    verbosity_flags = (('--debug', '--show-rc') if logger.isEnabledFor(logging.DEBUG) else ()) + (
-        ('--info',) if logger.isEnabledFor(logging.INFO) else ()
-    )
+    lock_wait = config.get('lock_wait', None)
 
-    # handle both the recreate and global arguments
-    recreate_flags = make_flags_from_arguments(
-        recreate_arguments, excludes=('repository', 'archive')
-    )
-    global_flags = make_flags_from_arguments(global_arguments)
-
-    repo_archive_flags = make_repository_archive_flags(repository, archive, local_borg_version)
+    repo_archive_arg = make_repository_archive_flags(repository, archive, local_borg_version)
     exclude_flags = make_exclude_flags(config)
 
-    return (
+    recreate_cmd = (
         (local_path, 'recreate')
-        + repo_archive_flags
-        + verbosity_flags
-        + global_flags
-        + recreate_flags
+        + (('--remote-path', remote_path) if remote_path else ())
+        + repo_archive_arg
+        + (('--log-json',) if global_arguments.log_json else ())
+        + (('--lock-wait', str(lock_wait)) if lock_wait else ())
+        + (('--info',) if logger.getEffectiveLevel() == logging.INFO else ())
+        + (('--debug', '--show-rc', '--list') if logger.isEnabledFor(logging.DEBUG) else ())
         + exclude_flags
     )
 
-
-def recreate_archive(
-    repository,
-    archive,
-    config,
-    local_borg_version,
-    recreate_arguments,
-    global_arguments,
-    local_path='borg',
-    remote_path=None,
-):
-    '''
-    Given a local or remote repository path, an archive name, a configuration dict,
-    the local Borg version string, an argparse.Namespace of recreate arguments,
-    an argparse.Namespace of global arguments, optional local and remote Borg paths.
-
-    Executes the recreate command with the given arguments.
-    '''
-    command = make_recreate_command(
-        repository,
-        archive,
-        config,
-        local_borg_version,
-        recreate_arguments,
-        global_arguments,
-        local_path,
-        remote_path,
-    )
+    if global_arguments.dry_run:
+        logger.info('Skipping the archive recreation (dry run)')
+        return
 
     borgmatic.execute.execute_command(
-        command,
+        recreate_cmd,
         output_log_level=logging.ANSWER,
         environment=borgmatic.borg.environment.make_environment(config),
         working_directory=borgmatic.config.paths.get_working_directory(config),

+ 21 - 0
borgmatic/commands/arguments.py

@@ -27,6 +27,7 @@ ACTION_ALIASES = {
     'break-lock': [],
     'key': [],
     'borg': [],
+    'recreate': [],
 }
 
 
@@ -1545,6 +1546,26 @@ def make_parsers():
     )
     borg_group.add_argument('-h', '--help', action='help', help='Show this help message and exit')
 
+    recreate_parser = action_parsers.add_parser(
+        'recreate',
+        aliases=ACTION_ALIASES['recreate'],
+        help='Recreate an archive in a repository',
+        description='Recreate an archive in a repository',
+        add_help=False,
+    )
+    recreate_group = recreate_parser.add_argument_group('recreate arguments')
+    recreate_group.add_argument(
+        '--repository',
+        help='Path of the repository containing the archive',
+    )
+    recreate_group.add_argument(
+        '--archive',
+        help='Name of the archive to recreate',
+    )
+    recreate_group.add_argument(
+        '-h', '--help', action='help', help='Show this help message and exit'
+    )
+
     return global_parser, action_parsers, global_plus_action_parser
 
 

+ 11 - 0
borgmatic/commands/borgmatic.py

@@ -17,6 +17,7 @@ import borgmatic.actions.config.bootstrap
 import borgmatic.actions.config.generate
 import borgmatic.actions.config.validate
 import borgmatic.actions.create
+import borgmatic.actions.recreate
 import borgmatic.actions.delete
 import borgmatic.actions.export_key
 import borgmatic.actions.export_tar
@@ -397,6 +398,16 @@ def run_actions(
                         local_path,
                         remote_path,
                     )
+                elif action_name == 'recreate' and action_name not in skip_actions:
+                    borgmatic.actions.recreate.run_recreate(
+                        repository,
+                        config,
+                        local_borg_version,
+                        action_arguments,
+                        global_arguments,
+                        local_path,
+                        remote_path,
+                    )
                 elif action_name == 'prune' and action_name not in skip_actions:
                     borgmatic.actions.prune.run_prune(
                         config_filename,