Skip to content

Commit 713eb55

Browse files
committed
Add backup up of data.
1 parent 9e6b6fb commit 713eb55

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Custom
2+
backup/*
23
data.json
34

45
# Byte-compiled / optimized / DLL files

bot.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22
from pathlib import Path
33

4+
from apscheduler.schedulers.asyncio import AsyncIOScheduler
45
from dis_snek.client import Snake
56
from dis_snek.errors import CommandCheckFailure
67
from dis_snek.mixins.send import SendMixin
@@ -76,6 +77,11 @@ async def on_command_error(self, source: str, error: Exception, *args, **kwargs)
7677

7778

7879
bot = BytehackzBot(default_prefix="!", debug_scope=GUILD)
79-
bot.storage = JsonStorage(bot, "data.json")
80+
81+
bot.storage = JsonStorage("data.json", "./backup", 20)
82+
scheduler = AsyncIOScheduler()
83+
scheduler.add_job(bot.storage.backup, 'interval', minutes=5)
84+
scheduler.start()
85+
8086
bot.load_all_scales("modules")
8187
bot.start(TOKEN)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ python-dotenv==0.19.1
22
orjson==3.6.4
33
numpy==1.21.4
44
dis-snek==2.0.0
5+
apscheduler==3.8.1

storage/storage.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import datetime
2+
import os
13
from pathlib import Path
24

35
import attr
@@ -15,9 +17,10 @@ class Container(DictSerializationMixin):
1517

1618

1719
class JsonStorage:
18-
def __init__(self, bot, filename: str):
19-
self.bot = bot
20+
def __init__(self, filename: str, backup_folder: str, max_backups=5):
2021
self.filename = Path(filename)
22+
self.backup_folder = Path(backup_folder)
23+
self.max_backups = max_backups
2124
self.container = None
2225
self._init_data()
2326

@@ -29,7 +32,23 @@ def _init_data(self):
2932
else:
3033
self.container = Container()
3134

35+
self.backup_folder.mkdir(exist_ok=True)
36+
3237
def save(self):
33-
with open(self.filename, "wb") as file:
38+
self._save_file(self.filename)
39+
40+
def backup(self):
41+
backup_filename = f"backup-{datetime.datetime.now().timestamp()}.json"
42+
backup_path = self.backup_folder.joinpath(backup_filename)
43+
self._save_file(backup_path)
44+
45+
backup_files = sorted(os.listdir(self.backup_folder), key=lambda file: os.path.getctime(self.backup_folder.joinpath(file).absolute()))
46+
if len(backup_files) > self.max_backups:
47+
os.remove(self.backup_folder.joinpath(backup_files[0]).absolute())
48+
49+
print("Backup done")
50+
51+
def _save_file(self, path):
52+
with open(path, "wb") as file:
3453
data = orjson.dumps(self.container.to_dict())
3554
file.write(data)

0 commit comments

Comments
 (0)