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.

238 lines
5.5 KiB

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