Skip to content

Instantly share code, notes, and snippets.

@renard
Created June 5, 2019 13:30
Show Gist options
  • Save renard/d767eae997db284bfb91895badbf225c to your computer and use it in GitHub Desktop.
Save renard/d767eae997db284bfb91895badbf225c to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright © 2019 Sébastien Gross
# Created: 2019-06-05
# Last changed: 2019-06-05 15:31:13
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
## extra commands not included in barman
from argparse import SUPPRESS, ArgumentTypeError
from argh import ArghParser, arg, expects_obj, named
import barman.config
from barman import output
from barman.infofile import BackupInfo
from barman.cli import server_completer, get_server, global_config
import dateutil
import datetime
@named('find-backup')
@arg('--target-time',
completer=server_completer,
default=None,
help='specifies the target time for the command')
@arg('server_name',
completer=server_completer,
help='specifies the server name for the command')
@expects_obj
def find_backup(args):
"""
Search a backup ID for a given date-time.
"""
# Sanity check, make sure a target_time is provided
if args.target_time is None:
target_time = datetime.datetime.now()
else:
target_time = dateutil.parser.parse(args.target_time)
# Ensure a timezone information for target_time
if target_time.tzinfo is None:
target_time = target_time.replace(
tzinfo=dateutil.tz.tzlocal())
# Keep track of last backup timestamp
last_backup = datetime.datetime.now(dateutil.tz.tzlocal())
# Ensure consistent target_time
if target_time > last_backup:
target_time = last_backup
server = get_server(args)
backups = server.get_available_backups(BackupInfo.STATUS_ALL)
found = False
for key in sorted(backups.keys(), reverse=True):
# Timestamp is found in current backup
msg = '%s %s %s -> %s' % (backups[key].server_name, key, backups[key].begin_time,backups[key].end_time)
output.debug(msg)
if target_time < last_backup and \
target_time >= backups[key].begin_time:
print '%s' % key
found = True
break
last_backup = backups[key].begin_time
# If no backup found.
if found is False:
if args.target_time:
output.error('Timestamp "%s" not found.' % (args.target_time))
else:
output.error('No backup found.')
output.close_and_exit()
def main():
p = ArghParser(epilog='Barman extra commands by Sébastien Gross')
## Mandatory barman options
p.add_argument('-v', '--version', action='version',
version='%s\n\nBarman by 2ndQuadrant (www.2ndQuadrant.com)'
% barman.__version__)
p.add_argument('-c', '--config',
help='uses a configuration file '
'(defaults: %s)'
% ', '.join(barman.config.Config.CONFIG_FILES),
default=SUPPRESS)
p.add_argument('-q', '--quiet', help='be quiet', action='store_true')
p.add_argument('-d', '--debug', help='debug output', action='store_true')
p.add_argument('-f', '--format', help='output format',
choices=output.AVAILABLE_WRITERS.keys(),
default=output.DEFAULT_WRITER)
p.add_commands([find_backup])
#p.set_default_command(find_backup)
try:
p.dispatch(pre_call=global_config)
except KeyboardInterrupt:
msg = "Process interrupted by user (KeyboardInterrupt)"
output.error(msg)
except Exception as e:
msg = "%s\nSee log file for more details." % e
output.exception(msg)
output.close_and_exit()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment