Import Remote MySQL Database

#!/bin/bash
# Import Remote MySQL Database

### LOCAL MYSQL INFO ### 
LOCAL_MYSQL_USER='root'
LOCAL_MYSQL_PASSWD='localMysqlPasswordHere'

### REMOTE MYSQL INFO ###
REMOTE_MYSQL_HOST='192.168.0.7'
REMOTE_MYSQL_USER='root'
REMOTE_MYSQL_PASSWD='remoteMysqlPasswordHere'
REMOTE_DATABASES_TO_EXPORT=(dbName1Here dbName2Here)

echo -e "\n[ $( date '+%d/%m/%Y %R' ) ] START SCRIPT" | tee -a /var/log/$( basename $0 ).log

for i in ${REMOTE_DATABASES_TO_EXPORT[*]}; do

  echo -e "\n[ $( date '+%d/%m/%Y %R' ) ] EXPORT REMOTE DB $i" | tee -a /var/log/$( basename $0 ).log
  mysqldump -h $REMOTE_MYSQL_HOST -u $REMOTE_MYSQL_USER -p$REMOTE_MYSQL_PASSWD --databases $i --single-transaction > '/tmp/'$i'.sql'

  echo "START IMPORT $i [ $( date '+%d/%m/%Y %R' ) ]" | tee -a /var/log/$( basename $0 ).log
  mysql -u $LOCAL_MYSQL_USER -p$LOCAL_MYSQL_PASSWD -e "DROP DATABASE $i"
  mysql -u $LOCAL_MYSQL_USER -p$LOCAL_MYSQL_PASSWD < '/tmp/'$i'.sql'
  echo "END IMPORT $i [ $( date '+%d/%m/%Y %H:%M' ) ]" | tee -a /var/log/$( basename $0 ).log

  # Delete dump file
  rm -f '/tmp/'$i'.sql'
done

echo "[ $( date '+%d/%m/%Y %R' ) ] END SCRIPT" | tee -a /var/log/$( basename $0 ).log
Advertisements

search for apache error in the last x minutes

#!/bin/bash
# Search for apache error in the last X minutes

MINUTES=30
DATA=$( date '+%d/%m/%Y %R' )
LAST_ERROR_LOG=$( grep 'erro' /var/log/apache2/error.log | tail -n 1 )
CUT_TIME_ERRO_LOG=$( echo $LAST_ERROR_LOG | egrep -o '.*([0-9]){2}:[0-9]{2}|.*([0-9]){2}:[0-9]{2} [0-9]{4}' | tr -d [ )
UNIX_TIMESTAMP_CUT_TIME_ERROR_LOG=$( date -d "$CUT_TIME_ERRO_LOG" +%s )

if [ ${#LAST_ERROR_LOG} -gt 0 ] && [ $(( `date +%s` - $UNIX_TIMESTAMP_CUT_TIME_ERROR_LOG )) -lt $(( $MINUTES*60 )) ]; then

 echo -e "[$DATA] \e[31;1m[ ERROR FOUND ]\e[m $LAST_ERROR_LOG" | tee -a /var/log/$( basename $0 ).log
fi

cut time in kernel or apache log

cut last time error in kernel log

awk '/erro/ {print $1,$2,$3}' /var/log/messages | tail -1

cut last time error in apache log

awk '/erro/ {print $1,$2,$3,$4,$5}' /var/log/httpd/error_log | tail -1 | tr -d []
#!/bin/bash
# cut last time error in both kernel and apache log

for i in /var/log/messages /var/log/httpd/error_log; do
  grep 'erro' $i | tail -1 | egrep -o '.*([0-9]){2}:[0-9]{2}|.*([0-9]){2}:[0-9]{2} [0-9]{4}' | tr -d [
done

check file size linux and send mail

#!/bin/bash
# Send mail if files exceed maximum allowed size

FILES=(/etc/hosts /etc/passwd)
MAXIMUM_FILE_SIZE_BYTES=1024
EMAIL=mail@domain.com

for i in ${FILES[*]}; do

  if [ $( ls -l $i | awk '{print $5}' ) -gt $MAXIMUM_FILE_SIZE_BYTES ]; then

    echo -e "[ $( date '+%F %R' ) ] [ \e[31;1mERRO\e[m ] $i higher than allowed: $( ls -l $i | awk '{print $5}' )" bytes | tee -a /var/log/$( basename $0 ).log
    echo | mail -s "$( hostname -s ) $i higher than allowed" $EMAIL
  fi
done