Interview Prep/Shell Scripting/Scenarios: backups, log rotation and housekeeping

Back up every MySQL database separately, skipping system schemas.

IntermediateScenarioScenarios: backups, log rotation and housekeeping
Answer

Situation read → Separate dumps allow restoring one database without the others.

What I'd do →

set -Eeuo pipefail
mapfile -t dbs < <(mysql --defaults-extra-file=/etc/mysql/backup.cnf -NBe 'SHOW DATABASES' |
  grep -Ev '^(information_schema|performance_schema|mysql|sys)$')
for db in "${dbs[@]}"; do
  mysqldump --defaults-extra-file=/etc/mysql/backup.cnf --single-transaction --routines --triggers -- "$db" |
    gzip > "/var/backups/mysql/${db}-$(date -u +%Y%m%dT%H%M%SZ).sql.gz"
done
  1. Credentials in a 0600 option file, not on the command line.
  2. --single-transaction gives a consistent snapshot for InnoDB without locking tables.
  3. -NB gives raw output without headers or boxes.

Why → Per-database dumps make partial restores fast, and pipefail catches a failing mysqldump.

Interview tip → Mention that logical dumps are slow for very large databases; physical tools (Percona XtraBackup) or managed snapshots fit better there.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: backups, log rotation and housekeeping