Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

165 rindas
5.6KB

  1. #!/bin/bash
  2. # @name: database_functions.sh
  3. # @creation_date: 2022-11-02
  4. # @license: The MIT License <https://opensource.org/licenses/MIT>
  5. # @author: Simon Bowie <simonxix@simonxix.com>
  6. # @purpose: Runs database functions for the Experimental Publishing Compendium
  7. # @acknowledgements:
  8. # https://www.redhat.com/sysadmin/arguments-options-bash-scripts
  9. ############################################################
  10. # subprograms #
  11. ############################################################
  12. License()
  13. {
  14. echo 'Copyright 2022-2025 Simon Bowie <simonxix@simonxix.com>'
  15. echo
  16. echo 'Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:'
  17. echo
  18. echo 'The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.'
  19. echo
  20. echo 'THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.'
  21. }
  22. Help()
  23. {
  24. # Display Help
  25. echo "This script performs database functions for the Experimental Publishing Compendium"
  26. echo
  27. echo "Syntax: database_functions.sh [-l|h|e|i|c|v|d]"
  28. echo "options:"
  29. echo "l Print the MIT License notification."
  30. echo "h Print this Help."
  31. echo "e Export whole database."
  32. echo "i Import whole database."
  33. echo "c Export single table as tab-delimited txt."
  34. echo "v Import tab-delimited txt file to table."
  35. echo "d Drop table."
  36. echo "b Backup database to remote storage."
  37. echo
  38. }
  39. Export()
  40. {
  41. docker exec -it $CONTAINER mariadb-dump --single-transaction -u $USERNAME -p$PASSWORD $DATABASE > $EXPORT_DIRECTORY/$EXPORT_SQL_FILENAME`date +"%Y%m%d"`.sql
  42. }
  43. Import()
  44. {
  45. docker exec -i $CONTAINER mariadb -u $USERNAME -p$PASSWORD $DATABASE < $IMPORT_SQL_FILE
  46. }
  47. Table_export()
  48. {
  49. docker exec -it $CONTAINER bash -c "mariadb -u $USERNAME -p$PASSWORD $DATABASE --batch -e 'SELECT * FROM $TABLE'" > $EXPORT_TXT_DIRECTORY/$EXPORT_TXT_FILENAME.txt
  50. }
  51. Table_import()
  52. {
  53. docker cp $IMPORT_TXT_FILE $CONTAINER:/tmp/import_file
  54. docker exec -i $CONTAINER bash -c "mariadb -u $USERNAME -p$PASSWORD $DATABASE -e 'LOAD DATA LOCAL INFILE '\''/tmp/import_file'\'' REPLACE INTO TABLE $TABLE FIELDS TERMINATED BY '\''\t'\'' LINES TERMINATED BY '\''\r'\'' IGNORE 1 ROWS;'"
  55. }
  56. Drop_table()
  57. {
  58. docker exec -i $CONTAINER bash -c "mariadb -u $USERNAME -p$PASSWORD $DATABASE -e 'DROP TABLE IF EXISTS $TABLE;'"
  59. }
  60. Backup()
  61. {
  62. docker exec -it $CONTAINER mariadb-dump --single-transaction -u $USERNAME -p$PASSWORD $DATABASE > $EXPORT_DIRECTORY/$BACKUP_SQL_FILENAME.sql
  63. scp $EXPORT_DIRECTORY/$BACKUP_SQL_FILENAME.sql $STORAGE_USERNAME@$STORAGE_SERVER:$STORAGE_DIRECTORY
  64. }
  65. ############################################################
  66. ############################################################
  67. # main program #
  68. ############################################################
  69. ############################################################
  70. # set variables
  71. CONTAINER=mariadb
  72. DATABASE=compendium
  73. USERNAME=xxxxxxxx
  74. PASSWORD=xxxxxxxx
  75. EXPORT_DIRECTORY="./db_exports"
  76. EXPORT_SQL_FILENAME=compendium_db_
  77. EXPORT_TXT_FILENAME=$2`date +"%Y%m%d"`
  78. BACKUP_SQL_FILENAME=compendium_backup
  79. STORAGE_USERNAME=xxxxxxxx
  80. STORAGE_SERVER=xxxxxxxx
  81. STORAGE_DIRECTORY=xxxxxxxx
  82. # error message for no flags
  83. if (( $# == 0 )); then
  84. Help
  85. exit 1
  86. fi
  87. # get the options
  88. while getopts ":hleicvdb" flag; do
  89. case $flag in
  90. l) # display License
  91. License
  92. exit;;
  93. h) # display Help
  94. Help
  95. exit;;
  96. e) # export whole database
  97. Export
  98. exit;;
  99. i) # import database from file
  100. if [ -z "$2" ]
  101. then
  102. echo "-i requires a file as an argument"
  103. echo
  104. echo "Syntax: database_functions.sh -i [file]"
  105. else
  106. IMPORT_SQL_FILE=$2
  107. Import
  108. exit 1
  109. fi;;
  110. c) # export single table as tab-delimited txt
  111. if [ -z "$2" ]
  112. then
  113. echo "-c requires a table name as an argument"
  114. echo
  115. echo "Syntax: database_functions.sh -c [table name]"
  116. else
  117. TABLE=$2
  118. Table_export
  119. exit 1
  120. fi;;
  121. v) # import single tab-delimited txt to table
  122. if [ -z "$2" ] || [ -z "$3" ]
  123. then
  124. echo "-v requires a table name as an argument and the file to be imported"
  125. echo
  126. echo "Syntax: database_functions.sh -v [table name] [file]"
  127. else
  128. TABLE=$2
  129. IMPORT_TXT_FILE=$3
  130. Table_import
  131. exit 1
  132. fi;;
  133. d) # drop table
  134. if [ -z "$2" ]
  135. then
  136. echo "-d requires a table name as an argument"
  137. echo
  138. echo "Syntax: database_functions.sh -d [table name]"
  139. else
  140. TABLE=$2
  141. Drop_table
  142. exit 1
  143. fi;;
  144. b) # backup database to secure storage
  145. Backup
  146. exit;;
  147. \?) # Invalid option
  148. Help
  149. exit;;
  150. esac
  151. done