From 673a9e1f3df1075fb8a0f842995618b35b9b5480 Mon Sep 17 00:00:00 2001 From: Paul Kocialkowski Date: Fri, 5 Apr 2024 21:45:17 +0200 Subject: data-sync: Some rework Signed-off-by: Paul Kocialkowski --- data-sync | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 54 insertions(+), 9 deletions(-) diff --git a/data-sync b/data-sync index c8014c1..53b0214 100755 --- a/data-sync +++ b/data-sync @@ -21,6 +21,7 @@ class data_sync(): hosts = [] diff = False merge = False + verbose = False # config @@ -53,16 +54,13 @@ class data_sync(): # excludes - def excludes_prepare(self, entry): - if "excludes" not in entry: - return None - + def excludes_prepare(self, source_path, excludes): fd, path = tempfile.mkstemp() s = os.fdopen(fd, 'w') - for exclude in entry["excludes"]: - s.write(entry["path"]+exclude+"\n") + for exclude in excludes: + s.write(source_path+exclude+"\n") return path @@ -104,12 +102,54 @@ class data_sync(): rsync_command += [ self.rsync_address(source, source_path) ] rsync_command += [ self.rsync_address(sink, sink_path) ] + if self.verbose: + print("rsync command: " + str(rsync_command)) + return rsync_command # sync def sync_target(self, source, sink, target): - print("Sync target "+target+" between "+source["name"]+" and "+sink["name"]) + source_path = None + sink_path = None + + print("Sync target "+text_green+text_bold+target+text_reset+" from "+text_cyan+text_bold+source["name"]+text_reset+" to "+text_cyan+text_bold+sink["name"]+text_reset) + + excludes_path = "" + excludes = [] + + for entry in source["targets"]: + if entry["label"] == target: + source_path = os.path.join(source["base"], entry["path"]) + excludes_path = entry["path"].split("/")[-1] # meh + + if "excludes" in entry: + excludes += entry["excludes"] + + break + + for entry in sink["targets"]: + if entry["label"] == target: + sink_path = os.path.join(sink["base"], entry["path"]) + + if "excludes" in entry: + excludes += entry["excludes"] + + break + + if not source_path or not sink_path: + return 1 + + if len(excludes) > 1: + excludes_path = self.excludes_prepare(excludes_path, excludes) + else: + excludes_path = None + + command = self.rsync_command(source, source_path, sink, sink_path, excludes_path) + subprocess.call(command) + + if excludes_path: + self.excludes_cleanup(excludes_path) def sync_targets(self, source, sink, targets): targets_list = [] @@ -137,7 +177,10 @@ class data_sync(): source_path = os.path.join(base, entry["path"]) sink_path = os.path.join(sink["base"], sync_host["path"]) + "/" - excludes_path = self.excludes_prepare(entry) + if "excludes" in entry: + excludes_path = self.excludes_prepare(entry["path"], entry["excludes"]) + else: + excludes_path = None command = self.rsync_command(source, source_path, sink, sink_path, excludes_path) subprocess.call(command) @@ -182,11 +225,13 @@ class data_sync(): def main(self): global config_path - opts, args = getopt.getopt(sys.argv[1:], "mdc:") + opts, args = getopt.getopt(sys.argv[1:], "vmdc:") for key, value in opts: if key == "-m": self.merge = True + elif key == "-v": + self.verbose = True elif key == "-d": self.diff = True elif key == "-c": -- cgit v1.2.3