mirror of
https://github.com/eduardogsilva/routerfleet.git
synced 2025-08-30 14:50:16 +02:00
Automatic BackupSchedule management
This commit is contained in:
parent
31b1c663f2
commit
a12a126d38
13 changed files with 298 additions and 31 deletions
|
@ -1,3 +1,140 @@
|
|||
from django.shortcuts import render
|
||||
from django.http import JsonResponse
|
||||
from backup.models import BackupProfile
|
||||
from backup_data.models import RouterBackup
|
||||
from router_manager.models import Router, BackupSchedule
|
||||
|
||||
# Create your views here.
|
||||
from datetime import datetime, timedelta
|
||||
from django.utils import timezone
|
||||
|
||||
|
||||
def next_weekday(now, weekday, hour):
|
||||
days_ahead = weekday - now.weekday()
|
||||
if days_ahead < 0 or (days_ahead == 0 and now.hour >= hour): # if backup date is for today and hour has passed, move to next week
|
||||
days_ahead += 7
|
||||
next_backup = now + timedelta(days=days_ahead)
|
||||
return next_backup.replace(hour=hour, minute=0, second=0, microsecond=0)
|
||||
|
||||
|
||||
def find_next_active_day(start_date, active_days, backup_hour):
|
||||
for i in range(7): # Verifica os próximos 7 dias
|
||||
potential_date = start_date + timedelta(days=i)
|
||||
if active_days[potential_date.weekday()]:
|
||||
next_active_date = potential_date.replace(hour=backup_hour, minute=0, second=0, microsecond=0)
|
||||
if next_active_date > timezone.now():
|
||||
return next_active_date
|
||||
# Se já passou a hora no primeiro dia válido, procura no dia correspondente da próxima semana
|
||||
if i == 0:
|
||||
return potential_date + timedelta(days=7, hours=(backup_hour - potential_date.hour))
|
||||
return None
|
||||
|
||||
|
||||
def calculate_next_backup(backup_profile):
|
||||
now = timezone.now()
|
||||
|
||||
if backup_profile.daily_backup:
|
||||
weekdays_enabled = [
|
||||
backup_profile.daily_day_monday,
|
||||
backup_profile.daily_day_tuesday,
|
||||
backup_profile.daily_day_wednesday,
|
||||
backup_profile.daily_day_thursday,
|
||||
backup_profile.daily_day_friday,
|
||||
backup_profile.daily_day_saturday,
|
||||
backup_profile.daily_day_sunday,
|
||||
]
|
||||
next_daily_backup = find_next_active_day(now, weekdays_enabled, backup_profile.daily_hour)
|
||||
else:
|
||||
next_daily_backup = None
|
||||
|
||||
if backup_profile.weekly_backup:
|
||||
weekday = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'].index(backup_profile.weekly_day)
|
||||
next_weekly_backup = next_weekday(now, weekday, backup_profile.weekly_hour)
|
||||
else:
|
||||
next_weekly_backup = None
|
||||
|
||||
if backup_profile.monthly_backup:
|
||||
potential_monthly_backup = datetime(year=now.year, month=now.month, day=backup_profile.monthly_day,
|
||||
hour=backup_profile.monthly_hour, tzinfo=timezone.get_current_timezone())
|
||||
if potential_monthly_backup <= now:
|
||||
# Se o dia do mês já passou, ou é hoje mas a hora já passou, agenda para o próximo mês
|
||||
month_increment = 1 if now.month < 12 else -11
|
||||
year_increment = 0 if now.month < 12 else 1
|
||||
next_monthly_backup = potential_monthly_backup.replace(year=now.year + year_increment,
|
||||
month=now.month + month_increment)
|
||||
else:
|
||||
next_monthly_backup = potential_monthly_backup
|
||||
else:
|
||||
next_monthly_backup = None
|
||||
|
||||
return next_daily_backup, next_weekly_backup, next_monthly_backup
|
||||
|
||||
|
||||
def generate_backup_schedule(request):
|
||||
data = {
|
||||
'backup_schedule_created': 0,
|
||||
'daily_backup_schedule_created': 0,
|
||||
'weekly_backup_schedule_created': 0,
|
||||
'monthly_backup_schedule_created': 0,
|
||||
'daily_backup_schedule_removed': 0,
|
||||
'weekly_backup_schedule_removed': 0,
|
||||
'monthly_backup_schedule_removed': 0
|
||||
}
|
||||
|
||||
|
||||
for router in Router.objects.filter(backupschedule__isnull=True):
|
||||
new_backup_schedule, _ = BackupSchedule.objects.get_or_create(router=router)
|
||||
data['backup_schedule_created'] += 1
|
||||
|
||||
for backup_profile in BackupProfile.objects.all():
|
||||
backup_schedule_list = BackupSchedule.objects.filter(router__backup_profile=backup_profile)
|
||||
next_daily_backup, next_weekly_backup, next_monthly_backup = calculate_next_backup(backup_profile)
|
||||
|
||||
if backup_profile.daily_backup and not next_daily_backup:
|
||||
backup_profile.profile_error_information = 'Error calculating next daily backup. Check profile settings'
|
||||
backup_profile.save()
|
||||
if backup_profile.weekly_backup and not next_weekly_backup:
|
||||
backup_profile.profile_error_information = 'Error calculating next weekly backup. Check profile settings'
|
||||
backup_profile.save()
|
||||
if backup_profile.monthly_backup and not next_monthly_backup:
|
||||
backup_profile.profile_error_information = 'Error calculating next monthly backup. Check profile settings'
|
||||
backup_profile.save()
|
||||
|
||||
if backup_profile.daily_backup:
|
||||
daily_schedule_list = backup_schedule_list.filter(next_daily_backup__isnull=True)
|
||||
for schedule in daily_schedule_list:
|
||||
schedule.next_daily_backup = next_daily_backup
|
||||
schedule.save()
|
||||
data['daily_backup_schedule_created'] += 1
|
||||
else:
|
||||
daily_schedule_list = backup_schedule_list.filter(next_daily_backup__isnull=False)
|
||||
for schedule in daily_schedule_list:
|
||||
schedule.next_daily_backup = None
|
||||
schedule.save()
|
||||
data['daily_backup_schedule_removed'] += 1
|
||||
|
||||
if backup_profile.weekly_backup:
|
||||
weekly_schedule_list = backup_schedule_list.filter(next_weekly_backup__isnull=True)
|
||||
for schedule in weekly_schedule_list:
|
||||
schedule.next_weekly_backup = next_weekly_backup
|
||||
schedule.save()
|
||||
data['weekly_backup_schedule_created'] += 1
|
||||
else:
|
||||
weekly_schedule_list = backup_schedule_list.filter(next_weekly_backup__isnull=False)
|
||||
for schedule in weekly_schedule_list:
|
||||
schedule.next_weekly_backup = None
|
||||
schedule.save()
|
||||
data['weekly_backup_schedule_removed'] += 1
|
||||
|
||||
if backup_profile.monthly_backup:
|
||||
monthly_schedule_list = backup_schedule_list.filter(next_monthly_backup__isnull=True)
|
||||
for schedule in monthly_schedule_list:
|
||||
schedule.next_monthly_backup = next_monthly_backup
|
||||
schedule.save()
|
||||
data['monthly_backup_schedule_created'] += 1
|
||||
else:
|
||||
monthly_schedule_list = backup_schedule_list.filter(next_monthly_backup__isnull=False)
|
||||
for schedule in monthly_schedule_list:
|
||||
schedule.next_monthly_backup = None
|
||||
schedule.save()
|
||||
data['monthly_backup_schedule_removed'] += 1
|
||||
return JsonResponse(data)
|
Loading…
Add table
Add a link
Reference in a new issue