Your IP : 18.221.215.5
#!/bin/bash
# create dump for database and upload it to local mysql
# src= server with source of db ( ip address or unix socket )
# dst= server with destination of db
# db= db anme
# config_file= mysql client config file
# Carefully! Ansible already provides values in quotes
#set -e
BY_TABLE=1 # if set to 1 it is create dump per table
# Longer but more reliable for large databases
REPLACE=0 # relace destionation database or not
DEFAULT_CONF=/root/.my.cnf # can set login and password from file
DEFAULT_CONF_USAGE=0
BASE_DIR=/opt/webdir
LOGDIR=$BASE_DIR/logs
[[ ! -d $LOGDIR ]] && mkdir -m 750 $LOGDIR
LOG_FILE=$LOGDIR/create_dbs.log
CACHE_DIR=$BASE_DIR/tmp
[[ ! -d $CACHE_DIR ]] && mkdir -p -m 700 $CACHE_DIR
CACHE_FILE=(mktemp $CACHE_DIR/.bx_dumpXXXXXX)
save_log(){
message=$1
printf "%-15s: [%s] %s\n" "$(date +%Y/%m/%d:%H:%M:%S)" "$$" "$message" >> $LOG_FILE
}
print_good(){
local msg="${1}"
local changed="${2:-true}"
save_log "$msg"
echo "{\"changed\": $changed, \"msg\": \"$msg\"}"
[[ -f $CACHE_FILE ]] && rm -f $CACHE_FILE
exit
}
print_error(){
local msg="${1}"
save_log "$msg"
echo "{\"changed\": false, \"failed\": true, \"msg\": \"$msg\"}"
[[ -f $CACHE_FILE ]] && rm -f $CACHE_FILE
exit 1
}
# test mandatory option and print error if not exists
test_variables() {
for desc in "src=$src" "dst=$dst" "db=$db" "config_file=$config_file"; do
var=$(echo $desc | cut -d'=' -f1)
val=$(echo $desc | cut -d'=' -f2)
if [[ -z "$val" ]]; then
# test config_file variable
if [[ "$var" == "config_file" ]]; then
if [[ ! -f "$DEFAULT_CONF" ]]; then
print_error "Not found client config file=$DEFAULT_CONF"
fi
DEFAULT_CONF_USAGE=1
else
print_error "option $var= is mandatory, you must define it"
fi
fi
done
# replace existen DB
REPLACE=0
if [[ ( -n "$replace" ) && ( $(echo "$replace" | grep -wci 'yes') -gt 0 ) ]]; then
REPLACE=1
fi
# dump database all at once or by table
BY_TABLE=1
if [[ ( -n "$by_table" ) && ( $(echo "$by_table" | grep -wci 'no') -gt 0 ) ]]; then
BY_TABLE=0
fi
SITE_TYPE='link' # if link - not dumped db, no dublicate
if [[ ( -n "$site_type" ) && ( $(echo "$site_type" | grep -wic 'kernel') -gt 0 ) ]]; then
SITE_TYPE='kernel'
fi
}
# create dump db and upload it to dest
dump_db() {
save_log "Start recreate DBs on slave server, REPLACE=$REPLACE BY_TABLE=$BY_TABLE"
if [[ "$SITE_TYPE" == "link" ]]; then
print_good "SITE_TYPE=$SITE_TYPE dump only for kernels. nothing to do" "false"
fi
# create command lines for mysqldump and mysql
if [[ $DEFAULT_CONF_USAGE -eq 1 ]]; then
src_mysqldump_cmd="mysqldump --defaults-file=$DEFAULT_CONF --opt --single-transaction"
src_mysql_cmd="mysql --defaults-file=$DEFAULT_CONF"
dst_mysql_cmd="mysql --defaults-file=$DEFAULT_CONF"
else
src_mysqldump_cmd="mysqldump --defaults-file=$config_file --opt --single-transaction"
src_mysql_cmd="mysql --defaults-file=$config_file"
dst_mysql_cmd="mysql --defaults-file=$config_file"
fi
# src is host address or local socket?
if [[ $(echo $src | grep -c '^/') -gt 0 ]]; then
src_mysql_cmd=$src_mysql_cmd" --socket=$src"
src_mysqldump_cmd=$src_mysqldump_cmd" --socket=$src"
else
src_mysql_cmd=$src_mysql_cmd" --host=$src"
src_mysqldump_cmd=$src_mysqldump_cmd" --host=$src"
fi
# dst is host address or local socket
if [[ $(echo $dst | grep -c '^/') -gt 0 ]]; then
dst_mysql_cmd=$dst_mysql_cmd" --socket=$dst"
else
dst_mysql_cmd=$dst_mysql_cmd" --host=$src"
fi
# final touch
src_mysql_cmd=$src_mysql_cmd" $db"
src_mysqldump_cmd=$src_mysqldump_cmd" $db"
# test if database exists on dst server, if exist => exit
if_db=$($dst_mysql_cmd \
-e "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$db'" 2>/dev/null)
if [[ -n "$if_db" ]];then
save_log "Found $db on server"
if [[ $REPLACE -eq 0 ]]; then
print_good "Database db=$db already exists. nothing to do!" "false"
else
save_log " REPLACE=$REPLACE and db will be replaced"
$dst_mysql_cmd -e "drop database $db;" >> $LOG_FILE 2>&1 || \
print_error "error occurred during deletion db=$db"
if_db=0
fi
fi
# create database
save_log "create $db"
$dst_mysql_cmd -e "create database $db;" >> $LOG_FILE 2>&1 || \
print_error "error occurred during creation db=$db"
# recovery database by mysqldump of it tables: one by one
save_log "start recovery data from $db; BY_TABLE=$BY_TABLE"
if [[ $BY_TABLE -eq 1 ]]; then
#save_log "$src_mysql_cmd -N -e \"show tables;\""
# get list of tables from master
$src_mysql_cmd -N -e "show tables;" >$CACHE_FILE 2>> $LOG_FILE || \
print_error "error occurred during getting list of tables db=$db"
table_list=$(cat $CACHE_FILE | awk '{print $1}')
[[ -z "$table_list" ]] && \
print_good "there are no tables in database db=$db. nothing to do!" "false"
# process table one by one, exit if error happened
for table in $table_list; do
save_log " $db - create table $table"
#save_log " $src_mysqldump_cmd $table | $dst_mysql_cmd $db"
$src_mysqldump_cmd $table | $dst_mysql_cmd $db >> $LOG_FILE 2>&1
for rtn in ${PIPESTATUS[*]}; do
[[ $rtn -gt 0 ]] && \
print_error "error occurred during recovery db=$db table=$table"
done
save_log " $db - create table $table complete"
done
err=0
# recovery database by mysqldump of whole database
else
$src_mysqldump_cmd | $dst_mysql_cmd >> $LOG_FILE 2>&1
err=$?
fi
if [[ $err -gt 0 ]]; then
print_error "error occurred during database db=$db recovery"
else
print_good "recovery database db=$db is complete"
fi
exit $err
}
# get options from file
source ${1}
test_variables
dump_db