little mistakes.

This commit is contained in:
2023-11-05 22:38:44 +01:00
parent c0191b8a73
commit d07c4056a6
2 changed files with 16 additions and 10 deletions

View File

@@ -1,5 +1,5 @@
[backandup] [backandup]
bindincludepattern=list of fixed strings bindcludepattern=list of fixed strings
bindexcludepattern=list of fixed strings bindexcludepattern=list of fixed strings
[restic] [restic]

24
main.py
View File

@@ -21,8 +21,8 @@ def read_config_file(filepath: str = "configuration.ini"):
global remoteUser global remoteUser
global repository global repository
global passwordFile global passwordFile
bindingIncludePattern = parser["backandup"]["bindingincludepattern"] bindingIncludePattern = parser["backandup"]["bindincludepattern"]
bindingExcludePattern = parser["backandup"]["bindingexcludepattern"] bindingExcludePattern = parser["backandup"]["bindexcludepattern"]
remoteHost = parser["restic"]["remotehost"] remoteHost = parser["restic"]["remotehost"]
remoteUser = parser["restic"]["remoteuser"] remoteUser = parser["restic"]["remoteuser"]
repository = parser["restic"]["repository"] repository = parser["restic"]["repository"]
@@ -30,8 +30,7 @@ def read_config_file(filepath: str = "configuration.ini"):
def do_backup(dirpath: str, tags: str = None): def do_backup(dirpath: str, tags: str = None):
repoString = f"sftp:{remoteUser}@{remoteHost}:/{repository}" restic.repository = f"sftp:{remoteUser}@{remoteHost}:/{repository}"
restic.repository = repoString
restic.password_file = passwordFile restic.password_file = passwordFile
if tags is None: if tags is None:
restic.backup(paths=[dirpath]) restic.backup(paths=[dirpath])
@@ -39,39 +38,46 @@ def do_backup(dirpath: str, tags: str = None):
restic.backup(paths=[dirpath], tags=[]) restic.backup(paths=[dirpath], tags=[])
def backup_ct_binds(ct: docker.models.containers.Container, includepattern: str|list = None, def backup_ct_binds(ct: docker.models.containers.Container, includepattern: str | list = None,
excludepattern: str|list = None): excludepattern: str | list = None):
if ct.attrs['HostConfig']['Binds'] is None: if ct.attrs['HostConfig']['Binds'] is None:
print("Nothing to backup") print(" Nothing to backup")
return 0 return 0
if type(includepattern) is str: if type(includepattern) is str:
includepattern = [includepattern] includepattern = [includepattern]
if type(excludepattern) is str: if type(excludepattern) is str:
excludepattern = [excludepattern] excludepattern = [excludepattern]
if type(includepattern) is None: if includepattern is None:
includepattern = ["NOPATTERNTOINCLUDEXXXXXX"] includepattern = ["NOPATTERNTOINCLUDEXXXXXX"]
if type(excludepattern) is None: if excludepattern is None:
excludepattern = ["NOPATTERNTOEXCLUDEXXXXXX"] excludepattern = ["NOPATTERNTOEXCLUDEXXXXXX"]
for BindMount in ct.attrs['HostConfig']['Binds']: for BindMount in ct.attrs['HostConfig']['Binds']:
BindPath = BindMount.split(":")[0] BindPath = BindMount.split(":")[0]
print(f" {BindPath}")
# Si on matche au moins 1 pattern d'inclusion ET aucun pattern d'exclusion # 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): if any(x in BindPath for x in includepattern) and not any(x in BindPath for x in excludepattern):
do_backup(BindPath) do_backup(BindPath)
else:
print(" not a directory to backup")
return 0 return 0
def stop_backup_restart_container(ct: docker.models.containers.Container): def stop_backup_restart_container(ct: docker.models.containers.Container):
print(ct.name)
dorestart = False dorestart = False
if ct.attrs['State']['Status'] == 'running': if ct.attrs['State']['Status'] == 'running':
print(" stop the container")
dorestart = True dorestart = True
ct.stop() ct.stop()
backup_ct_binds(ct, bindingIncludePattern, bindingExcludePattern) backup_ct_binds(ct, bindingIncludePattern, bindingExcludePattern)
if dorestart: if dorestart:
print(" start the container")
ct.start() ct.start()
return 0 return 0
# Press the green button in the gutter to run the script. # Press the green button in the gutter to run the script.
if __name__ == '__main__': if __name__ == '__main__':
dockerhost = docker.from_env() dockerhost = docker.from_env()
read_config_file()
for container in dockerhost.containers.list(all=True): for container in dockerhost.containers.list(all=True):
stop_backup_restart_container(container) stop_backup_restart_container(container)