2018-09-03 17:20:53 +00:00
|
|
|
import datetime, glob, os, shutil, time
|
2018-09-26 17:27:17 +00:00
|
|
|
from src import Utils
|
2018-09-03 17:20:53 +00:00
|
|
|
|
|
|
|
BACKUP_INTERVAL = 60*60 # 1 hour
|
|
|
|
BACKUP_COUNT = 5
|
|
|
|
|
|
|
|
class Module(object):
|
|
|
|
def __init__(self, bot, events, exports):
|
|
|
|
self.bot = bot
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
until_next_hour = 60-now.second
|
|
|
|
until_next_hour += ((60-(now.minute+1))*60)
|
|
|
|
|
|
|
|
bot.add_timer("database-backup", BACKUP_INTERVAL, persist=False,
|
|
|
|
next_due=time.time()+until_next_hour)
|
|
|
|
|
2018-09-26 17:27:17 +00:00
|
|
|
@Utils.hook("timer.database-backup")
|
2018-09-03 17:20:53 +00:00
|
|
|
def backup(self, event):
|
2018-09-27 12:16:27 +00:00
|
|
|
location = self.bot.database.location
|
2018-09-27 13:06:46 +00:00
|
|
|
files = glob.glob("%s.*" % location)
|
2018-09-03 17:20:53 +00:00
|
|
|
files = sorted(files)
|
|
|
|
|
|
|
|
if len(files) == 5:
|
|
|
|
os.remove(files[0])
|
|
|
|
|
|
|
|
suffix = datetime.datetime.now().strftime("%y-%m-%d.%H:%M:%S")
|
2018-09-27 12:16:27 +00:00
|
|
|
backup_file = "%s.%s" % (location, suffix)
|
|
|
|
shutil.copy2(location, backup_file)
|
2018-09-03 17:20:53 +00:00
|
|
|
|
|
|
|
event["timer"].redo()
|