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:

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

To repair all databases if errors:

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

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

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

To compress the files into gzip format, using:

1
mysqldump --opt -hlocalhost -uuser -ppassword database | gzip > database.sql.gz
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).

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
245 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

Leave a Reply