[ADD]Volumes backup image with save plan
This commit is contained in:
parent
835aca780d
commit
9f05ffff85
7
backups/base.yml
Normal file
7
backups/base.yml
Normal file
@ -0,0 +1,7 @@
|
||||
version: '2'
|
||||
|
||||
services:
|
||||
|
||||
rsyncvolumes:
|
||||
build: ./rsyncvolumes
|
||||
image: rsyncvolumes:base
|
18
backups/rsyncvolumes/Dockerfile
Normal file
18
backups/rsyncvolumes/Dockerfile
Normal file
@ -0,0 +1,18 @@
|
||||
FROM debian:jessie
|
||||
MAINTAINER Yaltik - Fabien Bourgeois <fabien@yaltik.com>
|
||||
|
||||
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
|
4
backups/rsyncvolumes/crontab
Normal file
4
backups/rsyncvolumes/crontab
Normal file
@ -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
|
5
backups/rsyncvolumes/launch.sh
Normal file
5
backups/rsyncvolumes/launch.sh
Normal file
@ -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
|
74
backups/rsyncvolumes/plan.tpl.py
Normal file
74
backups/rsyncvolumes/plan.tpl.py
Normal file
@ -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)
|
3
backups/rsyncvolumes/rsync.template
Normal file
3
backups/rsyncvolumes/rsync.template
Normal file
@ -0,0 +1,3 @@
|
||||
#!/bin/bash
|
||||
|
||||
rsync -av ${SOURCE} ${DEST}
|
Loading…
Reference in New Issue
Block a user