Skip to main content

Command Palette

Search for a command to run...

Bash Script for Auto DB Import

From External Database Server

Updated
2 min read
Bash Script for Auto DB Import

တစ်ခြား Database server က ကနေ database တစ်ခုကို dump လုပ်ပြီး ဒီဘက် Server ထဲ fresh import လုပ်ရတဲ့ Use case လေးတစ်ခုရှိလို့ ရေးလိုက်တဲ့ bash script လေးပါ။

Use Case ရှိ ရင် ယူသုံးလို့ရပါတယ်။ စမ်းကြည့်ချင်တယ် လေ့လာကြည့်ချင်တယ်ဆိုရင်လည်း Github link မှာ သွားကြည့်ပြီး download/clone လို့ရပါတယ်။

#!/bin/bash

# Author: Aung Kyaw Minn https://github.com/aungkyawminn
# Created on: 2024-04-02
# Description:
# This script connects to external database server by ssh tunnel using username/password, dump database and clean import to  local database.
# You will need to enter password for ssh user if ssh server is configured using password authentication.
# If ssh server is using public key authentication, you need to add your server public key in external server's authorized_keys file.
# Notice: You will need root access to execute this script and need to pre-configure networking and security for connecting between external database server and local database server

# SSH details for the external server
SSH_USER=""
SSH_HOST=""
SSH_PORT="22" # SSH port, 22 is the default

# External database details
EXTERNAL_DB_NAME=""
EXTERNAL_DB_USER=""
EXTERNAL_DB_PASSWORD=""

# Local database details
LOCAL_DB_NAME=""
LOCAL_DB_USER=""
LOCAL_DB_PASSWORD=""

# Local port for the SSH tunnel, different from the MySQL port to avoid conflicts
LOCAL_PORT="3307"

# Create an SSH tunnel
echo "Establishing an SSH tunnel on port $SSH_PORT..."
ssh -f -N -L ${LOCAL_PORT}:localhost:3306 -p $SSH_PORT ${SSH_USER}@${SSH_HOST}

# Check if the SSH tunnel was established successfully
if [ $? -eq 0 ]; then
  echo "SSH tunnel established successfully."
else
  echo "Failed to establish an SSH tunnel."
  exit 1
fi

# Temporary file to store the database dump
DUMP_FILE="/tmp/ems_old_uat.sql"

# Export the external database through the SSH tunnel
echo "Exporting external database..."
mysqldump -h 127.0.0.1 -P $LOCAL_PORT -u $EXTERNAL_DB_USER -p$EXTERNAL_DB_PASSWORD $EXTERNAL_DB_NAME > $DUMP_FILE

# Check if the dump was successful
if [ $? -eq 0 ]; then
  echo "Database exported successfully."
else
  echo "Failed to export database."
  exit 1
fi

# Drop the local database and recreate it to ensure it's clean for import
echo "Preparing local database..."
mysql -u $LOCAL_DB_USER -p$LOCAL_DB_PASSWORD -e "DROP DATABASE IF EXISTS $LOCAL_DB_NAME; CREATE DATABASE $LOCAL_DB_NAME;"

# Import the dump into the local database
echo "Importing dump into local database..."
mysql -u $LOCAL_DB_USER -p$LOCAL_DB_PASSWORD $LOCAL_DB_NAME < $DUMP_FILE

if [ $? -eq 0 ]; then
  echo "Database imported successfully."
else
  echo "Failed to import database."
  exit 1
fi

# Clean up the dump file
rm $DUMP_FILE

# Close the SSH tunnel after finished importing
# Find the process ID (PID) of the SSH tunnel and kill it.
SSH_TUNNEL_PID=$(ps aux | grep 'ssh -f -N -L' | grep -v grep | awk '{print $2}')
if [[ ! -z "$SSH_TUNNEL_PID" ]]; then
  kill $SSH_TUNNEL_PID
  echo "SSH tunnel closed."
fi

https://github.com/aungkyawminn/mysqldb-import-bash-script

Aung Kyaw Minn

tallysale.com

More from this blog

Infrastructure ကိုင်ပြီး အိပ်ရေးမပျက် ချင် လျှင် ဒါမျိုး Alarms လုပ် 🔥🔥🔥

High Level ရေးထားတာပါ ဒါပေမဲ့ လွယ်ပါတယ် ​ကိုယ့်မှာ AWS Infra တွေရှိတယ်ဆို တွေ့သမျှ metric တွေကို alarms တွေလုပ်ပြီး notification ယူမနေဘဲ တကယ် effective ဖြစ်တဲ့ metric တွေကိုမှ CloudWatch ရဲ့ alarm feature တွေနဲ့ ပေါင်းပြီး ပို့စေချင်ပါတယ်။ ​ဥပမာ prod...

Jan 17, 20263 min read159
Infrastructure ကိုင်ပြီး အိပ်ရေးမပျက် ချင် လျှင်  ဒါမျိုး Alarms လုပ် 🔥🔥🔥

How to connect On Premises Network and Cloud (AWS)? (Part-2)

ကိုယ့်ရဲ့ ‌data center (on-prem) network နဲ့ AWS ချိတ်ဆက်ဖို့ လိုလာပြီဆိုရင် ဘယ်လို ချိတ်ဆက်ကြမလဲ? အပိုင်း (၂) မှာ တော့ Direct connect အကြောင်းကို ဆွေးနွေး သွားမှာ ဖြစ်ပါတယ်။ အပိုင်း (၁) Site-to-site VPN အကြောင်းကို လေ့လာချင်ရင်တော့ အောက်ပါ link မှာ ...

Dec 20, 20253 min read224
How to connect On Premises Network and Cloud (AWS)? (Part-2)

How to connect On Premises Network and Cloud (AWS)? (Part-1)

ကိုယ့်ရဲ့ ‌data center (on-prem) network နဲ့ AWS ချိတ်ဆက်ဖို့ လိုလာပြီဆိုရင် ချိတ်ဆက်နိုင်တဲ့ နည်း (၂) နည်း ရှိပါတယ်။ 1. Site-to-Site VPN (Virtual Private Network) 2. Direct connect Site-to-Site VPN - On-prem network နဲ့ AWS resources တွေ ချိတ်ဆက်တဲ့...

Dec 12, 20252 min read262
How to connect On Premises Network and Cloud (AWS)? (Part-1)

Accessibility for Designer

လွန်ခဲ့တဲ့အပတ်က ရုံးက Designer တွေနဲ့ တော်ကီပွားရင်း Accessibility နဲ့ပတ်သတ်တာတွေ သူတို့ကို ရှင်းပြဖြစ်တယ်။ ကိုယ်တိုင်ကလည်း အရင်ကတည်းက ဒီ topic ကိုစိတ်ဝင်စားလို့ လေ့လာနေတာဆိုတော့ အခွင့်အရေးရရင် ရသလို sharing လုပ်ဖြစ်တယ်။ အဓိကက Accessibility နဲ့ပတ်သတ်...

Nov 21, 20253 min read69
Accessibility for Designer

VPC Endpoint

အားလုံးဘဲ မင်္ဂလာပါ။ ဒီနေ့ sharing လုပ်ပေးချင်တာကတော့ VPC Endpoint အကြောင်းဘဲဖြစ်ပါတယ်။ VPC Endpoint ဆိုတာ VPC နဲ့ AWS services တွေ ကို public Internet ကို အသုံးမပြုဘဲနဲ့ Privately connect လုပ်ပေးတာ ဖြစ်ပါတယ်။ Internet Gateway, NAT Gateway နဲ့ Public...

Nov 18, 20252 min read97
VPC Endpoint
M

Myanmar Technical Blog

108 posts

Cloud, Linux, DevOps, Docker, Security အစရှိတဲ့ နည်းပညာများ အကြောင်းကို မြန်မာလို ပြန်လည်မျှဝေပေးမယ့် Blog ပဲဖြစ်ပါတယ်ခဗျာ...