Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

107 Zeilen
4.0KB

  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]"
  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 CSV."
  34. echo "v Import CSV file to table."
  35. echo
  36. }
  37. Export()
  38. {
  39. docker exec -it $CONTAINER mysqldump --single-transaction -u $USERNAME -p$PASSWORD $DATABASE > $EXPORT_SQL_FILENAME`date +"%Y%m%d"`.sql
  40. }
  41. Import()
  42. {
  43. docker exec -i $CONTAINER mysql -u $USERNAME -p$PASSWORD $DATABASE < $IMPORT_SQL_DIRECTORY/$IMPORT_SQL_FILENAME
  44. }
  45. CSV_table_export()
  46. {
  47. docker exec -it $CONTAINER bash -c "mysql -u $USERNAME -p$PASSWORD $DATABASE --batch -e 'SELECT * FROM $TABLE'" > $EXPORT_CSV_DIRECTORY/$CSV_FILENAME.txt
  48. }
  49. CSV_table_import()
  50. {
  51. docker cp $IMPORT_CSV_FILE $CONTAINER:/tmp/import_file
  52. 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;'"
  53. }
  54. ############################################################
  55. ############################################################
  56. # Main program #
  57. ############################################################
  58. ############################################################
  59. # Set variables
  60. CONTAINER=mariadb
  61. DATABASE=toolkit
  62. USERNAME=xxxxxxxx
  63. PASSWORD=xxxxxxxx
  64. EXPORT_SQL_FILENAME=toolkit_db_
  65. IMPORT_SQL_DIRECTORY="/Users/ad7588/Downloads"
  66. IMPORT_SQL_FILENAME=toolkit_db.sql
  67. EXPORT_CSV_DIRECTORY="./db_exports"
  68. CSV_FILENAME=$2`date +"%Y%m%d"`
  69. # Get the options
  70. while getopts ":hleicv" flag; do
  71. case $flag in
  72. l) # display License
  73. License
  74. exit;;
  75. h) # display Help
  76. Help
  77. exit;;
  78. e) # export whole database
  79. Export
  80. exit;;
  81. i) # import database from file
  82. Import
  83. exit;;
  84. c) # export single table as CSV
  85. TABLE=$2
  86. CSV_table_export
  87. exit;;
  88. v) # import single CSV to table
  89. TABLE=$2
  90. IMPORT_CSV_FILE=$3
  91. CSV_table_import
  92. exit;;
  93. \?) # Invalid option
  94. echo "Error: Invalid option"
  95. exit;;
  96. esac
  97. done