Tags » Shell Script

Small Shell Script Tricks

This is something I often use in Shell scripts.

pipecmd.sh – Shell script as pipe command

#!/bin/sh
while true
do
    read LINE1
    [ $? -ne 0 ] && break
#   change something, here we just do copying
    LINE2=$LINE1
#   change something, here we just do copying
    echo $LINE2
done
… 120 more words
Systems Software

Replicate Oracle Standby DB Over Data Domain

Or how to synchronize local and remote standby database using two simple Shell scripts.

Standard Oracle database production configuration consists of one primary database and one local physical standby database, both RAC or standalone and both in same datacenter. 1,330 more words

Systems Software

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
Shell Script

Linux Shell Script

Find

-- Find corrupted zip file
find . -name "*.zip" -exec unzip -t "{}" \;  > rrrr.txt
find . -name "*.zip" -exec echo "{}" \;
{} means current file

-- Find file and limit the result
find 10587727/ -name "*.pdf" | head -2T

-- Find empty directory
find . 37 more words
Linux

align text in center bash

align text in center shell script linux

text=abc; printf "%*s\n" $(((${#text}+$COLUMNS)/2)) "$text"
Shell Script

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 '.*(){2}:{2}|.*(){2}:{2} {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\e[m $LAST_ERROR_LOG" | tee -a /var/log/$( basename $0 ).log
fi 8 more words
Shell Script