diff options
| author | Andy Grover <andy@groveronline.com> | 2017-12-18 10:29:41 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-12-18 10:29:41 -0800 |
| commit | 2f209d7749b12047be0c71469bb9b4f2ae2b6e9a (patch) | |
| tree | 91e23a24e50103049a567f3df694bc880be2fa44 /targetcli | |
| parent | 4f6008b7dbb22a1df19265f5cb4c7b6787475641 (diff) | |
| parent | 6efbd7e4c0217d1f6b46e22ec51a209439678f5a (diff) | |
| download | targetcli-2f209d7749b12047be0c71469bb9b4f2ae2b6e9a.tar.gz | |
Merge pull request #98 from pkalever/config_dir
config: defend on '/etc/target/backup' directory
Diffstat (limited to 'targetcli')
| -rw-r--r-- | targetcli/ui_root.py | 59 |
1 files changed, 36 insertions, 23 deletions
diff --git a/targetcli/ui_root.py b/targetcli/ui_root.py index 8bc8521..f398395 100644 --- a/targetcli/ui_root.py +++ b/targetcli/ui_root.py @@ -23,6 +23,7 @@ import os import re import shutil import stat +import filecmp from configshell_fb import ExecutionError from rtslib_fb import RTSRoot @@ -78,31 +79,43 @@ class UIRoot(UINode): datetime.now().strftime("%Y%m%d-%H:%M:%S") + ".json" backupfile = backup_dir + "/" + backup_name backup_error = None - try: - shutil.copy(savefile, backupfile) - except IOError as ioe: - backup_error = ioe.strerror or "Unknown error" - if backup_error == None: - # Kill excess backups + if not os.path.exists(backup_dir): try: - with open(universal_prefs_file) as prefs: - backups = [line for line in prefs.read().splitlines() if re.match('^kept_backups\s*=', line)] - kept_backups = int(backups[0].split('=')[1].strip()) - except: - kept_backups = default_kept_backups - - backups = sorted(glob(os.path.dirname(savefile) + "/backup/*.json")) - files_to_unlink = list(reversed(backups))[kept_backups:] - for f in files_to_unlink: - with ignored(IOError): - os.unlink(f) - - self.shell.log.info("Last %d configs saved in %s." % \ - (kept_backups, backup_dir)) - else: - self.shell.log.warning("Could not create backup file %s: %s." % \ - (backupfile, backup_error)) + os.makedirs(backup_dir); + except OSError as exe: + raise ExecutionError("Cannot create backup directory [%s] %s." % (backup_dir, exc.strerror)) + + # Only save backups if savefile exits + if os.path.exists(savefile): + backed_files_list = sorted(glob(os.path.dirname(savefile) + "/backup/*.json")) + # Save backup if 1. backup dir is empty, or 2. savefile is differnt from recent backup copy + if not backed_files_list or not filecmp.cmp(backed_files_list[-1], savefile): + try: + shutil.copy(savefile, backupfile) + + except IOError as ioe: + backup_error = ioe.strerror or "Unknown error" + + if backup_error == None: + # Kill excess backups + try: + with open(universal_prefs_file) as prefs: + backups = [line for line in prefs.read().splitlines() if re.match('^kept_backups\s*=', line)] + kept_backups = int(backups[0].split('=')[1].strip()) + except: + kept_backups = default_kept_backups + + files_to_unlink = list(reversed(backed_files_list))[kept_backups:] + for f in files_to_unlink: + with ignored(IOError): + os.unlink(f) + + self.shell.log.info("Last %d configs saved in %s." % \ + (kept_backups, backup_dir)) + else: + self.shell.log.warning("Could not create backup file %s: %s." % \ + (backupfile, backup_error)) self.rtsroot.save_to_file(savefile) |
