#!/bin/bash #################################################################################################### #### author: SlickStack ############################################################################ #### link: https://slickstack.io ################################################################### #### mirror: https://mirrors.slickstack.io/bash/ss-import-database.txt ############################# #### path: /var/www/ss-import-database ############################################################# #### destination: n/a (not a boilerplate) ########################################################## #### purpose: Force imports /tmp/import.sql|.sql.zip|.sql.gz and overwrites database ############### #### module version: MySQL 8.0.x ################################################################### #### sourced by: n/a ############################################################################### #### bash aliases: ss import database|db|mysql ##################################################### #################################################################################################### ## source ss-config ## source /var/www/ss-config ## source ss-functions ## source /var/www/ss-functions ## BELOW THIS RELIES ON SS-CONFIG AND SS-FUNCTIONS #################################################################################################### #### TABLE OF CONTENTS (SS-Import-Database) ######################################################## #################################################################################################### ## this is a brief summary of the different code snippets you will find in this script ## ## each section should be commented so you understand what is being accomplished ## ## A. Touch Timestamp File ## B. Message (Begin Script) ## C. Backup Database Before Proceeding ## D. Import Database (Overwrites Data) #################################################################################################### #### A. SS-Import-Database: Touch Timestamp File ################################################### #################################################################################################### ## this is a dummy timestamp file that will remember the last time this script was run ## ## it can be useful for developer reference and is sometimes used by SlickStack ## ss_touch "${TIMESTAMP_SS_IMPORT_DATABASE}" #################################################################################################### #### B. SS-Import-Database: Message (Begin Script) ################################################# #################################################################################################### ## this is a simple message that announces to the shell the purpose of this bash script ## ## it will only be seen by sudo users who manually run this script in the shell ## ss_echo "${COLOR_INFO}Running ss-import-database... ${COLOR_RESET}" #################################################################################################### #### C. SS-Import-Database: Backup Database Before Proceeding ###################################### #################################################################################################### source "${PATH_SS_DUMP_DATABASE}" #################################################################################################### #### D. SS-Import-Database: Import Database (Overwrites Data) ###################################### #################################################################################################### ## here the sudo user must manually confirm importing database before it can proceed ## ## import.sql is the preferred file format followed by .sql.zip and .sql.gz ## read -r -p "DATABASE will be completely overwritten, are you sure? [y/N]" response if [[ "${response}" =~ ^([yY][eE][sS]|[yY])$ ]]; then if [[ -f "/tmp/import.sql" ]]; then echo "found import.sql and proceeding to import and overwrite database" chown -R "${SFTP_USER}":www-data /tmp/import.sql chmod 6775 /tmp/import.sql if [[ "${SS_DATABASE_REMOTE}" == "true" ]]; then ss_mysql_user "${DB_NAME}" < /tmp/import.sql else ss_mysql_root "${DB_NAME}" < /tmp/import.sql fi elif [[ ! -f "/tmp/import.sql" ]] && [[ -f "/tmp/import.sql.zip" ]]; then echo "found import.sql.zip and proceeding to import and overwrite database" chown -R "${SFTP_USER}":www-data /tmp/import.sql.zip chmod 6775 /tmp/import.sql.zip if [[ "${SS_DATABASE_REMOTE}" == "true" ]]; then ss_mysql_user "${DB_NAME}" < /tmp/import.sql.zip else ss_mysql_root "${DB_NAME}" < /tmp/import.sql.zip fi elif [[ ! -f "/tmp/import.sql" ]] && [[ ! -f "/tmp/import.sql.zip" ]] && [[ -f "/tmp/import.sql.gz" ]]; then echo "found import.sql.gz and proceeding to import and overwrite database" chown -R "${SFTP_USER}":www-data /tmp/import.sql.gz ## should we gunzip it first, run below commands, and then import.sql to database? ## chmod g-s /tmp/import.sql.gz ## avoids gunzip errors chmod 0777 /tmp/import.sql.gz ## avoids gunzip errors if [[ "${SS_DATABASE_REMOTE}" == "true" ]]; then ss_mysql_user "${DB_NAME}" < /tmp/import.sql.gz else ss_mysql_root "${DB_NAME}" < /tmp/import.sql.gz fi elif [[ ! -f "/tmp/import.sql" ]] && [[ ! -f "/tmp/import.sql.zip" ]] && [[ ! -f "/tmp/import.sql.gz" ]]; then echo "No import.sql|.sql.zip|.sql.gz file found, cannot import database" fi else echo "cancelled import" exit 1 fi #################################################################################################### #### SlickStack: Reset Permissions (SlickStack Scripts) ############################################ #################################################################################################### ## we include this permissions reset in all cron jobs and bash scripts for redundancy ## ## chmod 0700 means only the root/sudo users can execute any SlickStack scripts ## ## THIS SNIPPET DOES NOT RELY ON SS-CONFIG OR SS-FUNCTIONS ## SNIPPET: ss bash scripts, ss cron jobs ## UPDATED: 02JUL2022 chown root:root /var/www/ss* ## must be root:root chown root:root /var/www/crons/*cron* ## must be root:root chown root:root /var/www/crons/custom/*cron* ## must be root:root chmod 0700 /var/www/ss* ## 0700 means only root/sudo can execute chmod 0700 /var/www/crons/*cron* ## 0700 means only root/sudo can execute chmod 0700 /var/www/crons/custom/*cron* ## 0700 means only root/sudo can execute #################################################################################################### #### SlickStack: External References Used To Improve This Script (Thanks, Interwebz) ############### #################################################################################################### ## Ref: https://github.com/littlebizzy/slickstack/blob/master/ss-dump.txt ## Ref: https://collegetimes.co/lemp-server/ ## Ref: https://unix.stackexchange.com/questions/630495/what-is-set-group-id-on-execution-ignored-and-why-am-i-unable-to-unzip-a-file ## Ref: https://www.reddit.com/r/Ubuntu/comments/s39uyj/gzip_error_mydbsql_is_setgroupid_on_execution/ ## SS_EOF