restic-compose-backup/restic_volume_backup/backup_runner.py

34 lines
954 B
Python
Raw Normal View History

2019-04-17 01:28:07 +00:00
import os
2019-04-17 02:38:15 +00:00
import time
2019-04-17 01:28:07 +00:00
import docker
from restic_volume_backup.config import Config
2019-04-17 02:38:15 +00:00
def run(image: str = None, command: str = None, volumes: dict = None, enviroment: dict = None):
2019-04-17 01:45:51 +00:00
config = Config()
client = docker.DockerClient(base_url=config.docker_base_url)
2019-04-17 01:28:07 +00:00
container = client.containers.run(
2019-04-17 02:38:15 +00:00
image,
command,
2019-04-17 01:45:51 +00:00
labels={"restic-volume-backup.backup_process": 'True'},
2019-04-17 01:28:07 +00:00
auto_remove=True,
detach=True,
environment={},
volumes=volumes,
2019-04-17 01:28:07 +00:00
working_dir=os.getcwd(),
)
2019-04-17 02:38:15 +00:00
print("Backup process container:", container.name)
logs = container.logs(stdout=True, stderr=True, stream=True)
try:
while True:
time.sleep(3)
for line in logs:
print(line.decode(), end='')
# Raises requests.exceptions.HTTPError if continer is dead
container.top()
except Exception as ex:
print("Container stopped")