Skip to content

Commit dc480ed

Browse files
authored
Merge pull request #431 from yungwine/mtc-db
Mtc db backups improvements
2 parents d613688 + c95a177 commit dc480ed

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

mytoncore/mytoncore.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,24 @@ def CheckConfigFile(self, fift, liteClient):
117117
subprocess.run(args)
118118
self.dbFile = mconfig_path
119119
self.Refresh()
120-
elif os.path.isfile(backup_path) == False:
121-
self.local.add_log("Create backup config file", "info")
122-
args = ["cp", mconfig_path, backup_path]
123-
subprocess.run(args)
120+
elif not os.path.isfile(backup_path) or time.time() - os.path.getmtime(backup_path) > 3600:
121+
self.local.try_function(self.create_self_db_backup)
124122
#end define
125123

124+
def create_self_db_backup(self):
125+
self.local.add_log("Create backup config file", "info")
126+
mconfig_path = self.local.buffer.db_path
127+
backup_path = mconfig_path + ".backup"
128+
backup_tmp_path = backup_path + '.tmp'
129+
subprocess.run(["cp", mconfig_path, backup_tmp_path])
130+
try:
131+
with open(backup_tmp_path, "r") as file:
132+
json.load(file)
133+
os.rename(backup_tmp_path, backup_path) # atomic opetation
134+
except:
135+
self.local.add_log("Could not update backup, backup_tmp file is broken", "warning")
136+
os.remove(backup_tmp_path)
137+
126138
def GetVarFromWorkerOutput(self, text, search):
127139
if ':' not in search:
128140
search += ':'
@@ -3037,6 +3049,7 @@ def SetSettings(self, name, data):
30373049
except: pass
30383050
self.local.db[name] = data
30393051
self.local.save()
3052+
self.create_self_db_backup()
30403053
#end define
30413054

30423055
def migrate_to_modes(self):

0 commit comments

Comments
 (0)