From d07c4056a69d2f5136bd454e3fbf3b2849134516 Mon Sep 17 00:00:00 2001 From: Sora Date: Sun, 5 Nov 2023 22:38:44 +0100 Subject: [PATCH] little mistakes. --- configuration.ini.example | 2 +- main.py | 24 +++++++++++++++--------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/configuration.ini.example b/configuration.ini.example index f9d3f4f..abfd934 100644 --- a/configuration.ini.example +++ b/configuration.ini.example @@ -1,5 +1,5 @@ [backandup] -bindincludepattern=list of fixed strings +bindcludepattern=list of fixed strings bindexcludepattern=list of fixed strings [restic] diff --git a/main.py b/main.py index 1412651..cb88cc0 100644 --- a/main.py +++ b/main.py @@ -21,8 +21,8 @@ def read_config_file(filepath: str = "configuration.ini"): global remoteUser global repository global passwordFile - bindingIncludePattern = parser["backandup"]["bindingincludepattern"] - bindingExcludePattern = parser["backandup"]["bindingexcludepattern"] + bindingIncludePattern = parser["backandup"]["bindincludepattern"] + bindingExcludePattern = parser["backandup"]["bindexcludepattern"] remoteHost = parser["restic"]["remotehost"] remoteUser = parser["restic"]["remoteuser"] repository = parser["restic"]["repository"] @@ -30,8 +30,7 @@ def read_config_file(filepath: str = "configuration.ini"): def do_backup(dirpath: str, tags: str = None): - repoString = f"sftp:{remoteUser}@{remoteHost}:/{repository}" - restic.repository = repoString + restic.repository = f"sftp:{remoteUser}@{remoteHost}:/{repository}" restic.password_file = passwordFile if tags is None: restic.backup(paths=[dirpath]) @@ -39,39 +38,46 @@ def do_backup(dirpath: str, tags: str = None): restic.backup(paths=[dirpath], tags=[]) -def backup_ct_binds(ct: docker.models.containers.Container, includepattern: str|list = None, - excludepattern: str|list = None): +def backup_ct_binds(ct: docker.models.containers.Container, includepattern: str | list = None, + excludepattern: str | list = None): if ct.attrs['HostConfig']['Binds'] is None: - print("Nothing to backup") + print(" Nothing to backup") return 0 if type(includepattern) is str: includepattern = [includepattern] if type(excludepattern) is str: excludepattern = [excludepattern] - if type(includepattern) is None: + if includepattern is None: includepattern = ["NOPATTERNTOINCLUDEXXXXXX"] - if type(excludepattern) is None: + if excludepattern is None: excludepattern = ["NOPATTERNTOEXCLUDEXXXXXX"] for BindMount in ct.attrs['HostConfig']['Binds']: BindPath = BindMount.split(":")[0] + print(f" {BindPath}") # Si on matche au moins 1 pattern d'inclusion ET aucun pattern d'exclusion if any(x in BindPath for x in includepattern) and not any(x in BindPath for x in excludepattern): do_backup(BindPath) + else: + print(" not a directory to backup") return 0 def stop_backup_restart_container(ct: docker.models.containers.Container): + print(ct.name) dorestart = False if ct.attrs['State']['Status'] == 'running': + print(" stop the container") dorestart = True ct.stop() backup_ct_binds(ct, bindingIncludePattern, bindingExcludePattern) if dorestart: + print(" start the container") ct.start() return 0 # Press the green button in the gutter to run the script. if __name__ == '__main__': dockerhost = docker.from_env() + read_config_file() for container in dockerhost.containers.list(all=True): stop_backup_restart_container(container)