fork 0k-charms
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
1.7 KiB

  1. #!/bin/bash
  2. . /etc/shlib
  3. include common
  4. include pretty
  5. usage="$exname [--host HOST] [DATABASE...]"
  6. DBS=()
  7. host=
  8. while [ "$1" ]; do
  9. case "$1" in
  10. "--help"|"-h")
  11. print_usage
  12. exit 0
  13. ;;
  14. "--host")
  15. host="$2"
  16. shift
  17. ;;
  18. *)
  19. DBS+=("$1")
  20. ;;
  21. esac
  22. shift
  23. done
  24. mysql_opts=()
  25. if [ "$host" ]; then
  26. mysql_opts+=(-h "$host")
  27. fi
  28. m() {
  29. mysql "${mysql_opts[@]}" -Bs "$@"
  30. }
  31. md() {
  32. mysqldump "${mysql_opts[@]}" "$@"
  33. }
  34. mysql_databases() {
  35. echo "SHOW DATABASES" | m
  36. }
  37. mysql_tables() {
  38. local db="$1"
  39. echo "SHOW TABLES" | m "$db"
  40. }
  41. if [ "${#DBS[@]}" == 0 ]; then
  42. DBS=($(mysql_databases)) || exit 1
  43. fi
  44. mkdir -p /var/backups/mysql
  45. for db in "${DBS[@]}"; do
  46. if [[ "$db" == "information_schema" || "$db" == "performance_schema" || "$db" == "mysql" ]]; then
  47. continue
  48. fi
  49. echo "Dumping database $db..." >&2
  50. # omitting all the rotation logic
  51. dst=/"var/backups/mysql/$db"
  52. [ -d "$dst.old" ] && rm -rf "$dst.old"
  53. [ -d "$dst" ] && mv "$dst" "$dst.old"
  54. mkdir -p "$dst.inprogress"
  55. (( start = SECONDS ))
  56. md "$db" --routines --no-data --add-drop-database --database "$db" | gzip --rsyncable > "$dst.inprogress/00-schema.sql.gz"
  57. tables=$(mysql_tables "$db")
  58. for table in $tables; do
  59. backup_file="$dst.inprogress/${table}.sql.gz"
  60. echo " Dumping $table into ${backup_file}"
  61. md "$db" "$table" | gzip --rsyncable > "$backup_file" || break
  62. done
  63. mv "$dst.inprogress" "$dst"
  64. [ -d "$dst.old" ] && rm -rf "$dst.old"
  65. (( elapsed = SECONDS - start ))
  66. echo " ..dumped $db to $dst ($(du -sh "$dst" | cut -f 1) in ${elapsed}s)" >&2
  67. done