utils/bash/sources/30_mysql

83 lines
1.8 KiB
Plaintext
Raw Permalink Normal View History

2014-08-04 09:40:25 +00:00
#!/bin/bash
2022-08-11 16:39:41 +00:00
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
}
2014-08-04 09:40:25 +00:00
function mysqlo () {
2016-06-23 07:51:12 +00:00
if [ -z "$1" ]; then
2014-08-04 09:40:25 +00:00
__func_head "SERVER_NAME"
echo
else
PASS=$(grepmysql $1 true)
2016-06-23 07:51:12 +00:00
if [ -z "$PASS" ]; then
2014-08-04 09:40:25 +00:00
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 () {
2016-06-23 07:51:12 +00:00
if [ -z "$1" ]; then
__func_head "[z] SERVER_NAME DATABASE_NAME SQL_FILE"
2014-08-04 09:40:25 +00:00
echo
else
2016-06-23 07:51:12 +00:00
GZIP=false
2018-12-13 21:52:33 +00:00
PV=false
_ANY=false
if [[ "$1" == *z* ]]; then
2016-06-23 07:51:12 +00:00
GZIP=true
2018-12-13 21:52:33 +00:00
_ANY=true
fi
if [[ "$1" == *p* ]]; then
PV=true
_ANY=true
fi
if $_ANY; then
shift
fi
2016-06-23 07:51:12 +00:00
2014-08-04 09:40:25 +00:00
USER=$(grep -i $1 ~/.mysql_pass | awk 'BEGIN{FS=":"}{ print $5 }')
PASS=$(grepmysql $1 true)
2016-06-23 07:51:12 +00:00
if [ -z "$PASS" ]; then
2014-08-04 09:40:25 +00:00
echo "Server not found in config: $1"
return 1
2016-06-23 07:51:12 +00:00
fi
2014-08-04 09:40:25 +00:00
2016-06-23 07:51:12 +00:00
if $GZIP; then
2018-12-13 21:52:33 +00:00
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
2016-06-23 07:51:12 +00:00
else
2018-12-13 21:52:33 +00:00
if $PV; then
pv $3 | mysql -u $USER --password=$PASS -D $2
else
mysql -u $USER --password=$PASS -D $2 < $3
fi
2014-08-04 09:40:25 +00:00
fi
fi
}