Skip to content

Instantly share code, notes, and snippets.

@charlyie
Last active February 23, 2021 14:10
Show Gist options
  • Save charlyie/54db9a90a2793357f89d209d1838ca57 to your computer and use it in GitHub Desktop.
Save charlyie/54db9a90a2793357f89d209d1838ca57 to your computer and use it in GitHub Desktop.
Will copy a database to another local one
#!/bin/bash
# copydb.sh
# description : will sync databases after backup
APP_VERSION="1.2.0-20210223"
### Editable variables
MYSQL_HOST="127.0.0.1"
MYSQL_USER="automysqlbackup"
MYSQL_PASS="xxxxxxx"
MYSQL_PORT="3306"
MYSQL_BASE_SRC=""
MYSQL_BASE_DEST=""
BACKUP_PATH="/tmp/copydb.backups"
### End of editable variables
PV_INSTALLED="false"
CONFIRMATION=""
DATE_SHORT=$(date +%Y-%m-%d)
DATE=$(date +%Y-%m-%d_%Hh%M)
RED="\033[0;31m"
GREEN="\033[0;32m"
LBLUE="\033[0;36m"
NC="\033[0m" # No Color
# Display output and save it to log file.
cli_output(){
if [[ $QUIET_MODE == "true" ]];
then
return
fi
BREAK="\n"
TIME="[$(date '+%Y-%m-%d %H:%M:%S')] "
COLOR_OPEN_TAG=''
COLOR_CLOSE_TAG=$NC
if [[ $2 == "green" ]];
then
COLOR_OPEN_TAG=$GREEN
elif [[ $2 == "red" ]];
then
COLOR_OPEN_TAG=$RED
elif [[ $2 == "blue" ]];
then
COLOR_OPEN_TAG=$LBLUE
elif [[ $2 == "standard" ]];
then
COLOR_OPEN_TAG=$NC
fi
if [[ $3 == "notime" ]] || [[ $TIME_LOG == false ]]; then
TIME=""
fi
if [[ $4 == "nobreakline" ]]; then
BREAK=""
fi
printf "${COLOR_OPEN_TAG}${TIME}$1 ${COLOR_CLOSE_TAG}${BREAK}"
}
# Manage arguments
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-h|--help)
shift # past argument
cli_output "Copy Database" green notime
cli_output "Usage: ./copydb.sh <source_database> <destination_database>" blue notime
cli_output "Startup:" standard notime
cli_output " --force \t without confirmation." standard notime
cli_output " -h or --help \t\t print this help." standard notime
exit 0
;;
--force)
CONFIRMATION="y"
shift # past argument
;;
-*) # unknown option
cli_output "Invalid option: ${1}. Type --help to show help" red notime
shift
exit 0
;;
--*) # unknown option
cli_output "Invalid option: ${1}. Type --help to show help" red notime
shift
exit 0
;;
*) # unknown option
MYSQL_BASE_SRC="$1"
MYSQL_BASE_DEST="$2"
shift # past argument
shift # past argument
;;
esac
done
if [[ -z $MYSQL_BASE_SRC ]]; then
cli_output "Missing argument <source_database>. Type --help for more help." red notime
shift
exit 0
fi
if [[ -z $MYSQL_BASE_DEST ]]; then
cli_output "Missing argument <destination_database>. Type --help for more help." red notime
shift
exit 0
fi
if [[ "$MYSQL_BASE_SRC" == "$MYSQL_BASE_DEST" ]]; then
cli_output "Source and destination databases cannot be the same value!" red notime
shift
exit 0
fi
if ! mysql -u ${MYSQL_USER} -p${MYSQL_PASS} -e "use $MYSQL_BASE_SRC"; then
cli_output "Source database not found : '$MYSQL_BASE_SRC'" red notime
exit 0
fi
if ! mysql -u ${MYSQL_USER} -p${MYSQL_PASS} -e "use $MYSQL_BASE_DEST"; then
cli_output "Destination database not found : '$MYSQL_BASE_DEST'" red notime
exit 0
fi
while [ "$CONFIRMATION" != "y" ] && [ "$CONFIRMATION" != "n" ]; do
cli_output "Are you sure you want to copy and overwrite '$MYSQL_BASE_SRC' to '$MYSQL_BASE_DEST' ?" green notime nobreakline
read -p "(y/n) " CONFIRMATION
done
if [[ "$CONFIRMATION" == "n" ]]; then
exit 0
fi
if which "pv" > /dev/null;
then
PV_INSTALLED="true"
fi
mkdir -p "${BACKUP_PATH}/${DATE_SHORT}"
current_user=$(whoami)
if [ "$current_user" == "root" ] ; then
chmod 1777 ${BACKUP_PATH} -R
fi
cli_output "Creating backup of destination database (${MYSQL_BASE_DEST})..." standard
mysqldump -u ${MYSQL_USER} -p${MYSQL_PASS} --port=${MYSQL_PORT} -h ${MYSQL_HOST} "${MYSQL_BASE_DEST}" | gzip > "${BACKUP_PATH}/${DATE_SHORT}/${MYSQL_BASE_DEST}-${DATE}.sql.gz"
cli_output "Backup created : ${BACKUP_PATH}/${DATE_SHORT}/${MYSQL_BASE_DEST}-${DATE}.sql.gz" standard
cli_output "Dropping destination database (${MYSQL_BASE_DEST})..." standard
TABLES=$(mysql -u ${MYSQL_USER} -p${MYSQL_PASS} --port=${MYSQL_PORT} -h ${MYSQL_HOST} "${MYSQL_BASE_DEST}" -e 'show tables' | awk '{ print $1}' | grep -v '^Tables' )
for t in $TABLES
do
cli_output ">> Deleting $t table from ${MYSQL_BASE_DEST}..."
mysql -u ${MYSQL_USER} -p${MYSQL_PASS} --port=${MYSQL_PORT} -h ${MYSQL_HOST} "${MYSQL_BASE_DEST}" -e "SET foreign_key_checks = 0;drop table $t;SET foreign_key_checks = 1;"
done
cli_output "Extracting source database (${MYSQL_BASE_SRC})..." standard
mysqldump -u ${MYSQL_USER} -p${MYSQL_PASS} --port=${MYSQL_PORT} -h ${MYSQL_HOST} "${MYSQL_BASE_SRC}" > "/tmp/${MYSQL_BASE_SRC}-${DATE}.sql"
cli_output "Extraction performed : /tmp/${MYSQL_BASE_SRC}-${DATE}.sql" standard
cli_output "Importing source database (${MYSQL_BASE_SRC}) into destination database (${MYSQL_BASE_DEST})..." standard
if [ "$PV_INSTALLED" == "true" ]; then
pv "/tmp/${MYSQL_BASE_SRC}-${DATE}.sql" | mysql -u ${MYSQL_USER} -p${MYSQL_PASS} --port=${MYSQL_PORT} -h ${MYSQL_HOST} -f "${MYSQL_BASE_DEST}"
else
mysql -u ${MYSQL_USER} -p${MYSQL_PASS} --port=${MYSQL_PORT} -h ${MYSQL_HOST} -f "${MYSQL_BASE_DEST}" < "/tmp/${MYSQL_BASE_SRC}-${DATE}.sql"
fi
cli_output "Import successful!" standard
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment