|
|
eb47f5 |
From 6efbd7e4c0217d1f6b46e22ec51a209439678f5a Mon Sep 17 00:00:00 2001
|
|
|
eb47f5 |
From: Prasanna Kumar Kalever <prasanna.kalever@redhat.com>
|
|
|
eb47f5 |
Date: Mon, 18 Dec 2017 15:25:42 +0530
|
|
|
eb47f5 |
Subject: [PATCH] config: backup when current config is different from recent
|
|
|
eb47f5 |
backup copy
|
|
|
eb47f5 |
|
|
|
eb47f5 |
With this change,
|
|
|
eb47f5 |
when '/etc/target/saveconfig.json' is same as '/etc/target/backup/saveconfig-${latest-stamp}.json
|
|
|
eb47f5 |
we skip backing up saveconfig.json
|
|
|
eb47f5 |
|
|
|
eb47f5 |
Signed-off-by: Prasanna Kumar Kalever <prasanna.kalever@redhat.com>
|
|
|
eb47f5 |
---
|
|
|
eb47f5 |
targetcli/ui_root.py | 53 ++++++++++++++++++++++++++++------------------------
|
|
|
eb47f5 |
1 file changed, 29 insertions(+), 24 deletions(-)
|
|
|
eb47f5 |
|
|
|
eb47f5 |
diff --git a/targetcli/ui_root.py b/targetcli/ui_root.py
|
|
|
eb47f5 |
index 33bb948..f398395 100644
|
|
|
eb47f5 |
--- a/targetcli/ui_root.py
|
|
|
eb47f5 |
+++ b/targetcli/ui_root.py
|
|
|
eb47f5 |
@@ -23,6 +23,7 @@ import os
|
|
|
eb47f5 |
import re
|
|
|
eb47f5 |
import shutil
|
|
|
eb47f5 |
import stat
|
|
|
eb47f5 |
+import filecmp
|
|
|
eb47f5 |
|
|
|
eb47f5 |
from configshell_fb import ExecutionError
|
|
|
eb47f5 |
from rtslib_fb import RTSRoot
|
|
|
eb47f5 |
@@ -85,32 +86,36 @@ class UIRoot(UINode):
|
|
|
eb47f5 |
except OSError as exe:
|
|
|
eb47f5 |
raise ExecutionError("Cannot create backup directory [%s] %s." % (backup_dir, exc.strerror))
|
|
|
eb47f5 |
|
|
|
eb47f5 |
+ # Only save backups if savefile exits
|
|
|
eb47f5 |
if os.path.exists(savefile):
|
|
|
eb47f5 |
- try:
|
|
|
eb47f5 |
- shutil.copy(savefile, backupfile)
|
|
|
eb47f5 |
- except IOError as ioe:
|
|
|
eb47f5 |
- backup_error = ioe.strerror or "Unknown error"
|
|
|
eb47f5 |
-
|
|
|
eb47f5 |
- if backup_error == None:
|
|
|
eb47f5 |
- # Kill excess backups
|
|
|
eb47f5 |
+ backed_files_list = sorted(glob(os.path.dirname(savefile) + "/backup/*.json"))
|
|
|
eb47f5 |
+ # Save backup if 1. backup dir is empty, or 2. savefile is differnt from recent backup copy
|
|
|
eb47f5 |
+ if not backed_files_list or not filecmp.cmp(backed_files_list[-1], savefile):
|
|
|
eb47f5 |
try:
|
|
|
eb47f5 |
- with open(universal_prefs_file) as prefs:
|
|
|
eb47f5 |
- backups = [line for line in prefs.read().splitlines() if re.match('^kept_backups\s*=', line)]
|
|
|
eb47f5 |
- kept_backups = int(backups[0].split('=')[1].strip())
|
|
|
eb47f5 |
- except:
|
|
|
eb47f5 |
- kept_backups = default_kept_backups
|
|
|
eb47f5 |
-
|
|
|
eb47f5 |
- backups = sorted(glob(os.path.dirname(savefile) + "/backup/*.json"))
|
|
|
eb47f5 |
- files_to_unlink = list(reversed(backups))[kept_backups:]
|
|
|
eb47f5 |
- for f in files_to_unlink:
|
|
|
eb47f5 |
- with ignored(IOError):
|
|
|
eb47f5 |
- os.unlink(f)
|
|
|
eb47f5 |
-
|
|
|
eb47f5 |
- self.shell.log.info("Last %d configs saved in %s." % \
|
|
|
eb47f5 |
- (kept_backups, backup_dir))
|
|
|
eb47f5 |
- else:
|
|
|
eb47f5 |
- self.shell.log.warning("Could not create backup file %s: %s." % \
|
|
|
eb47f5 |
- (backupfile, backup_error))
|
|
|
eb47f5 |
+ shutil.copy(savefile, backupfile)
|
|
|
eb47f5 |
+
|
|
|
eb47f5 |
+ except IOError as ioe:
|
|
|
eb47f5 |
+ backup_error = ioe.strerror or "Unknown error"
|
|
|
eb47f5 |
+
|
|
|
eb47f5 |
+ if backup_error == None:
|
|
|
eb47f5 |
+ # Kill excess backups
|
|
|
eb47f5 |
+ try:
|
|
|
eb47f5 |
+ with open(universal_prefs_file) as prefs:
|
|
|
eb47f5 |
+ backups = [line for line in prefs.read().splitlines() if re.match('^kept_backups\s*=', line)]
|
|
|
eb47f5 |
+ kept_backups = int(backups[0].split('=')[1].strip())
|
|
|
eb47f5 |
+ except:
|
|
|
eb47f5 |
+ kept_backups = default_kept_backups
|
|
|
eb47f5 |
+
|
|
|
eb47f5 |
+ files_to_unlink = list(reversed(backed_files_list))[kept_backups:]
|
|
|
eb47f5 |
+ for f in files_to_unlink:
|
|
|
eb47f5 |
+ with ignored(IOError):
|
|
|
eb47f5 |
+ os.unlink(f)
|
|
|
eb47f5 |
+
|
|
|
eb47f5 |
+ self.shell.log.info("Last %d configs saved in %s." % \
|
|
|
eb47f5 |
+ (kept_backups, backup_dir))
|
|
|
eb47f5 |
+ else:
|
|
|
eb47f5 |
+ self.shell.log.warning("Could not create backup file %s: %s." % \
|
|
|
eb47f5 |
+ (backupfile, backup_error))
|
|
|
eb47f5 |
|
|
|
eb47f5 |
self.rtsroot.save_to_file(savefile)
|
|
|
eb47f5 |
|
|
|
eb47f5 |
--
|
|
|
eb47f5 |
1.8.3.1
|
|
|
eb47f5 |
|