From 0ac0255e8e89784dc95e8981a82dd47ec620f19d Mon Sep 17 00:00:00 2001 From: Antony Chazapis Date: Tue, 16 Jul 2019 13:58:19 +0300 Subject: [PATCH] Allow configuring the prefix used in snapshot names --- pyznap/clean.py | 4 +++- pyznap/take.py | 20 +++++++++++--------- pyznap/utils.py | 6 ++++-- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/pyznap/clean.py b/pyznap/clean.py index a7b981a..62e0556 100644 --- a/pyznap/clean.py +++ b/pyznap/clean.py @@ -56,10 +56,12 @@ def clean_filesystem(filesystem, conf): logger = logging.getLogger(__name__) logger.debug('Cleaning snapshots on {}...'.format(filesystem)) + prefix = conf.get('prefix', 'pyznap') + snapshots = {'frequent': [], 'hourly': [], 'daily': [], 'weekly': [], 'monthly': [], 'yearly': []} for snap in filesystem.snapshots(): # Ignore snapshots not taken with pyznap or sanoid - if not snap.name.split('@')[1].startswith(('pyznap', 'autosnap')): + if not snap.name.split('@')[1].startswith(('pyznap', 'autosnap', prefix)): continue snap_type = snap.name.split('_')[-1] diff --git a/pyznap/take.py b/pyznap/take.py index 2c833c8..7433640 100644 --- a/pyznap/take.py +++ b/pyznap/take.py @@ -17,7 +17,7 @@ from .process import DatasetBusyError, DatasetNotFoundError, DatasetExistsError -def take_snap(filesystem, datetime_format, _type): +def take_snap(filesystem, prefix, datetime_format, _type): """Takes a snapshot of type '_type' Parameters @@ -31,7 +31,7 @@ def take_snap(filesystem, datetime_format, _type): logger = logging.getLogger(__name__) now = datetime.now - snapname = lambda _type: 'pyznap_{:s}_{:s}'.format(now().strftime(datetime_format), _type) + snapname = lambda _type: '{:s}_{:s}_{:s}'.format(prefix, now().strftime(datetime_format), _type) logger.info('Taking snapshot {}@{:s}...'.format(filesystem, snapname(_type))) try: @@ -62,6 +62,8 @@ def take_filesystem(filesystem, conf): logger.debug('Taking snapshots on {}...'.format(filesystem)) now = datetime.now + prefix = conf.get('prefix', 'pyznap') + date_format = conf.get('date_format', '%Y-%m-%d') time_format = conf.get('time_format', '%H:%M:%S') datetime_format = '{:s}_{:s}'.format(date_format, time_format) @@ -69,7 +71,7 @@ def take_filesystem(filesystem, conf): snapshots = {'frequent': [], 'hourly': [], 'daily': [], 'weekly': [], 'monthly': [], 'yearly': []} for snap in filesystem.snapshots(): # Ignore snapshots not taken with pyznap or sanoid - if not snap.name.split('@')[1].startswith(('pyznap', 'autosnap')): + if not snap.name.split('@')[1].startswith(('pyznap', 'autosnap', prefix)): continue try: _date, _time, snap_type = snap.name.split('_')[-3:] @@ -84,32 +86,32 @@ def take_filesystem(filesystem, conf): if conf['yearly'] and (not snapshots['yearly'] or snapshots['yearly'][0][1].year != now().year): - take_snap(filesystem, datetime_format, 'yearly') + take_snap(filesystem, prefix, datetime_format, 'yearly') if conf['monthly'] and (not snapshots['monthly'] or snapshots['monthly'][0][1].month != now().month or now() - snapshots['monthly'][0][1] > timedelta(days=31)): - take_snap(filesystem, datetime_format, 'monthly') + take_snap(filesystem, prefix, datetime_format, 'monthly') if conf['weekly'] and (not snapshots['weekly'] or snapshots['weekly'][0][1].isocalendar()[1] != now().isocalendar()[1] or now() - snapshots['weekly'][0][1] > timedelta(days=7)): - take_snap(filesystem, datetime_format, 'weekly') + take_snap(filesystem, prefix, datetime_format, 'weekly') if conf['daily'] and (not snapshots['daily'] or snapshots['daily'][0][1].day != now().day or now() - snapshots['daily'][0][1] > timedelta(days=1)): - take_snap(filesystem, datetime_format, 'daily') + take_snap(filesystem, prefix, datetime_format, 'daily') if conf['hourly'] and (not snapshots['hourly'] or snapshots['hourly'][0][1].hour != now().hour or now() - snapshots['hourly'][0][1] > timedelta(hours=1)): - take_snap(filesystem, datetime_format, 'hourly') + take_snap(filesystem, prefix, datetime_format, 'hourly') if conf['frequent'] and (not snapshots['frequent'] or snapshots['frequent'][0][1].minute != now().minute or now() - snapshots['frequent'][0][1] > timedelta(minutes=1)): - take_snap(filesystem, datetime_format, 'frequent') + take_snap(filesystem, prefix, datetime_format, 'frequent') def take_config(config): diff --git a/pyznap/utils.py b/pyznap/utils.py index 9373da4..ffe5eeb 100644 --- a/pyznap/utils.py +++ b/pyznap/utils.py @@ -91,7 +91,7 @@ def read_config(path): config = [] options = ['key', 'frequent', 'hourly', 'daily', 'weekly', 'monthly', 'yearly', 'snap', 'clean', - 'dest', 'dest_keys', 'compress', 'date_format', 'time_format'] + 'dest', 'dest_keys', 'compress', 'prefix', 'date_format', 'time_format'] for section in parser.sections(): dic = {} @@ -115,6 +115,8 @@ def read_config(path): elif option in ['dest_keys']: dic[option] = [i.strip() if os.path.isfile(i.strip()) else None for i in value.split(',')] + elif option in ['prefix']: + dic[option] = value.strip().replace('_', '') elif option in ['date_format', 'time_format']: dic[option] = value.strip() # Pass through values recursively @@ -125,7 +127,7 @@ def read_config(path): child_parent = '/'.join(child['name'].split('/')[:-1]) # get parent of child filesystem if child_parent.startswith(parent['name']): for option in ['key', 'frequent', 'hourly', 'daily', 'weekly', 'monthly', 'yearly', - 'snap', 'clean', 'date_format', 'time_format']: + 'snap', 'clean', 'prefix', 'date_format', 'time_format']: child[option] = child[option] if child[option] is not None else parent[option] # Sort by pathname config = sorted(config, key=lambda entry: entry['name'].split('/'))