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!
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.
Without GNU date
, using sqlite3
syntax:
Browsing sqlite
manual, I've finally found a correct syntax:
slite3 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', '-40 days'));vacuum;"
Will run this every nights a 02:01
.