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.

144 lines
4.9KB

  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 <ad7588@coventry.ac.uk>
  6. # @purpose: Runs database functions for the ExPub Compendium
  7. # @acknowledgements:
  8. # https://www.redhat.com/sysadmin/arguments-options-bash-scripts
  9. ############################################################
  10. # subprograms #
  11. ############################################################
  12. License()
  13. {
  14. echo 'Copyright 2022 Simon Bowie <ad7588@coventry.ac.uk>'
  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 ExPub 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
  37. }
  38. Export()
  39. {
  40. docker exec -it $CONTAINER mysqldump --single-transaction -u $USERNAME -p$PASSWORD $DATABASE > $EXPORT_SQL_FILENAME`date +"%Y%m%d"`.sql
  41. }
  42. Import()
  43. {
  44. docker exec -i $CONTAINER mysql -u $USERNAME -p$PASSWORD $DATABASE < $IMPORT_SQL_DIRECTORY/$IMPORT_SQL_FILENAME
  45. }
  46. Table_export()
  47. {
  48. docker exec -it $CONTAINER bash -c "mysql -u $USERNAME -p$PASSWORD $DATABASE --batch -e 'SELECT * FROM $TABLE'" > $EXPORT_TXT_DIRECTORY/$EXPORT_TXT_FILENAME.txt
  49. }
  50. Table_import()
  51. {
  52. docker cp $IMPORT_TXT_FILE $CONTAINER:/tmp/import_file
  53. docker exec -i $CONTAINER bash -c "mysql -u $USERNAME -p$PASSWORD $DATABASE -e 'LOAD DATA LOCAL INFILE '\''/tmp/import_file'\'' REPLACE INTO TABLE $TABLE FIELDS TERMINATED BY '\''\t'\'' LINES TERMINATED BY '\''\n'\'' IGNORE 1 ROWS;'"
  54. }
  55. Drop_table()
  56. {
  57. docker exec -i $CONTAINER bash -c "mysql -u $USERNAME -p$PASSWORD $DATABASE -e 'DROP TABLE IF EXISTS $TABLE;'"
  58. }
  59. ############################################################
  60. ############################################################
  61. # main program #
  62. ############################################################
  63. ############################################################
  64. # set variables
  65. CONTAINER=mariadb
  66. DATABASE=toolkit
  67. USERNAME=xxxxxxxxx
  68. PASSWORD=xxxxxxxxx
  69. EXPORT_SQL_FILENAME=toolkit_db_
  70. IMPORT_SQL_DIRECTORY="/Users/ad7588/Downloads"
  71. IMPORT_SQL_FILENAME=toolkit_db.sql
  72. EXPORT_TXT_DIRECTORY="./db_exports"
  73. EXPORT_TXT_FILENAME=$2`date +"%Y%m%d"`
  74. # error message for no flags
  75. if (( $# == 0 )); then
  76. Help
  77. exit 1
  78. fi
  79. # get the options
  80. while getopts ":hleicvd" flag; do
  81. case $flag in
  82. l) # display License
  83. License
  84. exit;;
  85. h) # display Help
  86. Help
  87. exit;;
  88. e) # export whole database
  89. Export
  90. exit;;
  91. i) # import database from file
  92. Import
  93. exit;;
  94. c) # export single table as tab-delimited txt
  95. if [ -z "$2" ]
  96. then
  97. echo "-c requires a table name as an argument"
  98. echo
  99. echo "Syntax: database_functions.sh -c [table name]"
  100. else
  101. TABLE=$2
  102. Table_export
  103. exit 1
  104. fi;;
  105. v) # import single tab-delimited txt to table
  106. if [ -z "$2" ] || [ -z "$3" ]
  107. then
  108. echo "-v requires a table name as an argument and the file to be imported"
  109. echo
  110. echo "Syntax: database_functions.sh -v [table name] [file]"
  111. else
  112. TABLE=$2
  113. IMPORT_TXT_FILE=$3
  114. Table_import
  115. exit 1
  116. fi;;
  117. d) # drop table
  118. if [ -z "$2" ]
  119. then
  120. echo "-d requires a table name as an argument"
  121. echo
  122. echo "Syntax: database_functions.sh -d [table name]"
  123. else
  124. TABLE=$2
  125. Drop_table
  126. exit 1
  127. fi;;
  128. \?) # Invalid option
  129. Help
  130. exit;;
  131. esac
  132. done