2016-10-07 12:22:46 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from zipfile import ZipFile, ZIP_DEFLATED
|
|
|
|
from datetime import datetime
|
2021-03-12 19:48:43 +01:00
|
|
|
from os import walk, unlink, environ
|
2016-10-07 12:22:46 +02:00
|
|
|
from os.path import join as osjoin
|
2017-07-03 10:01:07 +02:00
|
|
|
from os.path import isfile
|
2016-10-07 12:22:46 +02:00
|
|
|
from glob import glob
|
2021-03-12 19:48:43 +01:00
|
|
|
from remote import sync
|
2016-10-07 12:22:46 +02:00
|
|
|
|
|
|
|
# Backup plan for Docker volumes
|
|
|
|
# ::::::::::::::::::::::::::::::
|
|
|
|
#
|
|
|
|
# Keep :
|
|
|
|
#
|
|
|
|
# - all saves the last week
|
|
|
|
# - one save per week the last month
|
2021-03-12 19:48:43 +01:00
|
|
|
# - one save per month during 6 months
|
2016-10-07 12:22:46 +02:00
|
|
|
|
|
|
|
# Volumes Backup
|
|
|
|
# ==============
|
|
|
|
|
2021-03-12 19:48:43 +01:00
|
|
|
DEST = environ.get('DEST')
|
2016-10-07 12:22:46 +02:00
|
|
|
dt = datetime.now()
|
2016-10-08 07:45:00 +02:00
|
|
|
zipname = '{0}.zip'.format(dt.strftime('%Y-%m-%d_%Hh%Mm%Ss'))
|
2021-03-12 19:48:43 +01:00
|
|
|
zippath = DEST + zipname
|
2016-10-07 12:22:46 +02:00
|
|
|
|
|
|
|
# Zip files
|
|
|
|
|
2021-03-13 08:31:38 +01:00
|
|
|
EXCLUDE_WORDS = environ.get('EXCLUDE_WORDS', False)
|
|
|
|
if EXCLUDE_WORDS:
|
|
|
|
EXCLUDE_WORDS = EXCLUDE_WORDS.split(',')
|
2016-10-07 12:22:46 +02:00
|
|
|
|
|
|
|
def zipdir(path, ziph):
|
|
|
|
# ziph is zipfile handle
|
|
|
|
for root, dirs, files in walk(path):
|
|
|
|
for file in files:
|
2017-07-19 08:11:36 +02:00
|
|
|
pfile = osjoin(root, file)
|
2021-03-13 08:38:44 +01:00
|
|
|
is_excluded = EXCLUDE_WORDS and any(map(lambda w: w in pfile, EXCLUDE_WORDS))
|
|
|
|
if (isfile(pfile) and not is_excluded):
|
2017-07-19 08:11:36 +02:00
|
|
|
ziph.write(pfile)
|
2016-10-07 12:22:46 +02:00
|
|
|
|
2021-03-12 19:48:43 +01:00
|
|
|
print('Compression started')
|
2017-07-19 08:53:39 +02:00
|
|
|
f = ZipFile(zippath, 'w', ZIP_DEFLATED, allowZip64=True)
|
2021-03-12 19:48:43 +01:00
|
|
|
zipdir('%s/volumes' % DEST, f)
|
2016-10-07 12:22:46 +02:00
|
|
|
f.close()
|
2021-03-12 19:48:43 +01:00
|
|
|
print('Compression ended')
|
2016-10-07 12:22:46 +02:00
|
|
|
|
|
|
|
# Filter all obsolete save files
|
|
|
|
# ==============================
|
|
|
|
|
2021-03-12 19:48:43 +01:00
|
|
|
PLAN = environ.get('PLAN', u'180,15,2')
|
|
|
|
PLAN = map(int, PLAN.split(u','))
|
2016-10-07 12:22:46 +02:00
|
|
|
|
|
|
|
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
|
2021-03-12 19:48:43 +01:00
|
|
|
if delta.days > PLAN[0]:
|
2016-10-07 12:22:46 +02:00
|
|
|
return True
|
2021-03-12 19:48:43 +01:00
|
|
|
elif delta.days > PLAN[1]:
|
|
|
|
if d.day != 1:
|
2016-10-07 12:22:46 +02:00
|
|
|
return True
|
2021-03-12 19:48:43 +01:00
|
|
|
elif delta.days > PLAN[2]:
|
2016-10-07 12:22:46 +02:00
|
|
|
if (d.weekday() != 0) and (d.day != 1):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Filters zip files and keeps only legitimate ones
|
|
|
|
# ================================================
|
|
|
|
|
2021-03-12 19:48:43 +01:00
|
|
|
files = glob('%s/*.zip' % DEST)
|
2016-10-07 12:22:46 +02:00
|
|
|
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)
|
2021-03-12 19:48:43 +01:00
|
|
|
|
|
|
|
sync()
|