Initial import

This commit is contained in:
Christoph 2017-02-21 02:31:30 +01:00
commit 9ec6b1c2e1
12 changed files with 2770 additions and 0 deletions

33
FORWARDERS_IN.sql Normal file
View File

@ -0,0 +1,33 @@
DROP FUNCTION IF EXISTS FORWARDERS_IN ;
DELIMITER |
CREATE FUNCTION FORWARDERS_IN (forewarders_str TEXT,
email_str TEXT,
vacation_domain TEXT ,
list_seperator CHAR ,
vacation_enable BOOLEAN)
RETURNS TEXT
DETERMINISTIC
BEGIN
DECLARE return_str TEXT;
DECLARE local_email_part TEXT;
DECLARE domain_email_part TEXT;
SET return_str = email_str;
IF vacation_enable THEN
SET local_email_part = SUBSTRING(email_str,1, LOCATE('@',email_str) - 1);
SET domain_email_part = SUBSTRING(email_str, LOCATE('@',email_str) + 1, LENGTH(email_str));
SET return_str = CONCAT(return_str, list_seperator, local_email_part, "#" ,domain_email_part,"@", vacation_domain);
END IF;
IF LENGTH(forewarders_str) > 2 THEN
SET return_str = CONCAT(return_str, list_seperator, forewarders_str);
END IF;
RETURN return_str;
END |
DELIMITER ;

55
FORWARDERS_OUT.sql Normal file
View File

@ -0,0 +1,55 @@
DROP FUNCTION IF EXISTS FORWARDERS_OUT ;
DELIMITER |
CREATE FUNCTION FORWARDERS_OUT (email_str TEXT, vacation_domain TEXT , list_seperator CHAR)
RETURNS TEXT
DETERMINISTIC
BEGIN
DECLARE forward_str TEXT;
DECLARE local_email_part TEXT;
DECLARE domain_email_part TEXT;
DECLARE first_char CHAR;
DECLARE last_char CHAR;
-- get list of forwarders
--
SELECT goto INTO forward_str FROM alias WHERE address=email_str;
-- entferne mailbox emailadresse
--
SET forward_str = REPLACE(forward_str, email_str, '' );
-- SELECT REPLACE(forward_str, email_str, '' ) INTO forward_str;
-- entferne vacation adresse
--
SET local_email_part = SUBSTRING(email_str,1, LOCATE('@',email_str) - 1);
SET domain_email_part = SUBSTRING(email_str, LOCATE('@',email_str) + 1, LENGTH(email_str));
SET forward_str = REPLACE(forward_str, CONCAT(local_email_part, "#" ,domain_email_part,"@", vacation_domain), '');
-- SELECT REPLACE(forward_str, CONCAT(list_seperator,list_seperator), list_seperator) INTO forward_str;
-- enferne doppelte seperatorzeichen
--
WHILE LOCATE(CONCAT(list_seperator,list_seperator) , forward_str) DO
SET forward_str = REPLACE(forward_str, CONCAT(list_seperator,list_seperator), list_seperator);
-- SELECT REPLACE(forward_str, CONCAT(list_seperator,list_seperator), list_seperator) INTO forward_str;
END WHILE ;
-- entferne erstes zeichen wenn es das seperatorzeichen ist
--
IF LEFT(forward_str,1) = list_seperator THEN
SET forward_str = SUBSTRING(forward_str FROM 2);
-- SELECT SUBSTRING(forward_str FROM 2) INTO forward_str;
END IF;
-- entferne letztes zeichen wenn es das seperatorzeichen ist
--
IF RIGHT(forward_str,1) = list_seperator THEN
SET forward_str = SUBSTRING(forward_str , 1, LENGTH(forward_str) - 1);
-- SELECT SUBSTRING(forward_str , 1, LENGTH(forward_str) - 1) INTO forward_str;
END IF;
RETURN forward_str;
END |
DELIMITER ;

148
OLD/dump_all_mysql_dbs.sh Executable file
View File

@ -0,0 +1,148 @@
#!/usr/bin/env bash
## - *** some defines *** - ##
## - Backup direction
## -
backup_base_dir=$1
backup_base_dir=${backup_base_dir:="/data/backup"}
backup_dir=${backup_base_dir}/mysql
mkdir -p $backup_dir
_local=$2
_local=${_local:=false}
mkdir -p $backup_dir
## - Database connection data
## -
db_user=backup
db_pass=Rgfwcsgth6Zjk4W
db_host=localhost
db_port=3306
## - Some needed binaries
## -
mysql_bin=`which mysql`
mysqldump_bin=`which mysqldump`
mkdir_bin=`which mkdir`
date_bin=`which date`
gzip_bin=`which gzip`
find_bin=`which find`
if [ -z "$mysql" ]; then
mysql="/usr/local/mysql/bin/mysql"
fi
if [ -z "$mysqldump" ]; then
mysqldump="/usr/local/mysql/bin/mysqldump"
fi
YEAR=`date +"%Y"`
MONTH=`date +"%m"`
DAY=`date +"%d"`
HOUR=`date +"%H"`
MINUTE=`date +"%M"`
DATE=${YEAR}${MONTH}${DAY}-${HOUR}${MINUTE}
## - Hold backups at least $days days
## -
days=3
## - ** End: some defines *** - ##
DATABASES=`$mysql_bin -h$db_host -P$db_port -u$db_user -p$db_pass -N -s -e 'show databases'`
## - backup all databases
## -
for i in $DATABASES ; do
$mkdir_bin -p ${backup_dir}/${i}
mysqldump_flags="--opt --routines -l"
if [ "$i" = "information_schema" -o "$i" = "performance_schema" ];then
mysqldump_flags="--single-transaction $mysqldump_flags"
fi
echo -e "\tsave database $i ..."
$mysqldump_bin $mysqldump_flags -h$db_host -P$db_port -u$db_user -p$db_pass $i | \
$gzip_bin | cat > ${backup_dir}/${i}/${i}-${DATE}.sql.gz
## - dumptables "user" and "db" from database mysql"
## -
if [ "$i" = "mysql" ]; then
_flags="--add-locks --disable-keys --extended-insert --lock-tables --quick --no-create-info"
_table=user
## - dump table user (database mysql)
## - each row as aseperate sql statement
## -
## - | awk '{gsub(/\),\(/,");\n(")}; 1'
## - | sed 's/^(/INSERT INTO `user` VALUES (/'
## -
## - delete row containig host = "localhost" with empty user and password
## - sed "/^INSERT INTO \`user\` VALUES ('[^']*','root',.\+/d"
## -
## - delete rows containig user "root"
## - sed "/^INSERT INTO \`user\` VALUES ('[^']*','root',.\+/d"
## -
## - delete rows containig user "debian-sys-maint"
## - sed "/^INSERT INTO \`user\` VALUES ('[^']*','debian-sys-maint',.\+/d"
## -
$mysqldump_bin $_flags -h$db_host -P$db_port -u$db_user -p$db_pass $i $_table \
| awk '{gsub(/\),\(/,");\n(")}; 1' \
| sed "s/^(/INSERT INTO \`$_table\` VALUES (/" \
| sed "/^INSERT INTO \`$_table\` VALUES ('localhost','','','.*/d" \
| sed "/^INSERT INTO \`$_table\` VALUES ('[^']*','root','.*/d" \
| sed "/^INSERT INTO \`$_table\` VALUES ('[^']*','debian-sys-maint','.*/d" \
> ${backup_dir}/${i}/${i}-${_table}-${DATE}.sql
_table=db
## - dump table db (database mysql)
## - each row as aseperate sql statement
## -
## - | awk '{gsub(/\),\(/,");\n(")}; 1'
## - | sed 's/^(/INSERT INTO `user` VALUES (/'
## -
## - delete rows for database names "test*"
## - sed "/^INSERT INTO \`$_table\` VALUES ('[^']*','test[^']*','.*/d"
## -
$mysqldump_bin $_flags -h$db_host -P$db_port -u$db_user -p$db_pass $i $_table \
| awk '{gsub(/\),\(/,");\n(")}; 1' \
| sed "s/^(/INSERT INTO \`$_table\` VALUES (/" \
| sed "/^INSERT INTO \`$_table\` VALUES ('[^']*','test[^']*','.*/d" \
> ${backup_dir}/${i}/${i}-${_table}-${DATE}.sql
## - BSD sed is not GNU sed , so we used "awk" for substitution
## - a newline. On GNU sed (like on linux systems), also the
## - following sed statement works
## -
#| sed 's/),(/);\n(/g' \
fi
done
## - Create archive from the whole backup
## -
if ! $_local ; then
_curdir=`pwd`
cd $backup_dir
cd .. > /dev/null 2>&1
tar -czf mysql_dump-${DATE}.tar.gz `find mysql -name "*${DATE}*"`
cd $_curdir
fi
## - remove all files older than $days days..
## -
$find_bin $backup_dir -type f -mtime +${days} -exec rm {} \;
exit 0

212
OLD/restore_all_mysql_dbs.sh Executable file
View File

@ -0,0 +1,212 @@
#!/usr/bin/env bash
## - Database connection data
## -
db_user=root
db_pass=
db_host=localhost
db_port=3306
## - Some needed binaries
## -
mysql_bin=`which mysql`
mysqldump_bin=`which mysqldump`
gunzip_bin=`which gunzip`
find_bin=`which find`
echononl(){
echo X\\c > /tmp/shprompt$$
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
echo "$*\\c" 1>&2
else
echo -n "$*" 1>&2
fi
rm /tmp/shprompt$$
}
fatal(){
echo ""
echo Fehler: $*
echo -e "\n\t\033[31m\033[1mSkript wird abgebrochen\033[m\033[m\n"
exit 1
}
echo ""
echo -n "Give password for database user \"${db_user}\" [$db_pass]: "
stty -echo
read _pass
stty echo
while [ "X" == "X$_pass" ];do
echononl "Enry must not be empty! []: "
stty -echo
read _pass
stty echo
done
db_pass=$_pass
echo ""
PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/root/bin
BACKUP_BASE_DIR="/data/backup/mysql"
if [ ! -d $BACKUP_BASE_DIR ] ; then
echo
echo " Backup directory not found"
echo
exit 1
fi
logfile=restore_db_mysql.log
databases=`$find_bin $BACKUP_BASE_DIR -maxdepth 1 -mindepth 1 -type d -exec basename {} \;`
echo ""
echo "Do you want me restoring also the MySQL intern Database \"mysql\""
echo ""
_opt1="Yes, restore all table of intern database \"mysql\""
_opt2="Only add not present entries to table \"user\" and table \"db\""
_opt3="No, let all tables from MySQL intern database \"mysql\" untouched"
_opt4="QUIT - exit script NOW"
options=("$_opt1" "$_opt2" "$_opt3" "$_opt4")
PS3="Choose (1-4):"
_restore_mysql=3
select opt in "${options[@]}"
do
case $opt in
$_opt1)
_restore_mysql=1
break
;;
$_opt2)
_restore_mysql=2
break
;;
$_opt3)
_restore_mysql=3
break
;;
$_opt4)
exit 2
;;
*)
echo ""
echo "!! INVALID CHOISE !!"
echo ""
echo "1) $_opt1"
echo "2) $_opt2"
echo "3) $_opt3"
echo ""
;;
esac
done
echo -e "\n\tRestoring Databases\n"
for db in $databases ; do
#[ "$db" = "information_schema" ] && continue
_list=`ls -d -t ${BACKUP_BASE_DIR}/$db/*.sql.gz`
file=`echo $_list | awk '{print$1}'`
if [ "$db" = "information_schema" ]; then
echo ""
echononl "Do you want to restore database \"$db\" too? [yes/no]: "
read OK
while [ "X$OK" != "Xyes" -a "X$OK" != "XYes" -a "X$OK" != "Xja" -a "X$OK" != "XJa" \
-a "X$OK" != "XNo" -a "X$OK" != "Xno" -a "X$OK" != "Xn" -a "X$OK" != "Xnein" -a "X$OK" != "XNein" ]; do
echononl "Incorrect entry! [yes/no]: "
read OK
done
echo ""
if [ $OK = "Yes" -o $OK = "yes" -o "$OK" = "ja" -o "$OK" = "Ja" ]; then
echo -e "\t\trestoring ${db} (`basename $file`) .."
gunzip_bin < $file | mysql_bin -h$db_host -u$db_user -p'$db_pass $db'
fi
elif [ "$db" = "performance_schema" ]; then
echo ""
echononl "Do you want to restore database \"$db\" too? [yes/no]: "
read OK
while [ "X$OK" != "Xyes" -a "X$OK" != "XYes" -a "X$OK" != "Xja" -a "X$OK" != "XJa" \
-a "X$OK" != "XNo" -a "X$OK" != "Xno" -a "X$OK" != "Xn" -a "X$OK" != "Xnein" -a "X$OK" != "XNein" ]; do
echononl "Incorrect entry! [yes/no]: "
read OK
done
echo ""
if [ $OK = "Yes" -o $OK = "yes" -o "$OK" = "ja" -o "$OK" = "Ja" ]; then
echo -e "\t\trestoring ${db} (`basename $file`) .."
gunzip_bin < $file | mysql_bin -h$db_host -u$db_user -p'$db_pass $db'
fi
elif [ "$db" = "mysql" ]; then
if [ "$_restore_mysql" = "1" ]; then
## - save entry for User "debian-sys-maint"
## -
declare -i num
num=0
num=`$mysql_bin -u$db_user -P$db_port -u$db_user -p$db_pass $db -N -s -e "SELECT count(*) from user Where USER = 'debian-sys-maint'"`
if [ $num -gt 0 ];then
COLUMNS=`$mysql_bin -h$db_host -P$db_port -u$db_user -p$db_pass -N -s -e "SHOW COLUMNS FROM user FROM mysql" | awk '{print$1}'`
stmt_update="";
for column in $COLUMNS ; do
val=""
val=`mysql -h$db_host -P$db_port -u$db_user -p$db_pass mysql -N -s -e "SELECT $column FROM user WHERE User = 'debian-sys-maint'"`
stmt_update="UPDATE user SET $column = '$val' WHERE User = 'debian-sys-maint';$stmt_update"
done
fi
echo -e "\t\trestoring ${db} (`basename $file`) .."
$gunzip_bin < $file | $mysql_bin -h$db_host -u$db_user -p$db_pass $db
if [ $num -gt 0 ];then
stmt_insert="INSERT INTO user (Host,User) VALUES ('localhost','debian-sys-maint')"
$mysql_bin -u$db_user -P$db_port -u$db_user -p$db_pass $db -N -s -e "$stmt_insert"
$mysql_bin -u$db_user -P$db_port -u$db_user -p$db_pass $db -N -s -e "$stmt_update"
fi
# FLUSH PRIVILEGES
$mysql_bin -h$db_host -P$db_port -u$db_user -p$db_pass -N -s -e "FLUSH PRIVILEGES"
echo ""
echo "If password for database user \"root\" has changed within restoring \"${db}\""
echo -n "database, enter the new one. If not, only type <return>: "
stty -echo
read _pass
stty echo
if [ "X" != "X$_pass" ];then
db_pass=$_pass
fi
echo -e "\n"
elif [ "$_restore_mysql" = "2" ]; then
echo ""
_tables="db user"
for _table in $_tables ; do
_list_table=`ls -d -t ${BACKUP_BASE_DIR}/$db/mysql-${_table}-*.sql`
file_table=`echo $_list_table | awk '{print$1}'`
echo -e "\t\trestoring table \"$_table\" from database ${db} (`basename $file_table`) .."
$mysql_bin -f -h$db_host -P$db_port -u$db_user -p$db_pass $db < $file_table
done
echo ""
fi
else
echo -e "\t\trestoring ${db} (`basename $file`) .."
$mysql_bin -h$db_host -P$db_port -u$db_user -p$db_pass -N -s -e "DROP DATABASE IF EXISTS $db"
$mysql_bin -h$db_host -P$db_port -u$db_user -p$db_pass -N -s -e "CREATE DATABASE $db CHARACTER SET utf8 COLLATE utf8_general_ci"
$gunzip_bin < $file | $mysql_bin -h$db_host -P$db_port -u$db_user -p$db_pass $db
fi
done
echo
$mysql_bin -h$db_host -P$db_port -u$db_user -p$db_pass -N -s -e "FLUSH PRIVILEGES"
exit 0

37
create_databases.sh Executable file
View File

@ -0,0 +1,37 @@
#!/usr/bin/env bash
mysql_credential_args="--login-path=local"
_drop=false
echo ""
while read _db_name _db_user _db_pass ; do
echo -e "\tprocessing database \"$_db_name\""
if $_drop ; then
mysql $mysql_credential_args -N -s -e \
"DROP DATABASE $_db_name"
mysql $mysql_credential_args mysql -N -s -e \
"DELETE FROM db WHERE Db = '$_db_name'"
mysql $mysql_credential_args mysql -N -s -e \
"DELETE FROM user WHERE User = '$_db_user'"
else
mysql $mysql_credential_args -N -s -e \
"CREATE DATABASE IF NOT EXISTS $_db_name CHARACTER SET utf8 COLLATE utf8_general_ci"
mysql $mysql_credential_args -N -s -e \
"GRANT ALL ON $_db_name.* TO '$_db_user'@'localhost' IDENTIFIED BY '$_db_pass'"
mysql $mysql_credential_args -N -s -e \
"USE mysql; UPDATE user SET Super_priv = 'Y' WHERE User = '$_db_user'"
fi
done < databases.txt
echo ""
mysql $mysql_credential_args -N -s -e "FLUSH PRIVILEGES"
exit 0

49
flush_host_cache.sh Executable file
View File

@ -0,0 +1,49 @@
#!/usr/bin/env bash
#LOGGING=true
LOGGING=false
mysqladmin=`realpath $(which mysqladmin) 2>/dev/null`
if [ -z "$mysqladmin" ]; then
if [ -x "/usr/local/mysql/bin/mysqladmin" ]; then
mysql=/usr/local/mysql/bin/mysqladmin
else
echo
echo -e "\t[ Error ]: \"mysqladmin\" not found !!!"
echo
exit
fi
fi
## - Since Version 5.6, that method is considered as insecure.
## - To avoid giving the password on command line, we use an
## - encrypted option file
## -
## - Create (encrypted) option file:
## - $ mysql_config_editor set --login-path=local --socket=/var/run/mysqld/mysqld.sock --user=backup --password
## - $ Password:
## -
## - Use of option file:
## - $ mysql --login-path=local ...
## -
mysql_credential_args="--login-path=local"
#mysql_user='sys-maint'
#mysql_password='9A7NUgaJST1XiEUU'
if $LOGGING ;then
echo -e "\n[ `date` ] Going to flush host cache.."
fi
if [ -n "$mysql_credential_args" ] ; then
$mysqladmin $mysql_credential_args flush-hosts
else
$mysqladmin -u$mysql_user -p$mysql_password flush-hosts
fi
[[ $? -gt 0 ]] && echo -e "\t[ Error ]: Flushing host cache failed !!"
if $LOGGING ;then
echo -e "\n[ `date` ] End flushing host cache"
fi
exit 0

49
flush_query_cache.sh Executable file
View File

@ -0,0 +1,49 @@
#!/usr/bin/env bash
#LOGGING=true
LOGGING=false
mysql=`realpath $(which mysql) 2>/dev/null`
if [ -z "$mysql" ]; then
if [ -x "/usr/local/mysql/bin/mysql" ]; then
mysql=/usr/local/mysql/bin/mysql
else
echo
echo -e "\t[ Error ]: \"mysql\" not found !!!"
echo
exit
fi
fi
## - Since Version 5.6, that method is considered as insecure.
## - To avoid giving the password on command line, we use an
## - encrypted option file
## -
## - Create (encrypted) option file:
## - $ mysql_config_editor set --login-path=local --socket=/var/run/mysqld/mysqld.sock --user=backup --password
## - $ Password:
## -
## - Use of option file:
## - $ mysql --login-path=local ...
## -
mysql_credential_args="--login-path=local"
#mysql_user='sys-maint'
#mysql_password='9A7NUgaJST1XiEUU'
if $LOGGING ;then
echo -e "\n[ `date` ] Going to flush query cache.."
fi
if [ -n "$mysql_credential_args" ] ; then
$mysql $mysql_credential_args -N -s -e "FLUSH QUERY CACHE"
else
$mysql -u$mysql_user -p$mysql_password -N -s -e "FLUSH QUERY CACHE"
fi
[[ $? -gt 0 ]] && echo -e "\t[ Error ]: Flushing query cache failed !!"
if $LOGGING ;then
echo -e "\n[ `date` ] End flushing query cache"
fi
exit 0

266
max_memory_limit_mysql.sh Executable file
View File

@ -0,0 +1,266 @@
#!/usr/bin/env bash
mysql_credential="--login-path=local"
#mysql_credential="--defaults-file=/etc/mysql/debian.cnf"
#mysql_credential="--defaults-file=/usr/local/mysql/sys-maint.cnf"
declare -i key_buffer_size
declare -i query_cache_size
declare -i tmp_table_size
declare -i innodb_buffer_pool_size
declare -i innodb_additional_mem_pool_size
declare -i innodb_log_buffer_size
declare -i max_connections
declare -i sort_buffer_size
declare -i read_buffer_size
declare -i read_rnd_buffer_size
declare -i join_buffer_size
declare -i thread_stack
declare -i binlog_cache_size
key_buffer_size=`mysql $mysql_credential -N -s -e \
"SHOW VARIABLES LIKE 'key_buffer_size'" | awk '{print$2}'`
query_cache_size=`mysql $mysql_credential -N -s -e \
"SHOW VARIABLES LIKE 'query_cache_size'" | awk '{print$2}'`
tmp_table_size=`mysql $mysql_credential -N -s -e \
"SHOW VARIABLES LIKE 'tmp_table_size'" | awk '{print$2}'`
innodb_buffer_pool_size=`mysql $mysql_credential -N -s -e \
"SHOW VARIABLES LIKE 'innodb_buffer_pool_size'" | awk '{print$2}'`
innodb_additional_mem_pool_size=`mysql $mysql_credential -N -s -e \
"SHOW VARIABLES LIKE 'innodb_additional_mem_pool_size'" | awk '{print$2}'`
innodb_log_buffer_size=`mysql $mysql_credential -N -s -e \
"SHOW VARIABLES LIKE 'innodb_log_buffer_size'" | awk '{print$2}'`
max_connections=`mysql $mysql_credential -N -s -e \
"SHOW VARIABLES LIKE 'max_connections'" | awk '{print$2}'`
sort_buffer_size=`mysql $mysql_credential -N -s -e \
"SHOW VARIABLES LIKE 'sort_buffer_size'" | awk '{print$2}'`
read_buffer_size=`mysql $mysql_credential -N -s -e \
"SHOW VARIABLES LIKE 'read_buffer_size'" | awk '{print$2}'`
read_rnd_buffer_size=`mysql $mysql_credential -N -s -e \
"SHOW VARIABLES LIKE 'read_rnd_buffer_size'" | awk '{print$2}'`
join_buffer_size=`mysql $mysql_credential -N -s -e \
"SHOW VARIABLES LIKE 'join_buffer_size'" | awk '{print$2}'`
thread_stack=`mysql $mysql_credential -N -s -e \
"SHOW VARIABLES LIKE 'thread_stack'" | awk '{print$2}'`
binlog_cache_size=`mysql $mysql_credential -N -s -e \
"SHOW VARIABLES LIKE 'binlog_cache_size'" | awk '{print$2}'`
mysql_version=`mysql $mysql_credential -N -s -e \
"SHOW VARIABLES LIKE 'version'" | awk '{print$2}' | sed -e's/-.*$//'`
mysql_version_compile_machine=`mysql $mysql_credential -N -s -e \
"SHOW VARIABLES LIKE 'version_compile_machine'" | awk '{print$2}'`
declare -i max_used_connections
declare -i uptime
declare -i threads_connected
max_used_connections=`mysql $mysql_credential -N -s -e \
"SHOW STATUS LIKE 'Max_used_connections'" | awk '{print$2}'`
threads_connected=`mysql $mysql_credential -N -s -e \
"SHOW STATUS LIKE 'Threads_connected'" | awk '{print$2}'`
uptime=`mysql $mysql_credential -N -s -e \
"SHOW STATUS LIKE 'Uptime'" | awk '{print$2}'`
_now=`date +%s`
_timestamp_start=`expr $_now - $uptime`
starttime=`date -d \@$_timestamp_start +%c`
days=`echo "scale=0 ; $uptime / 86400" | bc -l`
days_rest=`echo "scale=0 ; $uptime % 86400" | bc -l`
hours=`echo "scale=0 ; $days_rest / 3600" | bc -l`
hours_rest=`echo "scale=0 ; $days_rest % 3600" | bc -l`
minutes=`echo "scale=0 ; $hours_rest / 60" | bc -l`
seconds=`echo "scale=0 ; $hours_rest % 60" | bc -l`
uptime_string="$days days $hours hrs $minutes min $seconds sec"
## -
#declare -i myisam_sort_buffer_size=`mysql --login-path=local -N -s -e \
# "SHOW VARIABLES LIKE 'myisam_sort_buffer_size'" | awk '{print$2}'`
echo ""
_key_buffer_size=""
if [ $key_buffer_size -gt 1048576 ]; then
_key_buffer_size=" (`expr $key_buffer_size / 1024 / 1024`M)"
elif [ $key_buffer_size -gt 1024 ]; then
_key_buffer_size=" (`expr $key_buffer_size / 1024`K)"
fi
_query_cache_size=""
if [ $query_cache_size -gt 1048576 ]; then
_query_cache_size=" (`expr $query_cache_size / 1024 / 1024`M)"
elif [ $key_buffer_size -gt 1024 ]; then
_query_cache_size=" (`expr $query_cache_size / 1024`K)"
fi
_tmp_table_size=""
if [ $tmp_table_size -gt 1048576 ]; then
_tmp_table_size=" (`expr $tmp_table_size / 1024 / 1024`M)"
elif [ $tmp_table_size -gt 1024 ]; then
_tmp_table_size=" (`expr $tmp_table_size / 1024`K)"
fi
_innodb_buffer_pool_size=""
if [ $innodb_buffer_pool_size -gt 1048576 ]; then
_innodb_buffer_pool_size=" (`expr $innodb_buffer_pool_size / 1024 / 1024`M)"
elif [ $innodb_buffer_pool_size -gt 1024 ]; then
_innodb_buffer_pool_size=" (`expr $innodb_buffer_pool_size / 1024`K)"
fi
_innodb_additional_mem_pool_size=""
if [ $innodb_additional_mem_pool_size -gt 1048576 ]; then
_innodb_additional_mem_pool_size=" (`expr $innodb_additional_mem_pool_size / 1024 / 1024`M)"
elif [ $innodb_additional_mem_pool_size -gt 1024 ]; then
_innodb_additional_mem_pool_size=" (`expr $innodb_additional_mem_pool_size / 1024`K)"
fi
_innodb_log_buffer_size=""
if [ $innodb_log_buffer_size -gt 1048576 ]; then
_innodb_log_buffer_size=" (`expr $innodb_log_buffer_size / 1024 / 1024`M)"
elif [ $innodb_log_buffer_size -gt 1024 ]; then
_innodb_log_buffer_size=" (`expr $innodb_log_buffer_size / 1024`K)"
fi
_sort_buffer_size=""
if [ $sort_buffer_size -gt 1048576 ]; then
_sort_buffer_size=" (`expr $sort_buffer_size / 1024 / 1024`M)"
elif [ $sort_buffer_size -gt 1024 ]; then
_sort_buffer_size=" (`expr $sort_buffer_size / 1024`K)"
fi
_read_buffer_size=""
if [ $read_buffer_size -gt 1048576 ]; then
_read_buffer_size=" (`expr $read_buffer_size / 1024 / 1024`M)"
elif [ $read_buffer_size -gt 1024 ]; then
_read_buffer_size=" (`expr $read_buffer_size / 1024`K)"
fi
_read_rnd_buffer_size=""
if [ $read_rnd_buffer_size -gt 1048576 ]; then
_read_rnd_buffer_size=" (`expr $read_rnd_buffer_size / 1024 / 1024`M)"
elif [ $read_rnd_buffer_size -gt 1024 ]; then
_read_rnd_buffer_size=" (`expr $read_rnd_buffer_size / 1024`K)"
fi
_join_buffer_size=""
if [ $join_buffer_size -gt 1048576 ]; then
_join_buffer_size=" (`expr $join_buffer_size / 1024 / 1024`M)"
elif [ $join_buffer_size -gt 1024 ]; then
_join_buffer_size=" (`expr $join_buffer_size / 1024`K)"
fi
_thread_stack=""
if [ $thread_stack -gt 1048576 ]; then
_thread_stack=" (`expr $thread_stack / 1024 / 1024`M)"
elif [ $thread_stack -gt 1024 ]; then
_thread_stack=" (`expr $thread_stack / 1024`K)"
fi
_binlog_cache_size=""
if [ $binlog_cache_size -gt 1048576 ]; then
_binlog_cache_size=" (`expr $binlog_cache_size / 1024 / 1024`M)"
elif [ $binlog_cache_size -gt 1024 ]; then
_binlog_cache_size=" (`expr $binlog_cache_size / 1024`K)"
fi
echo -e "\tGlobal Buffers"
echo -e "\t--------------"
echo -e "\tkey_buffer_size...................: $key_buffer_size $_key_buffer_size"
echo -e "\tquery_cache_size..................: $query_cache_size $_query_cache_size"
echo -e "\ttmp_table_size....................: $tmp_table_size $_tmp_table_size"
echo -e "\tinnodb_buffer_pool_size...........: $innodb_buffer_pool_size $_innodb_buffer_pool_size"
echo -e "\tinnodb_additional_mem_pool_size...: $innodb_additional_mem_pool_size $_innodb_additional_mem_pool_size"
echo -e "\tinnodb_log_buffer_size............: $innodb_log_buffer_size $_innodb_log_buffer_size"
echo ""
echo -e "\tmax_connections...................: $max_connections"
echo ""
echo -e "\tPer Thread Buffers"
echo -e "\t------------------"
echo -e "\tsort_buffer_size..................: $sort_buffer_size $_sort_buffer_size"
echo -e "\tread_buffer_size..................: $read_buffer_size $_read_buffer_size"
echo -e "\tread_rnd_buffer_size..............: $read_rnd_buffer_size $_read_rnd_buffer_size"
echo -e "\tjoin_buffer_size..................: $join_buffer_size $_join_buffer_size"
echo -e "\tthread_stack......................: $thread_stack $_thread_stack"
echo -e "\tbinlog_cache_size.................: $binlog_cache_size $_binlog_cache_size"
declare -i max_memory_usage
max_memory_usage=`expr $key_buffer_size \
+ $query_cache_size \
+ $tmp_table_size \
+ $innodb_buffer_pool_size \
+ $innodb_additional_mem_pool_size \
+ $innodb_log_buffer_size \
+ $max_connections \* \( $sort_buffer_size \
+ $read_buffer_size \
+ $read_rnd_buffer_size \
+ $join_buffer_size \
+ $thread_stack \
+ $binlog_cache_size \)`
max_memory_usage_mb=`expr $max_memory_usage / 1024 / 1024`
#max_memory_usage_mb=`echo "scale=3; $max_memory_usage/1024/1024" | bc -l`
declare -i max_memory_allocated
max_memory_allocated=`expr $key_buffer_size \
+ $query_cache_size \
+ $tmp_table_size \
+ $innodb_buffer_pool_size \
+ $innodb_additional_mem_pool_size \
+ $innodb_log_buffer_size \
+ $max_used_connections \* \( $sort_buffer_size \
+ $read_buffer_size \
+ $read_rnd_buffer_size \
+ $join_buffer_size \
+ $thread_stack \
+ $binlog_cache_size \)`
max_memory_allocated_mb=`expr $max_memory_allocated / 1024 / 1024`
cur_memory_usage=`ps -ylC mysqld | grep mysqld | awk '{print$8}'`
cur_memory_usage_mb=`echo "scale=0; $cur_memory_usage/1024+1" | bc -l`
echo ""
echo ""
echo -e "\tSUM(Global Buffers) + max_connections * SUM(Per Thread Buffers)"
echo -e "\t==============================================================="
echo ""
if [ $max_memory_usage_mb -gt 1023 ]; then
max_memory_usage_gb=`echo "scale=3; $max_memory_usage_mb/1024" | bc -l`
echo -e "\tMax Memory Limit..................: $max_memory_usage_mb MB ($max_memory_usage_gb GB)"
else
echo -e "\tMax Memory Limit..................: $max_memory_usage_mb MB"
fi
echo ""
echo ""
echo "----- (Since last MySQL start)"
echo ""
echo -e "\tStart Time........................: $starttime"
echo -e "\tUptime............................: $uptime_string"
echo ""
echo -e "\tMax Used Connections..............: $max_used_connections"
if [ $max_memory_allocated_mb -gt 1023 ]; then
max_memory_allocated_gb=`echo "scale=3; $max_memory_allocated_mb/1024" | bc -l`
echo -e "\tMax Memory Allocated..............: $max_memory_allocated_mb MB ($max_memory_allocated_gb GB)"
else
echo -e "\tMax Memory Allocated..............: $max_memory_allocated_mb MB"
fi
echo ""
echo ""
echo "----- Current Status"
echo ""
echo -e "\tMySQL Version.....................: $mysql_version ($mysql_version_compile_machine)"
echo ""
if [ $cur_memory_usage_mb -gt 1023 ]; then
cur_memory_usage_gb=`echo "scale=3; $cur_memory_usage_mb/1024" | bc -l`
echo -e "\tCurrent Memory Usage..............: $cur_memory_usage_mb MB ($cur_memory_usage_gb GB)"
else
echo -e "\tCurrent Memory Usage..............: $cur_memory_usage_mb MB"
fi
echo -e "\tTreads Connected..................: $threads_connected"
echo ""
exit

8
mysql_memory_usage.sh Executable file
View File

@ -0,0 +1,8 @@
#!/bin/sh
echo ""
ps -ylC mysqld
echo ""
ps -ylC mysqld | awk '{x += $8;y += 1} END {print "MySQL Memory Usage (MB): "x/1024; print "Average Proccess Size (MB): "x/((y-1)*1024)}'
echo ""

139
optimize_mysql_tables-ND.sh Executable file
View File

@ -0,0 +1,139 @@
#!/usr/bin/env bash
LOGGING=false
mysql=`which mysql`
if [ -z "$mysql" ]; then
if [ -x "/usr/local/mysql/bin/mysql" ]; then
mysql=/usr/local/mysql/bin/mysql
fi
fi
# - MySQL / MariaDB credentials
# -
# - Giving password on command line is insecure an sind mysql 5.5
# - you will get a warning doing so.
# -
# - Reading username/password fro file ist also possible, using MySQL/MariaDB
# - commandline parameter '--defaults-file'.
# -
# - Since Version 5.6, that method is considered as insecure.
# - To avoid giving the password on command line, we use an
# - encrypted option file
# -
# - Create (encrypted) option file:
# - $ mysql_config_editor set --login-path=local --socket=/var/run/mysqld/mysqld.sock --user=backup --password
# - $ Password:
# -
# - Use of option file:
# - $ mysql --login-path=local ...
# -
# - Example
# - mysql_credential_args="--login-path=local"
# - mysql_credential_args="--defaults-file=/etc/mysql/debian.cnf" (Debian default)
# - mysql_credential_args="--defaults-file=/usr/local/mysql/sys-maint.cnf"
# -
mysql_credential_args="--login-path=local"
if [ -n "$mysql_credential_args" ] ; then
DATABASES=`$mysql $mysql_credential_args -N -s -e "show databases"`
else
DATABASES=`$mysql -u$mysql_user -p$mysql_password -N -s -e "show databases"`
fi
_service_extension=PRODUCTION.$$
for db in $DATABASES ; do
if [ "$db" = "nd" ]; then
mv /var/www/html/projekte/nd/htdocs /var/www/html/projekte/nd/htdocs.$_service_extension
ln -s htdocs503 /var/www/html/projekte/nd/htdocs
/usr/local/apache2/bin/apachectl graceful
elif [ "$db" = "nd_archiv" ]; then
mv /var/www/html/projekte/nd-archiv/htdocs /var/www/html/projekte/nd-archiv/htdocs.$_service_extension
ln -s htdocs503 /var/www/html/projekte/nd-archiv/htdocs
/usr/local/apache2/bin/apachectl graceful
fi
[ "$db" = "information_schema" ] && continue
[ "$db" = "performance_schema" ] && continue
[ "$db" = "mysql" ] && continue
if $LOGGING ;then
echo -e "\n[`date`] Optimize tables in database $db.."
fi
if [ -n "$mysql_credential_args" ] ; then
TABLES=`$mysql $mysql_credential_args $db -N -s -e "show tables"`
else
TABLES=`$mysql -u$mysql_user -p$mysql_password $db -N -s -e "show tables"`
fi
for table in $TABLES ; do
if [ -n "$mysql_credential_args" ] ; then
if $LOGGING ;then
$mysql $mysql_credential_args $db -N -s -e "OPTIMIZE TABLE \`$table\`"
else
$mysql $mysql_credential_args $db -N -s -e "OPTIMIZE TABLE \`$table\`" > /dev/null 2>&1
fi
else
$mysql -u$mysql_user -p$mysql_password $db -N -s -e "OPTIMIZE TABLE \`$table\`" > /dev/null 2>&1
fi
if [ "$?" != "0" ]; then
echo -e "\t[ Warning ]: Optimizing table \"${table}\" of database \"$db\" failed. Trying to repair.."
if [ -n "$mysql_credential_args" ] ; then
if $LOGGING ;then
$mysql $mysql_credential_args $db -N -s -e "REPAIR TABLE \`$table\`"
else
$mysql $mysql_credential_args $db -N -s -e "REPAIR TABLE \`$table\`" > /dev/null 2>&1
fi
if [ "$?" != "0" ]; then
echo -e "\t[ERROR]: Reparing table \"${table}\" of database \"$db\" failed !!"
else
if $LOGGING ;then
$mysql $mysql_credential_args $db -N -s -e "OPTIMIZE TABLE \`$table\`"
else
$mysql $mysql_credential_args $db -N -s -e "OPTIMIZE TABLE \`$table\`" > /dev/null 2>&1
fi
[[ $? -gt 0 ]] && echo -e "\t[ERROR]: Optimizing table \"${table}\" of database \"$db\" failed !!"
fi
else
if $LOGGING ;then
$mysql -u$mysql_user -p$mysql_password $db -N -s -e "REPAIR TABLE \`$table\`"
else
$mysql -u$mysql_user -p$mysql_password $db -N -s -e "REPAIR TABLE \`$table\`" > /dev/null 2>&1
fi
if [ "$?" != "0" ]; then
echo -e "\t[ERROR]: Reparing table \"${table}\" of database \"$db\" failed !!"
else
if $LOGGING ;then
$mysql -u$mysql_user -p$mysql_password $db -N -s -e "OPTIMIZE TABLE \`$table\`"
else
$mysql -u$mysql_user -p$mysql_password $db -N -s -e "OPTIMIZE TABLE \`$table\`" > /dev/null 2>&1
fi
[[ $? -gt 0 ]] && echo -e "\t[ERROR]: Optimizing table \"${table}\" of database \"$db\" failed !!"
fi
fi
fi
done
if $LOGGING ;then
echo -e "\n[`date`] End optimize tables in database $db.."
fi
if [ "$db" = "nd" ]; then
rm -f /var/www/html/projekte/nd/htdocs
mv /var/www/html/projekte/nd/htdocs.$_service_extension /var/www/html/projekte/nd/htdocs
/usr/local/apache2/bin/apachectl graceful
elif [ "$db" = "nd_archiv" ]; then
rm -f /var/www/html/projekte/nd-archiv/htdocs
mv /var/www/html/projekte/nd-archiv/htdocs.$_service_extension /var/www/html/projekte/nd-archiv/htdocs
/usr/local/apache2/bin/apachectl graceful
fi
done
exit 0

140
optimize_mysql_tables.sh Executable file
View File

@ -0,0 +1,140 @@
#!/usr/bin/env bash
# - Is this script running on terminal ?
# -
if [[ -t 1 ]] ; then
terminal=true
LOGGING=true
else
terminal=false
LOGGING=false
fi
mysql=`which mysql`
if [ -z "$mysql" ]; then
if [ -x "/usr/local/mysql/bin/mysql" ]; then
mysql=/usr/local/mysql/bin/mysql
fi
fi
# - MySQL / MariaDB credentials
# -
# - Giving password on command line is insecure an sind mysql 5.5
# - you will get a warning doing so.
# -
# - Reading username/password fro file ist also possible, using MySQL/MariaDB
# - commandline parameter '--defaults-file'.
# -
# - Since Version 5.6, that method is considered as insecure.
# - To avoid giving the password on command line, we use an
# - encrypted option file
# -
# - Create (encrypted) option file:
# - $ mysql_config_editor set --login-path=local --socket=/tmp/mysql.sock --user=root --password
# - $ Password:
# -
# - Use of option file:
# - $ mysql --login-path=local ...
# -
# - Example
# - mysql_credential_args="--login-path=local"
# - mysql_credential_args="--defaults-file=/etc/mysql/debian.cnf" (Debian default)
# - mysql_credential_args="--defaults-file=/usr/local/mysql/sys-maint.cnf"
# -
mysql_credential_args="--login-path=local"
if [ -n "$mysql_credential_args" ] ; then
DATABASES=`$mysql $mysql_credential_args -N -s -e "show databases"`
else
DATABASES=`$mysql -u$mysql_user -p$mysql_password -N -s -e "show databases"`
fi
if $terminal ; then
echo ""
echo "MySQL: optimize (and try to repair) tables of databases at host $(hostname -f)."
echo ""
fi
for db in $DATABASES ; do
[ "$db" = "information_schema" ] && continue
[ "$db" = "performance_schema" ] && continue
[ "$db" = "mysql" ] && continue
if $LOGGING ;then
echo -e "\n[`date`] Optimize tables in database $db.."
fi
if [ -n "$mysql_credential_args" ] ; then
TABLES=`$mysql $mysql_credential_args $db -N -s -e "show tables"`
else
TABLES=`$mysql -u$mysql_user -p$mysql_password $db -N -s -e "show tables"`
fi
for table in $TABLES ; do
if [ -n "$mysql_credential_args" ] ; then
if $LOGGING ;then
$mysql $mysql_credential_args $db -N -s -e "OPTIMIZE TABLE \`$table\`"
else
$mysql $mysql_credential_args $db -N -s -e "OPTIMIZE TABLE \`$table\`" > /dev/null 2>&1
fi
else
$mysql -u$mysql_user -p$mysql_password $db -N -s -e "OPTIMIZE TABLE \`$table\`" > /dev/null 2>&1
fi
if [ "$?" != "0" ]; then
echo -e "\t[ Warning ]: Optimizing table \"${table}\" of database \"$db\" failed. Trying to repair.."
if [ -n "$mysql_credential_args" ] ; then
if $LOGGING ;then
$mysql $mysql_credential_args $db -N -s -e "REPAIR TABLE \`$table\`"
else
$mysql $mysql_credential_args $db -N -s -e "REPAIR TABLE \`$table\`" > /dev/null 2>&1
fi
if [ "$?" != "0" ]; then
echo -e "\t[ERROR]: Reparing table \"${table}\" of database \"$db\" failed !!"
else
if $LOGGING ;then
$mysql $mysql_credential_args $db -N -s -e "OPTIMIZE TABLE \`$table\`"
else
$mysql $mysql_credential_args $db -N -s -e "OPTIMIZE TABLE \`$table\`" > /dev/null 2>&1
fi
[[ $? -gt 0 ]] && echo -e "\t[ERROR]: Optimizing table \"${table}\" of database \"$db\" failed !!"
fi
else
if $LOGGING ;then
$mysql -u$mysql_user -p$mysql_password $db -N -s -e "REPAIR TABLE \`$table\`"
else
$mysql -u$mysql_user -p$mysql_password $db -N -s -e "REPAIR TABLE \`$table\`" > /dev/null 2>&1
fi
if [ "$?" != "0" ]; then
echo -e "\t[ERROR]: Reparing table \"${table}\" of database \"$db\" failed !!"
else
if $LOGGING ;then
$mysql -u$mysql_user -p$mysql_password $db -N -s -e "OPTIMIZE TABLE \`$table\`"
else
$mysql -u$mysql_user -p$mysql_password $db -N -s -e "OPTIMIZE TABLE \`$table\`" > /dev/null 2>&1
fi
[[ $? -gt 0 ]] && echo -e "\t[ERROR]: Optimizing table \"${table}\" of database \"$db\" failed !!"
fi
fi
fi
done
if $LOGGING ;then
echo -e "\n[`date`] End optimize tables in database $db.."
fi
done
if $terminal ; then
echo ""
echo "Finished optimizing MySQL databases at host $(hostname -f)."
echo ""
fi
exit 0

1634
tuning-primer.sh Executable file

File diff suppressed because it is too large Load Diff