From 9f05ffff854edc9d96a24474546e360e627ead87 Mon Sep 17 00:00:00 2001 From: Fabien Bourgeois Date: Fri, 7 Oct 2016 12:22:46 +0200 Subject: [PATCH] [ADD]Volumes backup image with save plan --- backups/base.yml | 7 +++ backups/rsyncvolumes/Dockerfile | 18 +++++++ backups/rsyncvolumes/crontab | 4 ++ backups/rsyncvolumes/launch.sh | 5 ++ backups/rsyncvolumes/plan.tpl.py | 74 +++++++++++++++++++++++++++++ backups/rsyncvolumes/rsync.template | 3 ++ 6 files changed, 111 insertions(+) create mode 100644 backups/base.yml create mode 100644 backups/rsyncvolumes/Dockerfile create mode 100644 backups/rsyncvolumes/crontab create mode 100644 backups/rsyncvolumes/launch.sh create mode 100644 backups/rsyncvolumes/plan.tpl.py create mode 100644 backups/rsyncvolumes/rsync.template diff --git a/backups/base.yml b/backups/base.yml new file mode 100644 index 0000000..23b122c --- /dev/null +++ b/backups/base.yml @@ -0,0 +1,7 @@ +version: '2' + +services: + + rsyncvolumes: + build: ./rsyncvolumes + image: rsyncvolumes:base diff --git a/backups/rsyncvolumes/Dockerfile b/backups/rsyncvolumes/Dockerfile new file mode 100644 index 0000000..7c6198f --- /dev/null +++ b/backups/rsyncvolumes/Dockerfile @@ -0,0 +1,18 @@ +FROM debian:jessie +MAINTAINER Yaltik - Fabien Bourgeois + +ENV SOURCE /var/lib/docker/volumes +ENV DEST /var/backups/ + +RUN apt-get update && apt-get install --no-install-recommends -y \ + cron gettext-base rsync python-minimal libpython-stdlib + +COPY crontab /etc/crontab +RUN chmod 644 /etc/crontab + +WORKDIR /opt/backup +COPY rsync.template . +COPY plan.tpl.py . +COPY launch.sh . + +CMD bash launch.sh diff --git a/backups/rsyncvolumes/crontab b/backups/rsyncvolumes/crontab new file mode 100644 index 0000000..38a9a71 --- /dev/null +++ b/backups/rsyncvolumes/crontab @@ -0,0 +1,4 @@ +# hourly backup +# m h dom mon dow user command +0 * * * * root /bin/bash /opt/backup/rsync.sh +0 1 * * * root /bin/python /opt/backup/plan.py diff --git a/backups/rsyncvolumes/launch.sh b/backups/rsyncvolumes/launch.sh new file mode 100644 index 0000000..7c4b5c7 --- /dev/null +++ b/backups/rsyncvolumes/launch.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +envsubst < /opt/backup/rsync.template > /opt/backup/rsync.sh +envsubst < /opt/backup/plan.tpl.py > /opt/backup/plan.py +cron -f diff --git a/backups/rsyncvolumes/plan.tpl.py b/backups/rsyncvolumes/plan.tpl.py new file mode 100644 index 0000000..5928261 --- /dev/null +++ b/backups/rsyncvolumes/plan.tpl.py @@ -0,0 +1,74 @@ +# -*- coding: utf-8 -*- + +from zipfile import ZipFile, ZIP_DEFLATED +from datetime import datetime +from os import walk, unlink +from os.path import join as osjoin +from glob import glob + +# Backup plan for Docker volumes +# :::::::::::::::::::::::::::::: +# +# Keep : +# +# - all saves the last week +# - one save per week the last month +# - one save per month the last year + +# Volumes Backup +# ============== + +dt = datetime.now() +zipname = '{0}.zip'.format(dt.strftime('%Y-%m-%d_%H-%M-%S')) +zippath = '${DEST}' + zipname + +# Zip files + + +def zipdir(path, ziph): + # ziph is zipfile handle + for root, dirs, files in walk(path): + for file in files: + ziph.write(osjoin(root, file)) + +f = ZipFile(zippath, 'w', ZIP_DEFLATED) +zipdir('${SOURCE}', f) +f.close() + +# Filter all obsolete save files +# ============================== + + +def notkeep(fname): + fname = fname.split('/')[-1] + ds = fname.split('_') + if len(ds) <= 1: + return False + ds = ds[0] + d = datetime.strptime(ds, '%Y-%m-%d') + delta = dt - d + if delta.days > 365: + return True + elif delta.days > 31: + # if (delta.days %% 30) != 0: + if (d.day != 1): + return True + elif delta.days > 7: + # if (delta.days %% 7) != 0: + if (d.weekday() != 0) and (d.day != 1): + return True + else: + return False + +# Filters zip files and keeps only legitimate ones +# ================================================ + +files = glob('${DEST}/*.zip') +backups_to_remove = filter(notkeep, files) + +if len(backups_to_remove) > 0: + print('Save files outdated, will be deleted : \n\n* ' + + '\n* '.join(backups_to_remove)) + +for b in backups_to_remove: + unlink(b) diff --git a/backups/rsyncvolumes/rsync.template b/backups/rsyncvolumes/rsync.template new file mode 100644 index 0000000..ec87030 --- /dev/null +++ b/backups/rsyncvolumes/rsync.template @@ -0,0 +1,3 @@ +#!/bin/bash + +rsync -av ${SOURCE} ${DEST}