Bash Script to Check, Repair, Optimise and Backup MySQL database


I have a VPS (Virtual Private Server) and I have just move another domain (steakovercooked.com) onto this. I find it useful to have a script that can automatically check, repair, optimise and backup the MySQL databases. It is also nice to set this running at crontab so you don’t need to manually do it every time.

To optimise all databases:

mysqlcheck -hlocalhost -uuser -ppassword --optimize --all-databases

To repair all databases if errors:

mysqlcheck -hlocalhost -uuser -ppassword --auto-repair --check --all-databases

To backup a database and save it into *.SQL files (plain text)

mysqldump --opt -hlocalhost -uuser -ppassword database > database.sql

To compress the files into gzip format, using:

mysqldump --opt -hlocalhost -uuser -ppassword database | gzip > database.sql.gz

Then, you can put the above commands into a script file (make sure you chmod +x it) and you can use command crontab -e to edit the cron jobs (e.g. let it run once per hour).

DevOps / Site Reliability Engineering

–EOF (The Ultimate Computing & Technology Blog) —

238 words
Last Post: Things You Should Never Do To Avoid Adsense Account Closed
Next Post: How To Add a Virtual Host on Apache2 Under Ubuntu Server?

The Permanent URL is: Bash Script to Check, Repair, Optimise and Backup MySQL database (AMP Version)

Leave a Reply