diff --git a/mytoncore/mytoncore.py b/mytoncore/mytoncore.py index 22d42bd7..63c571ed 100644 --- a/mytoncore/mytoncore.py +++ b/mytoncore/mytoncore.py @@ -117,12 +117,24 @@ def CheckConfigFile(self, fift, liteClient): subprocess.run(args) self.dbFile = mconfig_path self.Refresh() - elif os.path.isfile(backup_path) == False: - self.local.add_log("Create backup config file", "info") - args = ["cp", mconfig_path, backup_path] - subprocess.run(args) + elif not os.path.isfile(backup_path) or time.time() - os.path.getmtime(backup_path) > 3600: + 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 += ':' @@ -3037,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):