def backup(self):
# Wait initial time - then start loop
await asyncio.sleep(self.backupWait)
while not self.bot.is_closed:
# Initial backup - then wait
if not os.path.exists(self.backupDir):
# Create it
os.makedirs(self.backupDir)
# Flush backup
timeStamp = datetime.today().strftime("%Y-%m-%d %H.%M")
self.flushSettings("./{}/Backup-{}.json".format(self.backupDir, timeStamp))
# Get curr dir and change curr dir
retval = os.getcwd()
os.chdir(self.backupDir)
# Get reverse sorted backups
backups = sorted(os.listdir(os.getcwd()), key=os.path.getmtime)
numberToRemove = None
if len(backups) > self.backupMax:
# We have more than 100 backups right now, let's prune
numberToRemove = len(backups)-self.backupMax
for i in range(0, numberToRemove):
os.remove(backups[i])
# Restore curr dir
os.chdir(retval)
if numberToRemove:
print("Settings Backed Up ({} removed): {}".format(numberToRemove, timeStamp))
else:
print("Settings Backed Up: {}".format(timeStamp))
await asyncio.sleep(self.backupTime)
评论列表
文章目录