1. Using GNU date, simple shell command
In addition to Piotr P. Karwasz's correct answer, I would show my simple shell syntax, using GNU date (not BSD date):
First show stat of bans table:
sqlite3 fail2ban.sqlite3 'SELECT count(timeofban) FROM bans'
1147784
sqlite3 fail2ban.sqlite3 "SELECT count(timeofban) FROM bans
WHERE timeofban < `date -d 'now -1 month' +%s`;"
1129083
Doing UNIXEPOCH translation at command line parameter expansion is quicker as sqlite don't have to translate each rows!
Compare:
time sqlite3 fail2ban.sqlite3 "SELECT count(timeofban) FROM bans
WHERE DATE(timeofban, 'unixepoch') < '$(date -d 'now -1 month' +'%F %T')';"
94074
real 0m0.427s
user 0m0.363s
sys 0m0.060s
With:
time sqlite3 fail2ban.sqlite3 "SELECT count(timeofban) FROM bans
WHERE timeofban < $(date -d 'now -1 month' +%s);"
93709
real 0m0.169s
user 0m0.121s
sys 0m0.045s
Where using DATE() conversion for each row make this operation near 3 time slower!! (Result differ because DATE() is day rounded, while GNU date return EPOCH in seconds.)
Of course, answer should be different on your system!
Then
sqlite3 fail2ban.sqlite3 "DELETE FROM bans WHERE
timeofban < $(date -d 'now -1 month' +%s); VACUUM;"
(Don't miss VACUUM;!)
Main advantage of using date command with a time lapse is that this could be placed in a periodically script.
Note: My choice is to run this with now -1 month as time lapse, every nights.
2. Without GNU date, using sqlite3 syntax:
Browsing sqlite manual, I've finally found a correct syntax:
sqlite3 fail2ban.sqlite3 "DELETE FROM bans
WHERE timeofban <= STRFTIME('%s', DATE('now', '-1 month'));VACUUM;"
Again: Using this syntax, sqlite3 will make translation on request, not on rows!!
Remark about crontabs
If you plan to write this an a crontab, dont miss to escape all percent signs!!
1 2 * * * root /usr/bin/sqlite3 /var/lib/fail2ban/fail2ban.sqlite3 "delete from bans where timeofban <= strftime('\%s', date('now', '-42 days'));vacuum;"
Will run this every night at 02:01. The command could also be added to a BASH script into /etc/cron.weekly for instance.