chore: Update backup-data.py to execute rotation even if the backup as already been done but the number of files is over the limit

This commit is contained in:
Théophile Diot 2024-08-22 16:57:22 +01:00
parent 0e854ca812
commit 6769736821
No known key found for this signature in database
GPG key ID: FA995104A0BA376A

View file

@ -43,18 +43,6 @@ try:
"monthly": timedelta(weeks=4).total_seconds(),
}
if last_backup_date and last_backup_date.timestamp() + PERIOD_STAMPS[backup_period] > current_time.timestamp():
LOGGER.info(f"Backup already done within the last {backup_period} period, skipping backup ...")
sys_exit(0)
db_metadata = JOB.db.get_metadata()
if isinstance(db_metadata, str) or db_metadata["scheduler_first_start"]:
LOGGER.info("First start of the scheduler, skipping backup ...")
sys_exit(0)
backup_database(current_time, JOB.db, backup_dir)
backup_rotation = int(getenv("BACKUP_ROTATION", "7"))
# Get all backup files in the directory
@ -63,6 +51,21 @@ try:
# Sort the backup files by name
sorted_files = sorted(backup_files)
already_done = last_backup_date and last_backup_date.timestamp() + PERIOD_STAMPS[backup_period] > current_time.timestamp()
if len(sorted_files) <= backup_rotation and already_done:
LOGGER.info(f"Backup already done within the last {backup_period} period, skipping backup ...")
sys_exit(0)
if not already_done:
db_metadata = JOB.db.get_metadata()
if isinstance(db_metadata, str) or db_metadata["scheduler_first_start"]:
LOGGER.info("First start of the scheduler, skipping backup ...")
sys_exit(0)
backup_database(current_time, JOB.db, backup_dir)
# Check if the number of backup files exceeds the rotation limit
if len(sorted_files) > backup_rotation:
# Calculate the number of files to remove