provisioning tool for building opinionated architecture
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.

221 lines
5.1 KiB

8 months ago
8 months ago
  1. #!/bin/bash
  2. DEFAULT_BACKUP_FOLDER="$HOME/RECOVERY_MARIADB"
  3. confirm() {
  4. read -rp "$1 ([y]es or [N]o): "
  5. case $(echo "$REPLY" | tr '[:upper:]' '[:lower:]') in
  6. y | yes) echo "yes" ;;
  7. *) echo "no" ;;
  8. esac
  9. }
  10. synopsis() {
  11. echo "usage: "
  12. printf "\t list | console | connections\n"
  13. printf "\t ---------------------------\n"
  14. printf "\t use <DB_NAME>\n"
  15. printf "\t create <DB_NAME> [PASSWORD]\n"
  16. printf "\t ---------------------------\n"
  17. printf "\t backup <DB_NAME> [FOLDER]\n"
  18. printf "\t restore <DB_NAME> <FILE>\n"
  19. printf "\t ---------------------------\n"
  20. printf "\t rename <DB_NAME> <NEW_NAME>\n"
  21. }
  22. list() {
  23. lxc exec ct1 -- sh -c "echo \"SELECT schema_name FROM information_schema.schemata where schema_name not in ('information_schema','mariadb','mysql','performance_schema')\" | mariadb -u root --skip-column-names -r "
  24. }
  25. console() {
  26. if [[ -z $1 ]]; then
  27. lxc exec ct1 -- mariadb -u root
  28. else
  29. lxc exec ct1 -- sh -c "echo \"$1\" | mariadb -u root"
  30. fi
  31. }
  32. connections() {
  33. lxc exec ct1 -- sh -c "echo \"select id, user, host, db, command, time, state, info, progress from information_schema.processlist\" | mariadb -u root "
  34. }
  35. use() {
  36. lxc exec ct1 -- mariadb -u root "$DB_NAME"
  37. }
  38. create() {
  39. # shellcheck disable=SC1091
  40. source /opt/miaou-bash/lib/functions.sh
  41. # shellcheck disable=SC2034
  42. mapfile -t DBs < <(list)
  43. local NEW_DB="${1:-$DB_NAME}"
  44. local NEW_PASSWORD="${2:-$NEW_DB}"
  45. if ! containsElement DBs "$NEW_DB"; then
  46. lxc exec ct1 -- sh -c "echo \"\
  47. CREATE DATABASE \\\`$NEW_DB\\\`; \
  48. GRANT ALL ON \\\`$NEW_DB\\\`.* TO \\\`$NEW_DB\\\`@'%' IDENTIFIED BY '$NEW_PASSWORD'; \
  49. FLUSH PRIVILEGES; \
  50. \" | mariadb -u root"
  51. else
  52. echo "$NEW_DB already exists!"
  53. fi
  54. }
  55. backup() {
  56. if [[ ! -d "$FOLDER" ]]; then
  57. echo "error: Folder required!"
  58. file "$FOLDER"
  59. exit 2
  60. fi
  61. mkdir -p "$FOLDER"
  62. DATE=$(date '+%F')
  63. ARCHIVE="$FOLDER"/$DB_NAME-$DATE.mariadb.gz
  64. if [[ -f $ARCHIVE ]]; then
  65. VERSION_CONTROL=numbered mv -b "$ARCHIVE" "$FOLDER"/"$DB_NAME"-"$DATE"-daily.mariadb.gz
  66. fi
  67. echo "backup $DB_NAME into $FOLDER"
  68. mariadb-dump -h ct1.lxd -u "$DB_NAME" -p"$DB_NAME" "$DB_NAME" | gzip >"$ARCHIVE"
  69. echo "archive file created: $ARCHIVE"
  70. }
  71. restore() {
  72. echo "restore $DB_NAME $FILE"
  73. if [[ ! -f "$FILE" ]]; then
  74. echo "error: Backup file (*.mariadb.gz) required!"
  75. file "$FILE"
  76. exit 2
  77. fi
  78. PROCESSES=$(lxc exec ct1 -- sh -c "echo \"select id, user, host, db, command, time, state, info, progress from information_schema.processlist\" | mariadb -u root")
  79. set +e
  80. PROCESS_COUNT=$(echo "$PROCESSES" | grep -c "$DB_NAME")
  81. if [[ $PROCESS_COUNT -gt 0 ]]; then
  82. echo "FAILURE: There are some connections to database, please consider stopping bound services"
  83. echo
  84. echo "$PROCESSES"
  85. exit 2
  86. fi
  87. set -e
  88. if [[ "yes" == $(confirm "RESTORATION will drop DATABASE, please acknowledge with care!!!") ]]; then
  89. if list | grep -q "$DB_NAME"; then
  90. echo "backup <$DB_NAME> for safety reason"
  91. backup
  92. echo "drop database <$DB_NAME>"
  93. lxc exec ct1 -- sh -c "echo \"DROP DATABASE \\\`$DB_NAME\\\`\" | mariadb -u root"
  94. fi
  95. echo "create <$DB_NAME>"
  96. create
  97. # lxc exec ct1 -- sh -c "CREATE DATABASE \\\`$DB_NAME\\\`\" | mariadb -u root"
  98. gunzip -c "$FILE" | grep -av "^CREATE DATABASE" | grep -av "^USE" | mariadb -h ct1.lxd -u "$DB_NAME" -p"$DB_NAME" "$DB_NAME"
  99. echo RESTORATION completed successfully
  100. else
  101. exit 1
  102. fi
  103. }
  104. rename() {
  105. echo "rename $DB_NAME to $NEW_NAME"
  106. local DB_NAME_FOUND=false
  107. for database in $(list); do
  108. if [[ $database == "$DB_NAME" ]]; then
  109. DB_NAME_FOUND=true
  110. fi
  111. if [[ $database == "$NEW_NAME" ]]; then
  112. echoerr "$NEW_NAME already exists! please provide another name instead of <$NEW_NAME> or run list command"
  113. exit 20
  114. fi
  115. done
  116. if [[ ! $DB_NAME_FOUND ]]; then
  117. echoerr "source <$DB_NAME> does not exist!"
  118. exit 20
  119. fi
  120. if [[ "$DB_NAME" == "$NEW_NAME" ]]; then
  121. echowarn "no need to rename; no change required <$DB_NAME>"
  122. exit 0
  123. fi
  124. echo "create new database <$NEW_NAME>"
  125. create "$NEW_NAME"
  126. for table in $(console "use '$DB_NAME'; show tables"); do
  127. if [[ $table != "Tables_in_$DB_NAME" ]]; then
  128. echo "renaming table \`$DB_NAME\`.$table to \`$NEW_NAME\`.$table"
  129. console "use '$DB_NAME'; rename table \\\`$DB_NAME\\\`.$table to \\\`$NEW_NAME\\\`.$table;"
  130. fi
  131. done
  132. echo "every table has been renamed, so remove old database <$DB_NAME>"
  133. console "drop user \\\`$DB_NAME\\\`"
  134. console "drop database \\\`$DB_NAME\\\`"
  135. }
  136. # MAIN
  137. set -Eeuo pipefail
  138. # shellcheck source=/dev/null
  139. . "$MIAOU_BASEDIR/lib/functions.sh"
  140. [[ $# -lt 1 ]] && synopsis && exit 1
  141. ACTION=$1
  142. case $ACTION in
  143. console)
  144. shift
  145. TAIL=$*
  146. console "$TAIL"
  147. ;;
  148. list)
  149. list
  150. ;;
  151. connections)
  152. connections
  153. ;;
  154. use)
  155. [[ $# -lt 2 ]] && synopsis && exit 1
  156. DB_NAME=$2
  157. use
  158. ;;
  159. create)
  160. [[ $# -lt 2 ]] && synopsis && exit 1
  161. DB_NAME=$2
  162. DB_PASSWORD=${3:-$DB_NAME}
  163. create
  164. ;;
  165. backup)
  166. [[ $# -lt 2 ]] && synopsis && exit 1
  167. DB_NAME=$2
  168. FOLDER=${3:-$DEFAULT_BACKUP_FOLDER}
  169. backup
  170. ;;
  171. restore)
  172. [[ $# -lt 3 ]] && synopsis && exit 1
  173. DB_NAME=$2
  174. FILE=$3
  175. FOLDER=${4:-$DEFAULT_BACKUP_FOLDER}
  176. DB_PASSWORD="$DB_NAME"
  177. restore
  178. ;;
  179. rename)
  180. [[ $# -lt 3 ]] && synopsis && exit 1
  181. DB_NAME=$2
  182. NEW_NAME=$3
  183. rename
  184. ;;
  185. *)
  186. synopsis
  187. exit 1
  188. ;;
  189. esac