DB Maintenance Script for Drupal MySQL Servers (Optional)
Optional weekly MySQL cleanup for Drupal servers
DB maintenance script
db-maintenance.sh is an optional server script that ships with the Turbo Indexer.
It has nothing to do with indexing: it is a weekly MySQL cleanup for servers that host Drupal sites,
run from cron on the database machine.
What one run does
- Finds every database on the server that has a Drupal
cache_bootstraptable. - In each one, when the
watchdogtable exists (Database Logging is on): deletes the rows older than 7 days, then optimizes the table. - Empties every InnoDB
cache_table of 2 MB or more. Drupal fills them again as pages are requested. - Deletes expired
floodrows andbatchrows older than one day, when those tables exist. - When binary logging is on and no replica is connected, purges the binary logs older than one day. With a replica connected, it leaves them alone.
- Prints the size of
/var/lib/mysqland the disk use of/.
Install
The script is in the module's folder; with the standard Composer layout that is web/modules/contrib/opensolr_turbo_indexer/.
sudo cp web/modules/contrib/opensolr_turbo_indexer/db-maintenance.sh /usr/local/bin/
sudo chmod +x /usr/local/bin/db-maintenance.sh
# See what it would do, change nothing
db-maintenance.sh -u USER -p PASSWORD --dry-run
Then add it to root's crontab (crontab -e). This line runs it every Sunday at 04:00:
0 4 * * 0 /usr/local/bin/db-maintenance.sh -u USER -p PASSWORD >> /var/log/db-maintenance.log 2>&1
Options
MySQL user. Default: root.
MySQL password. Default: root.
Days of watchdog rows to keep. Default: 7.
Prints each change as a DRY-RUN: line and makes none. Default: off.
Shows the usage line.
Good to know
- Without
-uand-pit logs in asrootwith the passwordroot. Pass your own. - It talks to the MySQL server on the same machine; there is no host option.
- It needs bash and the
mysqlcommand-line client. - Run it with
--dry-runonce on every new server, to see which databases it finds.
Bonus tools: optional server scripts
- DB maintenance script
- IP blocker script