Repository navigation
Description
src/uwsgi.ini defines two nightly crons that clean up /backup_restore:
cron = 0 0 -1 -1 -1 sh -c 'find /backup_restore/ -maxdepth 1 -type f -mtime +30 -printf "%T@ %p\n" | sort -n | head -n -3 | awk "{ print $2 }" | xargs -r rm'
cron = 0 0 -1 -1 -1 sh -c 'find /backup_restore/ -type d -ctime +30 -exec rm -rf {} \;'Neither does what it's meant to do, and both can cause damage.
1. The file cleanup deletes uwsgi.ini instead of old backups
uWSGI expands its magic variables in every config value, including cron commands: %p is the absolute path of the config file and %T is the current time. So -printf "%T@ %p\n" becomes:
-printf "<timestamp>@ /usr/src/project/uwsgi.ini\n"
find prints that line once for each backup file older than 30 days. Once there are more than 3 such files, xargs rm deletes uwsgi.ini. No backup file is ever removed, so old backups pile up.
There's a second bug: $2 inside sh -c '…' is expanded by the shell to an empty string, so awk prints whole lines.
Nothing goes wrong right away, because the running uWSGI has already read its config. The failure comes at the next reload or container restart: uWSGI can't find its ini, the django container loops on the entrypoint, and the log shows
realpath() of /usr/src/project/uwsgi.ini failed: No such file or directory
- Default setup (
srcbuilt into the image): the file is deleted from the container's writable layer.docker compose restart, or a reload viatouch-reload, fails. Recreating the container restores the file. - With
./srcbind-mounted (local development, or derived projects): the file is deleted from the host checkout too.
2. The directory cleanup can delete the whole backup directory
find /backup_restore/ -type d -ctime +30 also matches /backup_restore/ itself. If no backup is added to or removed from it for 30 days, the directory's ctime gets older than 30 days, and the cron runs rm -rf /backup_restore/. It can't remove the mountpoint (Device or resource busy), but it deletes everything inside, recent backups included. Since cron 1 never removes anything, this happens exactly when backups have stopped for a month, so the last good backups are lost.
Seen in a derived project: at midnight UTC the first cron deleted uwsgi.ini and the second emptied the backup volume. The problem showed up hours later, when a reload left Django unable to start.
Proposed fix
Move the cleanup into a shell script, so uWSGI has nothing to expand, and never match the backup directory itself:
# src/uwsgi.ini
cron = 0 0 -1 -1 -1 sh /usr/src/project/scripts/cleanup_backups.sh#!/bin/sh
# src/scripts/cleanup_backups.sh
DIR=${1:-/backup_restore}
# backup files older than 30 days, except the most recent 3 (a backup is composed by 3 files)
find "$DIR" -maxdepth 1 -type f -mtime +30 -printf '%T@ %p\n' | sort -n | head -n -3 | cut -d' ' -f2- | xargs -r -d '\n' rm -f --
# backup directories older than 30 days, never DIR itself
find "$DIR" -mindepth 1 -maxdepth 1 -type d -ctime +30 -exec rm -rf {} +Note for existing projects:
Projects generated from geonode-project carry their own copy of uwsgi.ini and need the same change.
Activity
[Fixes GeoNode#649] uWSGI backup cleanup crons delete uwsgi.ini and c…