Purge old automysqlbackup files from a backup directory tree based on a configurable retention policy derived from the daily/weekly/monthly directory structure.
Python 3.8 or later. No external dependencies — stdlib only.
# Run directly
python3 automysqlbackup_purger.py /path/to/automysqlbackup
# Or install via pip (adds automysqlbackup-purger to PATH)
pip install .| Backup type | Default retention | Flag |
|---|---|---|
monthly |
730 days (2 years) | --retention-monthly |
weekly |
180 days (6 months) | --retention-weekly |
daily |
30 days (1 month) | --retention-daily |
| other | 365 days (1 year) | --retention-other |
Retention type is determined by path components, so a database named bi-weekly_reports will not accidentally match weekly.
Dry run (default — shows what would be deleted, makes no changes):
python3 automysqlbackup_purger.py /path/to/automysqlbackupLive run (actually deletes expired files):
python3 automysqlbackup_purger.py /path/to/automysqlbackup --no-dry-runArchive mode (moves expired files to a directory instead of deleting):
python3 automysqlbackup_purger.py /path/to/automysqlbackup \
--archive-dir /mnt/cold-storage/expired --no-dry-runOverride retention and prune empty directories:
python3 automysqlbackup_purger.py /path/to/automysqlbackup \
--retention-daily 14 --retention-weekly 90 \
--prune-empty-dirs --no-dry-runPoint the script at the automysqlbackup subtree, not the storage root. Files are already .sql.gz — re-compression gains nothing and breaks per-file restorability.
| Flag | Default | Description |
|---|---|---|
path |
(required) | automysqlbackup directory to scan |
--patterns |
*.sql.gz *.sql |
File patterns to match |
--exclude-patterns |
Additional filename patterns to skip | |
--exclude-dirs |
Directory names (or fnmatch patterns) to skip, matched against path components | |
--archive-dir |
Move expired files here instead of deleting | |
--no-dry-run |
Actually delete/move files (default is a safe dry run) | |
--prune-empty-dirs |
Remove empty directories after purging (reported in dry-run) | |
--follow-symlinks |
Follow symlinks when walking; symlinked files are skipped by default | |
--retention-daily |
30 |
Retention for daily backups (days) |
--retention-weekly |
180 |
Retention for weekly backups (days) |
--retention-monthly |
730 |
Retention for monthly backups (days) |
--retention-other |
365 |
Retention for unclassified backups (days) |
-v / --verbose |
Enable debug output | |
-q / --quiet |
Suppress all output except errors | |
--version |
Show version and exit |
Regardless of age, the following are never purged: stat, .DS_Store, Thumbs.db, *.log, *.log.gz, *.tmp, *.meta.
| Code | Meaning |
|---|---|
0 |
Success |
1 |
Fatal error (bad path, archive check failed) |
2 |
Completed with per-file errors |
python3 -m pytest
# or
python3 -m unittest discover tests