Skip to content

Commit 0fe1687

Browse files
committed
2 parents 1794413 + f3a230b commit 0fe1687

File tree

5 files changed

+57
-8
lines changed

5 files changed

+57
-8
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

Dockerfile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM python:3.9-slim
2+
ENV PYTHONPATH=/bot PYTHONUNBUFFERED=1
3+
WORKDIR /bot
4+
5+
COPY . .
6+
7+
RUN set -x \
8+
&& adduser --system --group --home "$PWD" --shell /bin/sh --no-create-home --disabled-password bot-user \
9+
&& chown -R bot-user:bot-user . \
10+
&& su bot-user -c "python3 -m venv venv" \
11+
&& su bot-user -c "venv/bin/pip install --upgrade pip wheel" \
12+
&& su bot-user -c "venv/bin/pip install --requirement requirements.txt" \
13+
&& rm -rf .cache
14+
15+
USER bot-user
16+
ENTRYPOINT ["/bot/venv/bin/python", "bot.py"]

bot.py

Lines changed: 12 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
@@ -53,6 +54,11 @@ async def sync(self, ctx: MessageContext):
5354
await bot.synchronise_interactions()
5455
await sync_msg.edit("Syncing done!")
5556

57+
@message_command("data_download")
58+
# @check(has_role(BOT_DEV_ROLE))
59+
async def data_download(self, ctx: MessageContext):
60+
await ctx.send("Here is the data!", file=self.storage.filename)
61+
5662
@slash_command("help", "Basic instructions and what this bot is.")
5763
async def help(self, ctx: InteractionContext):
5864
embed = Embed(
@@ -71,6 +77,11 @@ async def on_command_error(self, source: str, error: Exception, *args, **kwargs)
7177

7278

7379
bot = BytehackzBot(default_prefix="!", debug_scope=GUILD)
74-
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+
7586
bot.load_all_scales("modules")
7687
bot.start(TOKEN)

requirements.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
python-dotenv==0.19.1
2-
orjson==3.6.4
3-
numpy==1.21.4
4-
dis-snek==2.0.0
1+
python-dotenv~=0.19.1
2+
orjson~=3.6.4
3+
numpy~=1.21.4
4+
dis-snek~=2.0.0
5+
apscheduler~=3.8.1
6+
attrs~=21.2.0

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)