Script para optimizar las tablas fragmentadas en MySQL

Bueno otro buen script a la huchaca… esta escrito en bash aparte de ser utilisisimo porque chequea las tablas fragmentadas y tambien chequea todas las bbdds en busca de tablas MyISAM o INNODB y las optimiza.

#!/bin/bash

VERSION="0.7.2"
log="$PWD/mysql_error_log.txt"

echo "MySQL fragmentation finder (and fixer) v$VERSION, written by Phil Dufault ( http://www.dufault.info/ )"

showHelp() {
echo -e "\tThis script only repairs MyISAM and InnoDB tables"
echo -e "\t--help or -h\t\tthis menu"
echo -e "\t--user username\tspecify mysql username to use\n\t\t\tusing this flag means the script will ask for a password during runtime, unless you supply..."
echo -e "\t--password \"yourpassword\""
echo -e "\t--host hostname\tspecify mysql hostname to use, be it local (default) or remote"
}

#s parse arguments
while [[ $1 == -* ]]; do
case "$1" in
--help|-h) showHelp; exit 0;;
--user) mysqlUser="$2"; shift 2;;
--password) mysqlPass="$2"; shift 2;;
--host) mysqlHost="$2"; shift 2;;
--) shift; break;;
esac
done

# prevent overwriting the commandline args with the ones in .my.cnf, and check that .my.cnf exists
if [[ ! $mysqlUser && -f "$HOME/.my.cnf" ]]; then
if grep "user=" "$HOME/.my.cnf" >/dev/null 2>&1; then
if grep "pass=" "$HOME/.my.cnf" >/dev/null 2>&1; then
mysqlUser=$(grep user= < "$HOME/.my.cnf" | awk -F\" '{print $2}'); mysqlPass=$(grep pass= < "$HOME/.my.cnf" | awk -F\" '{print $2}'); if grep "host=" "$HOME/.my.cnf" >/dev/null 2>&1; then
mysqlHost=$(grep host= < "$HOME/.my.cnf" | awk -F\" '{print $2}'); fi else echo "Found no pass line in your .my.cnf,, fix this or specify with --password" fi else echo "Found no user line in your .my.cnf, fix this or specify with --user" exit 1; fi fi # set localhost if no host is set anywhere else if [[ ! $mysqlHost ]]; then mysqlHost="127.0.0.1" fi # error out if [[ ! $mysqlUser ]]; then echo "Authentication information not found as arguments, nor in $HOME/.my.cnf" echo showHelp exit 1 fi if [[ ! $mysqlPass ]]; then echo -n "Enter your MySQL password: " read -s mysqlPass fi # Test connecting to the database: mysql -u"$mysqlUser" -p"$mysqlPass" -h"$mysqlHost" --skip-column-names --batch -e "show status" >/dev/null 2>&1
if [[ $? -gt 0 ]]; then
echo "An error occured, check $log for more information.";
exit 1;
fi

# Retrieve the listing of databases:
databases=( $(mysql -u"$mysqlUser" -p"$mysqlPass" -h"$mysqlHost" --skip-column-names --batch -e "show databases;" 2>"$log") );
if [[ $? -gt 0 ]]; then
echo "An error occured, check $log for more information."
exit 1;
fi

echo -e "Found ${#databases[@]} databases";
for i in ${databases[@]}; do
# get a list of all of the tables, grep for MyISAM or InnoDB, and then sort out the fragmented tables with awk
fragmented=( $(mysql -u"$mysqlUser" -p"$mysqlPass" -h"$mysqlHost" --skip-column-names --batch -e "SHOW TABLE STATUS FROM $i;" 2>"$log" | awk '{print $1,$2,$10}' | egrep "MyISAM|InnoDB" | awk '$3 > 0' | awk '{print $1}') );
if [[ $? -gt 0 ]]; then
echo "An error occured, check $log for more information."
exit 1;
fi
tput sc
echo -n "Checking $i ... ";
if [[ ${#fragmented[@]} -gt 0 ]]; then
if [[ ${#fragmented[@]} -gt 0 ]]; then
if [[ ${#fragmented[@]} -gt 1 ]]; then
echo "found ${#fragmented[@]} fragmented tables."
else
echo "found ${#fragmented[@]} fragmented table."
fi
fi
for table in ${fragmented[@]}; do
let fraggedTables=$fraggedTables+1;
echo -ne "\tOptimizing $table ... ";
mysql -u"$mysqlUser" -p"$mysqlPass" -h"$mysqlHost" -D "$i" --skip-column-names --batch -e "optimize table $table" 2>"$log" >/dev/null
if [[ $? -gt 0 ]]; then
echo "An error occured, check $log for more information."
exit 1;
fi
echo done
done
else
tput rc
tput el
fi
unset fragmented
done

# footer message
if [[ ! $fraggedTables -gt 0 ]]; then
echo "No tables were fragmented, so no optimizing was done.";
else
if [[ $fraggedTables -gt 1 ]]; then
echo "$fraggedTables tables were fragmented, and were optimized.";
else
echo "$fraggedTables table was fragmented, and was optimized.";
fi
fi

if [[ ! -s $log ]]; then
rm -f "$log"
fi

unset fraggedTables

Link | http://www.dufault.info/blog/a-script-to-optimize-fragmented-tables-in-mysql/

Script en bash para cambiar el puerto de Apache en todos los dominios en Plesk

Si quereis cambiar el puerto en todos los vhost`s en Plesk, tendreis que cambiar el numero 80 (puerto Apache default de Plesk) dentro del archivo httpd.include de cada vhosts, y si teneis muchos vhosts en el servidor o servidores… como no tengais este pequeño script en bash, os podeis morir en el intento

#!/bin/bash
MY_FILEPATH="/var/www/vhosts"
MY_FILE="httpd.include"
MY_REPLACE=":81"
find $MY_FILEPATH  -name $MY_FILE -exec perl -p -i -e "s[:80][$MY_REPLACE]g" {} \;
service httpd restart"

El Archivo Zip de la muerte – 42.zip

42.zip es un archivo zip de 42.374 bytes, contiene 16 archivos zips que a su vez contienen otros 16 zips y así hasta 6 veces. Los 16 últimos zips contienen un archivo de 4,3 Gb cada uno. Si descomprimimos el archivo 42.zip de 42.374 bytes obtendriamos 4.5 Pb de datos descomprimidos.

  • 16 x 4294967295 = 68.719.476.720 (68GB)
  • 16 x 68719476720 = 1.099.511.627.520 (1TB)
  • 16 x 1099511627520 = 17.592.186.040.320 (17TB)
  • 16 x 17592186040320 = 281.474.976.645.120 (281TB)
  • 16 x 281474976645120 = 4.503.599.626.321.920 (4,5PB)

Este tipo de archivos se denominan Zips de la muerte o Zips bomba y suelen usarse como herramienta para ataques DoS.

PD:  hay que ver como una cosa tan pequeña te puede joder el servidor enterito 😀 , así que pequeños sysadmins tener muy definidas las reglas de cuotas de disco con sus respectivas alertas a vuestro email 😀
raiola manda y no el panda

Funciones a deshabilitar en php.ini – Tips para securizar php

Y es que, cada vez se ven mas destrozos a causa de las webs mal hechas (o poco actualizadas) en php . Que si postnuke, que si phpnuke, phpbb, mybb y un larguisimo etc. Ya que esto es un dolor de cabeza para cualquier sysadmin, algo a tener en cuenta es el deshabilitar ciertas funciones de php en el php.ini, y el que las quiera.. que las habilite en el .htaccess de su virtualhost bajo su responsabilidad, no? Empezamos:

Editamos fichero de configuración de PHP php.ini

#vi /etc/php.ini

buscamos la linea que contiene disable_functions. Posiblemente tenga un ; delante, que sirve para inutilizarla. Eliminamos el ; y dejamos la linea de esta forma:

disable_functions = system, exec, popen, passthru, shell_exec, proc_close, proc_get_status, proc_nice, proc_open, proc_terminate

Asi, nos cargamos unas cuantas funciones peligrosas que a mas de uno le han dejado sin dormir.

Por otro lado, es de esperar el estar atentos a cosas como el safe_mode, allow_url_fopen y demas… pero creo que eso ya lo puse en otro post anterior 🙂