From bf531a7a908ad4a6af9b7edc29e1627c095a2850 Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Mon, 26 Oct 2020 20:17:27 -0400 Subject: [PATCH] Create intitial script --- maria_db.sh | 144 ++++++++++++++++++++++++++++++++++++++++++++++++++++ run.sh | 21 +++++++- 2 files changed, 164 insertions(+), 1 deletion(-) create mode 100755 maria_db.sh mode change 100644 => 100755 run.sh diff --git a/maria_db.sh b/maria_db.sh new file mode 100755 index 0000000..21ec9d9 --- /dev/null +++ b/maria_db.sh @@ -0,0 +1,144 @@ +#!/usr/bin/env bash +### + # Installs and sets up MariaDB. + ## + + +# Import utils script. +. $(dirname ${0})/utils.sh + + +### + # Script entry point. + ## +function main () { + + check_is_user root + + echo "Starting MariaDB install for Arch Linux." + echo -e "To continue, press ${text_cyan}enter${text_reset}. Otherwise, hit ${text_cyan}ctrl+c${text_reset} to cancel." + read + + echo -e "${text_blue}Starting MariaDB installation.${text_reset}" + + install_db + set_config + + echo -e "${text_green}MariaDB installation complete.${text_reset}" +} + + +### + # Core installation logic. + ## +function install_db () { + # Install MariaDB. + pacman -Syu mariadb --noconfirm + + # Run initial configuration. + sudo mariadb-install-db --user=mysql --basedir=/usr --datadir=/var/lib/mysql + + # Start MariaDB service and set to start on boot. + systemctl enable mariadb.service + systemctl start mariadb.service + + # Proceed with initial configuration. + mysql_secure_installation +} + + +### + # Updates config file for first time setup. + ## +function set_config () { + # Check that expected config file exists. + if [[ -f "/etc/mysql/my.cnf" ]] + then + config_file="/etc/mysql/my.cnf" + elif [[ -f "/etc/my.cnf" ]] + then + config_file="/etc/my.cnf" + else + echo -e "${text_orange}Failed to locate config file at \"/etc/mysql/my.cnf\" or \"/etc/my.cnf\".${text_reset}" + echo -e "${text_orange}Unable to set config for first time setup.${text_reset}" + return + fi + + # Check file contents. + file_contents=$(cat ${config_file}) + search_string_1="default-character-set =" + search_string_2="collation_server =" + search_string_3="character_set_server =" + default_char_set_found=false + if [[ ! "${file_contents}" =~ "${search_string_1}" || + ! "${file_contents}" =~ "${search_string_2}" || + ! "${file_contents}" =~ "${search_string_3}" ]] + then + # One or more values not set. + echo -e "${text_blue}Running config first time setup.${text_reset}" + + # Set config header. + echo "" >> ${config_file} + echo "" >> ${config_file} + echo "###" >> ${config_file} + echo " # Automatically set Config Values" >> ${config_file} + echo " ##" >> ${config_file} + + # Check for default char set. + if [[ "${file_contents}" =~ "${search_string_1}" ]] + then + default_char_set_found=true + fi + + # Set default char set if was not found. + if [[ ${default_char_set_found} == false ]] + then + # Default char set not found. Define here for client. + echo "[client]" >> ${config_file} + echo "default-character-set = utf8mb4" >> ${config_file} + echo "" >> ${config_file} + fi + + # Check for default collation and server set. + if [[ ! "${file_contents}" =~ "${search_string_2}" || ! "${file_contents}" =~ "${search_string_3}" ]] + then + # Default collation or server set not found. Define here. + echo "[mysqld]" >> ${config_file} + + if [[ ! "${file_contents}" =~ "${search_string_2}" ]] + then + echo "collation_server = utf8mb4_unicode_ci" >> ${config_file} + fi + if [[ ! "${file_contents}" =~ "${search_string_3}" ]] + then + echo "character_set_server = utf8mb4" >> ${config_file} + fi + echo "" >> ${config_file} + fi + + # Set default char set if was not found. + if [[ ${default_char_set_found} == false ]] + then + # Default char set not found. Define here for mysql. + echo "[mysql]" >> ${config_file} + echo "default-character-set = utf8mb4" >> ${config_file} + echo "" >> ${config_file} + + # While here, also set auto complete within the client. + echo "# Enable auto-completion in MySQL client." >> ${config_file} + echo "auto-rehash" >> ${config_file} + echo "" >> ${config_file} + fi + + # Restart MariaDB for new config values. + systemctl restart mariadb.service + + else + # Config values found. + echo -e "${text_orange}Config appears to already be setSkipping config first time setup.${text_reset}" + fi + +} + + +main diff --git a/run.sh b/run.sh old mode 100644 new mode 100755 index 388e8fd..132132b --- a/run.sh +++ b/run.sh @@ -1,4 +1,23 @@ #!/usr/bin/env bash ### - # + # Installs and sets up database for given system. ## + + +# Import utils script. +. $(dirname ${0})/utils.sh + + +### + # Script entry point. + ## +function main () { + echo "Starting Script." + + sudo ./maria_db.sh + + echo "Terminating Script." +} + + +main -- GitLab