Dumps each MySQL database to its own compressed SQL file.
2 views
Share
This script backs up every user database on a MySQL/MariaDB server, one compressed file per database. It reads credentials from a MySQL option file (default ~/.my.cnf) using --defaults-extra-file, so the password is never exposed on the command line or in ps output. After validation, it queries SHOW DATABASES and filters out the internal schemas (information_schema, mysql, etc.). For each remaining database it runs mysqldump with --single-transaction for a consistent, lock-free snapshot of InnoDB tables, plus --routines, --triggers, and --events so stored code is included, piping the output straight through gzip. Run it as ./mysql_backup.sh /backups/mysql. Example:
Dumping 'shop' -> /backups/mysql/shop-20260604-101530.sql.gz
Dumping 'blog' -> /backups/mysql/blog-20260604-101530.sql.gz
All databases dumped to /backups/mysql
Per-database files make selective restores trivial.