From a0c6de4c33a42bf95b7c5a032dcd5ada4b6223fe Mon Sep 17 00:00:00 2001 From: chris Date: Wed, 16 Nov 2016 10:44:03 +0100 Subject: [PATCH] initial --- Dockerfile | 2 ++ backup.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 2 ++ 3 files changed, 65 insertions(+) create mode 100644 Dockerfile create mode 100644 backup.py create mode 100644 requirements.txt diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a3dbb32 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,2 @@ +FROM python:2-onbuild +CMD ['python', './backup.py'] diff --git a/backup.py b/backup.py new file mode 100644 index 0000000..990cd3a --- /dev/null +++ b/backup.py @@ -0,0 +1,61 @@ +import argparse +import subprocess +from datetime import datetime + +from apscheduler.schedulers.background import BackgroundScheduler +from docker import Client + + +scheduler = BackgroundScheduler() + + +def backup(socket, outdir, stack, services): + api = Client(base_url=args.socket) + for service in services: + containers = api.containers( + filters={ + 'label': "io.rancher.project_service.name={}/{}".format( + stack, + service, + ) + } + ) + + for container in containers: + + for mount in container.get('Mounts'): + name = mount.get('Name') + source = mount.get('Source') + + outfile="{outdir}/{stack}-{service}-{name}-{date}-{container}.tgz".format( + outdir=outdir, + stack=stack, + service=service, + name=name, + date=datetime.now().strftime('%Y%m%d-%H%M%S'), + container=container.get('Id')[:8], + ) + subprocess.call([ + "tar", + "-cpzf", + outfile, + source, + ]) + +parser = argparse.ArgumentParser(description="backup a rancher services docker volumes") +parser.add_argument('socket', default='unix://var/run/docker.sock', + help="docker socket") +parser.add_argument('outdir') +parser.add_argument('stack', help="rancher stack name") +parser.add_argument('services', metavar='service', nargs='+', + help="rancher service names") +args = parser.parse_args() +backup(args.socket, args.outdir, args.stack, args.services) + +scheduler.start() +scheduler.add_job( + func=backup, + trigger='interval', + days=1, + args=(args.socket, args.outdir, args.stack, args.services), +) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..73f587e --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +apscheduler +docker-py