Your IP : 18.218.167.89
#!/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
# login= login for connection
# password= password
# 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
MYSQLCMD=mysql
LOGDIR=/opt/webdir/logs
[[ ! -d $LOGDIR ]] && mkdir -m 750 $LOGDIR
TMPDIR=/tmp
[[ -d /dev/shm ]] && TMPDIR=/dev/shm
TMPFILE=$TMPDIR/.bx_db
# test mandatory option and print error if not exists
test_variables() {
[[ -z $db ]] && db=mysql
if [[ ( -z $login ) && ( -z $password ) ]]; then
if [[ -f $DEFAULT_CONF ]]; then
DEFAULT_CONF_USAGE=1
else
# create config with empty root
echo "# test mysql conf" > $DEFAULT_CONF
echo "[mysqld]" >> $DEFAULT_CONF
echo "user=root" >> $DEFAULT_CONF
echo "password=" >> $DEFAULT_CONF
echo >> $DEFAULT_CONF
chmod 640 $DEFAULT_CONF
# test default
mysql -e 'select 1;' 1>/dev/null 2>&1
if [[ $? -gt 0 ]]; then
echo "{\"changed\":false,\"failed\":true,\"msg\":\"option $var= is mandatory, you must define it or set in $DEFAULT_CONF file\"}"
rm -f $DEFAULT_CONF
exit 1
fi
DEFAULT_CONF_USAGE=1
fi
else
login=root
MYSQLCMD=$MYSQLCMD" --user=$login --password='$password'"
fi
}
get_db_list() {
# test if mysql running
system_process=$(ps -ef | grep -w 'mysqld' | grep -vw grep | grep -vwc $$)
if [[ $system_process -gt 0 ]]; then
$MYSQLCMD --skip-column-names -e "show databases;" | \
grep -vw 'information_schema\|performance_schema\|mysql\|test' | \
grep -v '#mysql' >$TMPFILE 2>&1
if [[ $? -gt 0 ]]; then
echo "{\"changed\":false,\"failed\":true,\"msg\":\"mysql cmd return error $(head $TMPFILE)\"}"
rm -f $TMPFILE
exit 1
fi
dbs_list=$(grep -vw "$db" $TMPFILE| awk '{printf "\"%s\",", $1}' | sed -e 's/,$//;')
else
dbs_list=""
fi
dbs_out="{\"ansible_facts\":{"
dbs_out=$dbs_out"\"dbs_list\":["
dbs_out=$dbs_out$dbs_list
dbs_out=$dbs_out"]}}"
echo $dbs_out
}
# get options from file
source ${1}
test_variables
get_db_list