2018-09-03 17:20:53 +00:00
|
|
|
import datetime, glob, os, shutil, time
|
2018-10-12 17:07:23 +00:00
|
|
|
from src import ModuleManager, utils
|
2018-09-03 17:20:53 +00:00
|
|
|
|
|
|
|
BACKUP_INTERVAL = 60*60 # 1 hour
|
|
|
|
BACKUP_COUNT = 5
|
|
|
|
|
2018-10-12 17:07:23 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
|
|
|
def on_load(self):
|
2018-09-03 17:20:53 +00:00
|
|
|
now = datetime.datetime.now()
|
|
|
|
until_next_hour = 60-now.second
|
|
|
|
until_next_hour += ((60-(now.minute+1))*60)
|
|
|
|
|
2019-10-08 12:49:43 +00:00
|
|
|
self.timers.add("database-backup", self._backup, BACKUP_INTERVAL,
|
2018-09-28 15:51:36 +00:00
|
|
|
time.time()+until_next_hour)
|
2018-09-03 17:20:53 +00:00
|
|
|
|
2019-10-08 12:49:43 +00:00
|
|
|
def _backup(self, timer):
|
2018-09-27 12:16:27 +00:00
|
|
|
location = self.bot.database.location
|
2019-10-14 17:13:43 +00:00
|
|
|
files = glob.glob("%s.*.back" % location)
|
2018-09-03 17:20:53 +00:00
|
|
|
files = sorted(files)
|
|
|
|
|
2019-10-14 17:07:18 +00:00
|
|
|
while len(files) > 4:
|
|
|
|
os.remove(files[-1])
|
|
|
|
files.pop(-1)
|
2018-09-03 17:20:53 +00:00
|
|
|
|
|
|
|
suffix = datetime.datetime.now().strftime("%y-%m-%d.%H:%M:%S")
|
2019-10-14 17:13:43 +00:00
|
|
|
backup_file = "%s.%s.back" % (location, suffix)
|
2018-09-27 12:16:27 +00:00
|
|
|
shutil.copy2(location, backup_file)
|
2018-09-03 17:20:53 +00:00
|
|
|
|
2019-10-08 16:03:57 +00:00
|
|
|
timer.redo()
|