Skip to content

Instantly share code, notes, and snippets.

@setioaji
Last active September 24, 2024 09:15
Show Gist options
  • Save setioaji/5d46e14e067f35ba884a6239cc58d867 to your computer and use it in GitHub Desktop.
Save setioaji/5d46e14e067f35ba884a6239cc58d867 to your computer and use it in GitHub Desktop.
mysql import dari file gz
#!/bin/bash
#==============================================================================
# SETTINGS
#==============================================================================
# Directory containing the .sql.gz backup files
BACKUP_DIR=/ubuntu/each_db
# MySQL credentials
MYSQL_UNAME=root
MYSQL_PWORD=
# Databases to ignore (you can adjust the regex to match your needs)
IGNORE_DB="(^mysql|_schema$)"
# Include MySQL binaries in PATH if necessary
PATH=$PATH:/usr/local/mysql/bin
#==============================================================================
# METHODS
#==============================================================================
# Function to generate the MySQL login command
function mysql_login() {
local mysql_login="-u $MYSQL_UNAME"
if [ -n "$MYSQL_PWORD" ]; then
mysql_login+=" -p$MYSQL_PWORD"
fi
echo $mysql_login
}
# Function to extract the database name from a backup file (based on your naming convention)
function get_dbname_from_file() {
local file_name=$(basename "$1")
echo "${file_name#*.}" | sed 's/.sql.gz//'
}
# Function to check if a database should be ignored
function should_ignore_db() {
local db_name="$1"
if [[ $db_name =~ $IGNORE_DB ]]; then
return 0 # true (ignore)
else
return 1 # false (do not ignore)
fi
}
# Function to create the database if it doesn't exist, with backticks for special characters
function create_database_if_not_exists() {
local db_name="$1"
local check_db_exists_sql="SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$db_name'"
local result=$(mysql $(mysql_login) -sse "$check_db_exists_sql")
if [ -z "$result" ]; then
echo "Creating database: \`$db_name\`"
mysql $(mysql_login) -e "CREATE DATABASE \`$db_name\`"
else
echo "Database \`$db_name\` already exists."
fi
}
# Function to import a single database, with backticks for special characters
function import_database() {
local backup_file="$1"
local db_name=$(get_dbname_from_file "$backup_file")
if should_ignore_db "$db_name"; then
echo "Ignoring database: \`$db_name\` (matches IGNORE_DB pattern)"
return
fi
# Create the database if it does not exist
create_database_if_not_exists "$db_name"
# Import the data into the database
echo "Importing \`$db_name\` from $backup_file..."
gunzip < "$backup_file" | mysql $(mysql_login) $db_name
echo "Database \`$db_name\` imported successfully."
}
#==============================================================================
# RUN SCRIPT
#==============================================================================
# Check if backup directory exists
if [ ! -d "$BACKUP_DIR" ]; then
echo "Backup directory $BACKUP_DIR does not exist!"
exit 1
fi
# Find all .sql.gz files in the backup directory
backup_files=$(find "$BACKUP_DIR" -type f -name "*.sql.gz")
# Loop through the backup files and import each
for backup_file in $backup_files; do
import_database "$backup_file"
done
echo "All database imports processed."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment