Essential MySQL Administration Bash Scripts for Linux: Backup, Optimize, Monitor & More
This article provides a collection of ready-to-use Bash scripts for common MySQL administration tasks on Linux, including database backup, table optimization, cleanup of old backups, process monitoring, status checks, size reporting, schema export/import, and lock/unlock operations.
In Linux environments, routine MySQL administration can be streamlined with Bash scripts. The following examples cover ten essential tasks, each presented as a self‑contained script that you can adapt by replacing placeholder values such as your_mysql_user, your_mysql_password, your_database_name, and the backup directory path.
Backup database:
#!/bin/bash
BACKUP_DIR="/path/to/backup/directory"
MYSQL_USER="your_mysql_user"
MYSQL_PASSWORD="your_mysql_password"
DATABASE="your_database_name"
DATE=$(date +%Y%m%d%H%M%S)
mysqldump -u $MYSQL_USER -p$MYSQL_PASSWORD $DATABASE > $BACKUP_DIR/$DATABASE-$DATE.sqlOptimize and repair tables:
#!/bin/bash
MYSQL_USER="your_mysql_user"
MYSQL_PASSWORD="your_mysql_password"
DATABASE="your_database_name"
mysqlcheck -u $MYSQL_USER -p$MYSQL_PASSWORD --auto-repair --optimize $DATABASEDelete expired backup files (keep last 7 days):
#!/bin/bash
BACKUP_DIR="/path/to/backup/directory"
DAYS_TO_KEEP=7
find $BACKUP_DIR -type f -mtime +$DAYS_TO_KEEP -exec rm {} \;Monitor MySQL process list:
#!/bin/bash
MYSQL_USER="your_mysql_user"
MYSQL_PASSWORD="your_mysql_password"
mysqladmin -u $MYSQL_USER -p$MYSQL_PASSWORD processlistShow extended MySQL status information:
#!/bin/bash
MYSQL_USER="your_mysql_user"
MYSQL_PASSWORD="your_mysql_password"
mysqladmin -u $MYSQL_USER -p$MYSQL_PASSWORD extended-statusDisplay size of each database (in MB):
#!/bin/bash
MYSQL_USER="your_mysql_user"
MYSQL_PASSWORD="your_mysql_password"
DATABASE="your_database_name"
mysql -u $MYSQL_USER -p$MYSQL_PASSWORD -e "SELECT table_schema AS 'Database Name', SUM(data_length + index_length) / 1024 / 1024 AS 'Size (MB)' FROM information_schema.TABLES GROUP BY table_schema;"Export only the schema (no data) of a database:
#!/bin/bash
MYSQL_USER="your_mysql_user"
MYSQL_PASSWORD="your_mysql_password"
DATABASE="your_database_name"
mysqldump -u $MYSQL_USER -p$MYSQL_PASSWORD --no-data $DATABASE > schema.sqlImport a previously exported schema file:
#!/bin/bash
MYSQL_USER="your_mysql_user"
MYSQL_PASSWORD="your_mysql_password"
DATABASE="your_database_name"
mysql -u $MYSQL_USER -p$MYSQL_PASSWORD $DATABASE < schema.sqlLock all tables for a consistent backup:
#!/bin/bash
MYSQL_USER="your_mysql_user"
MYSQL_PASSWORD="your_mysql_password"
mysql -u $MYSQL_USER -p$MYSQL_PASSWORD -e "FLUSH TABLES WITH READ LOCK;"Unlock tables after the backup is complete:
#!/bin/bash
MYSQL_USER="your_mysql_user"
MYSQL_PASSWORD="your_mysql_password"
mysql -u $MYSQL_USER -p$MYSQL_PASSWORD -e "UNLOCK TABLES;"Before using these scripts, replace all placeholder values (e.g., your_mysql_user , your_mysql_password , your_database_name , /path/to/backup/directory ) with the actual credentials and paths for your environment.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
