The backup --hour cron job runs delete_old_local_backups() even when no users have configured hourly backups, causing unintended deletion of daily/weekly/monthly backup files.
Steps to Reproduce
- Configure all users with daily (or weekly/monthly) backup schedule only
- Set "Maximum backup store" to 1 in admin panel
- Have backup --hour cron running every 3 hours:
30 */3 * * * /path/to/manage.py backup --hour >> /var/log/backup.log 2>&1
- Wait for backup --hour to run (e.g., at 6 AM)
---
Expected Behavior
When no users have schedule='hour' in BackupList, the backup --hour command should:
- Query BackupList.objects.filter(schedule='hour') → returns empty
- Skip processing (no backups to create)
- NOT run delete_old_local_backups() (no reason to cleanup)
- Also shouldnt clean the daily backups thats there . maybe use folder inside /home/backups like > daily , hourly.....to keep them separated
---
Actual Behavior
The backup --hour command:
- Always runs delete_old_local_backups() at the start (line 26 of backup.py)
- Always runs per-user cleanup (lines 30-34)
- Deletes existing backups (even though no hourly backups were created)
---
Root Cause
File: panel_setup/mypanel/users/management/commands/backup.py
Lines 21-54:
def handle(self, *args, **options):
schedule_value = None
max_backup = int(AppSettings...first() or 100)
# THIS RUNS FOR ALL SCHEDULES (--hour, --day, --week, --month)
delete_old_local_backups(max_backup, '/home/backup') # <-- BUG!
for user in User.objects.all():
max_backup = UserSettings...first() or 100
backup_dir = f'/home/{user.username}/backup'
delete_old_local_backups(max_backup, backup_dir) # <-- ALSO BUG!
# Then check which schedule was requested
if options['hour']:
schedule_value = 'hour'
# ...
backups = BackupList.objects.filter(schedule=schedule_value)
# If no users have 'hour' schedule, this is EMPTY!
Problem: Cleanup runs before checking which schedule was requested and before processing any backups.