diff --git a/src/restic_compose_backup/cli.py b/src/restic_compose_backup/cli.py index 85a6542..f824fb5 100644 --- a/src/restic_compose_backup/cli.py +++ b/src/restic_compose_backup/cli.py @@ -54,15 +54,20 @@ def status(config, containers): logger.info("Status for compose project '%s'", containers.project_name) logger.info("Repository: '%s'", config.repository) logger.info("Backup currently running?: %s", containers.backup_process_running) - logger.info("%s Detected Config %s", "-" * 25, "-" * 25) if containers.stale_backup_process_containers: utils.remove_containers(containers.stale_backup_process_containers) # Check if repository is initialized with restic snapshots - if restic.snapshots(config.repository) != 0: - logger.info("Initializing repository") - restic.init_repo(config.repository) + if not restic.is_initialized(config.repository): + logger.info("Could not get repository info. Attempting to initialize it.") + result = restic.init_repo(config.repository) + if result == 0: + logger.info("Successfully initialized repository: %s", config.repository) + else: + logger.error("Failed to initialize repository") + + logger.info("%s Detected Config %s", "-" * 25, "-" * 25) # Start making snapshots backup_containers = containers.containers_for_backup() diff --git a/src/restic_compose_backup/restic.py b/src/restic_compose_backup/restic.py index 1281a95..2bf2a60 100644 --- a/src/restic_compose_backup/restic.py +++ b/src/restic_compose_backup/restic.py @@ -58,12 +58,23 @@ def backup_from_stdin(repository: str, filename: str, source_command: List[str]) def snapshots(repository: str, last=True) -> Tuple[str, str]: + """Returns the stdout and stderr info""" args = ["snapshots"] if last: args.append('--last') return commands.run_capture_std(restic(repository, args)) +def is_initialized(repository: str) -> bool: + """ + Checks if a repository is initialized using snapshots command. + Note that this cannot separate between uninitalized repo + and other errors, but this method is reccomended by the restic + community. + """ + return commands.run(restic(repository, ["snapshots", '--last'])) == 0 + + def forget(repository: str, daily: str, weekly: str, monthly: str, yearly: str): return commands.run(restic(repository, [ 'forget',