From 02bfb3b580fcc30338a5158b1901218e4b83390a Mon Sep 17 00:00:00 2001 From: yungwine Date: Sun, 16 Feb 2025 12:26:49 +0400 Subject: [PATCH 1/2] create mtc db backup more often --- mytoncore/mytoncore.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/mytoncore/mytoncore.py b/mytoncore/mytoncore.py index 22d42bd7..e9f2412f 100644 --- a/mytoncore/mytoncore.py +++ b/mytoncore/mytoncore.py @@ -117,10 +117,17 @@ def CheckConfigFile(self, fift, liteClient): subprocess.run(args) self.dbFile = mconfig_path self.Refresh() - elif os.path.isfile(backup_path) == False: + elif not os.path.isfile(backup_path) or time.time() - os.path.getmtime(backup_path) > 3600: self.local.add_log("Create backup config file", "info") - args = ["cp", mconfig_path, backup_path] - subprocess.run(args) + backup_tmp_path = backup_path + '.tmp' + subprocess.run(["cp", mconfig_path, backup_tmp_path]) + try: + with open(backup_tmp_path, "r") as file: + json.load(file) + os.rename(backup_tmp_path, backup_path) # atomic opetation + except: + self.local.add_log("Could not update backup, backup_tmp file is broken", "warning") + os.remove(backup_tmp_path) #end define def GetVarFromWorkerOutput(self, text, search): From c95a177181b8ee6dd3e608941e5f1f6257edbff2 Mon Sep 17 00:00:00 2001 From: yungwine Date: Mon, 17 Feb 2025 13:23:37 +0400 Subject: [PATCH 2/2] create mtc db backup after every set --- mytoncore/mytoncore.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/mytoncore/mytoncore.py b/mytoncore/mytoncore.py index e9f2412f..63c571ed 100644 --- a/mytoncore/mytoncore.py +++ b/mytoncore/mytoncore.py @@ -118,18 +118,23 @@ def CheckConfigFile(self, fift, liteClient): self.dbFile = mconfig_path self.Refresh() elif not os.path.isfile(backup_path) or time.time() - os.path.getmtime(backup_path) > 3600: - self.local.add_log("Create backup config file", "info") - backup_tmp_path = backup_path + '.tmp' - subprocess.run(["cp", mconfig_path, backup_tmp_path]) - try: - with open(backup_tmp_path, "r") as file: - json.load(file) - os.rename(backup_tmp_path, backup_path) # atomic opetation - except: - self.local.add_log("Could not update backup, backup_tmp file is broken", "warning") - os.remove(backup_tmp_path) + self.local.try_function(self.create_self_db_backup) #end define + def create_self_db_backup(self): + self.local.add_log("Create backup config file", "info") + mconfig_path = self.local.buffer.db_path + backup_path = mconfig_path + ".backup" + backup_tmp_path = backup_path + '.tmp' + subprocess.run(["cp", mconfig_path, backup_tmp_path]) + try: + with open(backup_tmp_path, "r") as file: + json.load(file) + os.rename(backup_tmp_path, backup_path) # atomic opetation + except: + self.local.add_log("Could not update backup, backup_tmp file is broken", "warning") + os.remove(backup_tmp_path) + def GetVarFromWorkerOutput(self, text, search): if ':' not in search: search += ':' @@ -3044,6 +3049,7 @@ def SetSettings(self, name, data): except: pass self.local.db[name] = data self.local.save() + self.create_self_db_backup() #end define def migrate_to_modes(self):