utils/bash/sources/30_mysql

83 lines
1.8 KiB
Bash
Executable File

#!/bin/bash
function grepmysql {
if [[ -z "$1" ]]; then
__func_head "MySQL_SERVER_NAME [WITH_OPTIONS]"
echo
else
if [[ -z "$2" ]]; then
grep -i $1 ~/.mysql_pass | awk 'BEGIN{FS=":"}{ print $2 }'
else
grep -i $1 ~/.mysql_pass | awk 'BEGIN{FS=":"}{ print $2" "$3 }'
fi
fi
}
function mysqlo () {
if [ -z "$1" ]; then
__func_head "SERVER_NAME"
echo
else
PASS=$(grepmysql $1 true)
if [ -z "$PASS" ]; then
echo "Server not found in config: $1"
return 1
fi
PS_SQL=$(grep -i $1 ~/.mysql_pass | awk 'BEGIN{FS=":"}{ print $4 }')
USER=$(grep -i $1 ~/.mysql_pass | awk 'BEGIN{FS=":"}{ print $5 }')
export MYSQL_PS1="$PS_SQL> "
shift
echo $__mysql
mysql -u $USER --password=$PASS $@
unset PS_SQL
fi
}
function mysqls () {
if [ -z "$1" ]; then
__func_head "[z] SERVER_NAME DATABASE_NAME SQL_FILE"
echo
else
GZIP=false
PV=false
_ANY=false
if [[ "$1" == *z* ]]; then
GZIP=true
_ANY=true
fi
if [[ "$1" == *p* ]]; then
PV=true
_ANY=true
fi
if $_ANY; then
shift
fi
USER=$(grep -i $1 ~/.mysql_pass | awk 'BEGIN{FS=":"}{ print $5 }')
PASS=$(grepmysql $1 true)
if [ -z "$PASS" ]; then
echo "Server not found in config: $1"
return 1
fi
if $GZIP; then
if $PV; then
pv $3 | gunzip | mysql -u $USER --password=$PASS -D $2
else
gunzip -c $3 | mysql -u $USER --password=$PASS -D $2
fi
else
if $PV; then
pv $3 | mysql -u $USER --password=$PASS -D $2
else
mysql -u $USER --password=$PASS -D $2 < $3
fi
fi
fi
}