This commit is contained in:
2024-06-04 22:08:15 +02:00
commit e899252f31
74 changed files with 1969 additions and 0 deletions

53
app/db/tasks.py Normal file
View File

@@ -0,0 +1,53 @@
from django.db.models import Prefetch, Q
from django.utils import timezone
import celery
from datetime import timedelta
from .models import DBBackup, DB
from .backup_engine import MySQLBackupEngine, PostgreSQLBackupEngine
@celery.shared_task
def start_backup(backup_id):
backupdb = DBBackup.objects.get(id=backup_id)
# task_id can be none if it's not a celery task (for ex : if running the function without apply it)
task_id = start_backup.request.id
klass = None
match backupdb.db.db_type:
case "mysql":
klass = MySQLBackupEngine
case "postgres":
klass = PostgreSQLBackupEngine
case _:
pass
if klass:
be = klass(backupdb=backupdb)
be.run(task_id)
# if start_backup.request.id:
# # running from celery task
# pass
# else:
# # running from standard function
# pass
@celery.shared_task
def job_check_daily_backup():
now = timezone.now()
for db in DB.objects.all():
# check if need backup
last_backup: DBBackup = (db.backups.order_by("-date_created")
.filter(Q(status="ok") | Q(status="running") | Q(status="waiting"))
.first())
if not last_backup or last_backup.date_created < (now - timedelta(hours=24)):
DBBackup.objects.create(db=db)
for backup in db.backups.filter(date_created__lt=now - timedelta(days=30)):
backup.set_expired()