52 lines
1.3 KiB

  1. #!/bin/bash
  2. m() {
  3. mysql "${mysql_opts[@]}" -Bs "$@"
  4. }
  5. md() {
  6. mysqldump "${mysql_opts[@]}" "$@"
  7. }
  8. mysql_databases() {
  9. echo "SHOW DATABASES" | m
  10. }
  11. mysql_tables() {
  12. local db="$1"
  13. echo "SHOW TABLES" | m "$db"
  14. }
  15. mysql_opts=()
  16. if [ "$MYSQLHOST" ]; then
  17. mysql_opts+=(-h "$MYSQLHOST")
  18. fi
  19. DBS=($(mysql_databases)) || exit 1
  20. mkdir -p /var/backups/mysql
  21. for db in "${DBS[@]}"; do
  22. if [[ "$db" == "information_schema" || "$db" == "performance_schema" || "$db" == "mysql" ]]; then
  23. continue
  24. fi
  25. echo "Dumping database $db..." >&2
  26. # omitting all the rotation logic
  27. dst=/"var/backups/mysql/$db"
  28. [ -d "$dst.old" ] && rm -rf "$dst.old"
  29. [ -d "$dst" ] && mv "$dst" "$dst.old"
  30. mkdir -p "$dst.inprogress"
  31. (( start = SECONDS ))
  32. md "$db" --routines --no-data --add-drop-database --database "$db" | gzip --rsyncable > "$dst.inprogress/schema.sql.gz"
  33. tables=$(mysql_tables "$db")
  34. for table in $tables; do
  35. backup_file="$dst.inprogress/${table}.sql.gz"
  36. echo " Dumping $table into ${backup_file}"
  37. md "$db" "$table" | gzip --rsyncable > "$backup_file" || break
  38. done
  39. mv "$dst.inprogress" "$dst"
  40. [ -d "$dst.old" ] && rm -rf "$dst.old"
  41. (( elapsed = SECONDS - start ))
  42. echo " ..dumped $db to $dst ($(du -sh "$dst" | cut -f 1) in ${elapsed}s)" >&2
  43. done