Skip to content

Commit

Permalink
Merge pull request #25 from probablyjassin:feat/auto-db-backup
Browse files Browse the repository at this point in the history
Feat/auto-db-backup
  • Loading branch information
probablyjassin authored Nov 9, 2024
2 parents c73d5cd + 5eeba36 commit d9950e9
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,5 @@ tests

# volumes
state/
logs/
logs/
backups/
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,5 @@ tests

# volumes
state/
logs/
logs/
backups/
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ RUN pip install --no-cache-dir -r requirements.txt

COPY . /app

# Add volume for data
# Add volumes for data
VOLUME /app/state
VOLUME /app/logs
VOLUME /app/backups

CMD ["python", "-u", "main.py"]
35 changes: 35 additions & 0 deletions cogs/tasks.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import os
import time
import random
import json
from datetime import datetime, timedelta

from discord import Activity, ActivityType, Streaming
from discord.ext import commands, tasks

from utils.data.state import state_manager
from utils.data.database import db_players, db_mogis


class tasks(commands.Cog):
Expand All @@ -14,6 +19,7 @@ def __init__(self, bot):
async def on_ready(self):
self.change_activity.start()
self.manage_state.start()
self.daily_db_backup.start()

@tasks.loop(seconds=15)
async def change_activity(self):
Expand All @@ -36,6 +42,35 @@ async def change_activity(self):
async def manage_state(self):
state_manager.backup()

@tasks.loop(time=time(hour=0))
async def daily_db_backup(self):
backup_folder = "./backups"
date_format = "%d-%m-%Y"

if not os.path.exists(backup_folder):
os.makedirs(backup_folder)

# Create the backup file
backup_filename = os.path.join(
backup_folder, f"backup_{datetime.now().strftime(date_format)}.json"
)
backup_data = {
"players": list(db_players.find()),
"mogis": list(db_mogis.find()),
}

with open(backup_filename, "w") as backup_file:
json.dump(backup_data, backup_file, indent=4)

# Remove backups older than 3 days
for filename in os.listdir(backup_folder):
file_path = os.path.join(backup_folder, filename)
if os.path.isfile(file_path):
file_date_str = filename.split("_")[1].split(".")[0]
file_date = datetime.strptime(file_date_str, date_format)
if datetime.now() - file_date > timedelta(days=3):
os.remove(file_path)


def setup(bot: commands.Bot):
bot.add_cog(tasks(bot))
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ services:
volumes:
- ./state:/app/state
- ./logs:/app/logs
- ./backups:/app/backups
env_file:
- .env

Expand Down

0 comments on commit d9950e9

Please sign in to comment.