Skip to content

Instantly share code, notes, and snippets.

@yolabingo
Last active July 3, 2020 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yolabingo/ef4a41853b46503bb2e6537d1129fb46 to your computer and use it in GitHub Desktop.
Save yolabingo/ef4a41853b46503bb2e6537d1129fb46 to your computer and use it in GitHub Desktop.
purge old fail2ban bans from sqlite db
#!/bin/bash
# prune sqlite db file for fail2ban v 0.9.x
# run me from cron to keep sqlite db a sane size
# fail2ban doesn't purge old entries from sqlite db, so it grows until things break
# https://gist.github.com/yolabingo/ef4a41853b46503bb2e6537d1129fb46
dbfile=$(fail2ban-client get dbfile | grep "\`-" | awk '{print $2}')
dbpurgeage=$(fail2ban-client get dbpurgeage | grep "\`-" | awk '{print $2}' | sed 's/seconds.*//')
python << EOF
import sys, logging
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
from fail2ban.server.database import Fail2BanDb
db = Fail2BanDb(${dbfile}, ${dbpurgeage})
db.purge()"
EOF
sqlite3 $dbfile 'VACUUM;'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment