From: Frank Brehm Date: Fri, 10 Nov 2017 10:21:50 +0000 (+0100) Subject: Adding option to keep a backup of old configuration file X-Git-Tag: 0.1.2~83 X-Git-Url: https://git.uhu-banane.de/?a=commitdiff_plain;h=32197b10c184c02e7a1421dcc363f6c3b66983d5;p=pixelpark%2Fadmin-tools.git Adding option to keep a backup of old configuration file --- diff --git a/pp_lib/deploy_zones_from_pdns.py b/pp_lib/deploy_zones_from_pdns.py index 7ca8579..5601735 100644 --- a/pp_lib/deploy_zones_from_pdns.py +++ b/pp_lib/deploy_zones_from_pdns.py @@ -34,7 +34,7 @@ import requests from six.moves.urllib.parse import urlunsplit # Own modules -from .common import pp, compare_fqdn, to_str +from .common import pp, compare_fqdn, to_str, to_bool from .common import RE_DOT_AT_END from .pdns_app import PpPDNSAppError, PpPDNSApplication @@ -44,7 +44,7 @@ from .pdns_record import compare_rrsets from .pidfile import PidFileError, InvalidPidFileError, PidFileInUseError, PidFile -__version__ = '0.4.1' +__version__ = '0.4.2' LOG = logging.getLogger(__name__) @@ -120,6 +120,7 @@ class PpDeployZonesApp(PpPDNSApplication): self.tempdir = None self.temp_zones_cfg_file = None self.keep_tempdir = False + self.keep_backup = False self.backup_suffix = ( '.' + datetime.datetime.utcnow().strftime('%Y-%m-%d_%H-%M-%S') + '.bak') @@ -171,6 +172,11 @@ class PpDeployZonesApp(PpPDNSApplication): super(PpDeployZonesApp, self).init_arg_parser() + self.arg_parser.add_argument( + '-B', '--backup', dest="keep_backup", action='store_true', + help=("Keep a backup file for each changed configuration file."), + ) + self.arg_parser.add_argument( '-K', '--keep-tempdir', dest='keep_tempdir', action='store_true', help=( @@ -190,6 +196,9 @@ class PpDeployZonesApp(PpPDNSApplication): if self.args.keep_tempdir: self.keep_tempdir = True + if self.args.keep_backup: + self.keep_backup = True + # ------------------------------------------------------------------------- def perform_config(self): @@ -204,6 +213,10 @@ class PpDeployZonesApp(PpPDNSApplication): if section_name.lower() == 'app': self._check_path_config(section, section_name, 'pidfile', 'pidfile_name', True) + if 'keep-backup' in section: + self.keep_backup = to_bool(section['keep-backup']) + if 'keep_backup' in section: + self.keep_backup = to_bool(section['keep_backup']) if section_name.lower() == 'named': self.set_named_options(section, section_name) @@ -355,9 +368,12 @@ class PpDeployZonesApp(PpPDNSApplication): backup_file = self.moved_files[tgt_file] LOG.debug("Searching for {!r}.".format(backup_file)) if os.path.exists(backup_file): - LOG.info("Removing {!r} ...".format(backup_file)) - if not self.simulate: - os.remove(backup_file) + if self.keep_backup: + LOG.info("Keep existing backup file {!r}.".format(backup_file)) + else: + LOG.info("Removing {!r} ...".format(backup_file)) + if not self.simulate: + os.remove(backup_file) # ----------------------- def emit_rm_err(function, path, excinfo):