Init
This commit is contained in:
53
app/db/tasks.py
Normal file
53
app/db/tasks.py
Normal 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()
|
||||
Reference in New Issue
Block a user