rSync cron script with error handling.

Posted in Bash Scripts on February 22nd, 2011 by admin – Be the first to comment

This is a simple rSync script I wrote in bash, has error handling and e-mail functionality, used primarily for automated/cron jobs.

#!/bin/sh
# Objective:  Handles rSync
# Author Russ Thompson @ viGeek.net
# This script assumes you're using password-less login (via keys).  User/PASS can be added easily via opts.

# Log & Email parms...
LOGF="$(pwd)/rsync-results.log"
ETO"whatever@email.com"
ESUB="$(hostname -s) - rSync - FAILURE"
EFILE="tmpemail.txt"

# Make initial log entry...
echo "rSyncd script kicked off at $(date) by $(whoami)" >> $LOGF

# Remote host machine name (supports multiple entries)
REMOTE_SERVERS="69.69.69.69"

# Directory to copy from on the source machine.
SRCDIR="/path/to/source/"

# Directory to copy to on the destination machine.
DESTDIR="/path/to/destination"

# Path to SSH
SSH=/usr/bin/ssh

# Does copy, but still gives a verbose display of what it is doing (CHANGE THESE)
OPTS="-rptgvv --remove-sent-files --delete-after"

# This deletes rsync-results.log after xyz (to prevent buildup)
find $LOGF -mtime +30 -exec rm {} \;
# Can also be deleted via size paramaters

function trap_clean {
    # Error handling...
    echo -e "$(hostname) caught error on line $LINENO at $(date +%l:%M%p) via script $(basename $0)" | tee -a $EFILE $LOGF
    echo -e "Please see the tail end of $LOGF for additional error details...">> $EFILE
    /bin/mail -s "$ESUB" "$EADDY" < $EFILE
    # Cleanup our temp e-mail file
    rm -f $EFILE
}

# Defined trap conditions
trap trap_clean ERR SIGHUP SIGINT SIGTERM

# Determine that files exist
if ls $SRCDIR ; then
  for REMOTE in $REMOTE_SERVERS
  do
      VAR=`ping -s 1 -c 1 $REMOTE > /dev/null; echo $?`
      if [ $VAR -eq 0 ]; then
      echo "rsync $OPTS $REMOTE::$SRCDIR $DESTDIR"
      rsync -e "$SSH" $OPTS $SRCDIR $REMOTE::$DESTDIR 2>> $LOGF
  else
      echo "Cannot connect to $REMOTE." >> $LOGF
      fi
  done
else
  echo "rSync script did not run : no files present" >> $LOGF
fi

# Fill log with line for easy reading...
echo "--------------------------------------------------------------------------------------------------------------" >> $LOGF

mySQL Backup Script.

Posted in Bash Scripts on February 22nd, 2011 by admin – Be the first to comment

The following script will backup all of the specified databases, create a report and throw them in a tar.gz. Script features error handling, e-mail notifications and easily customizable. I wrote this several years ago, so it’s probably due for an update soon…

#!/bin/sh
## MySQL Makeover Backup Script v1.0
## Authored By Russ Thompson 2008 @ viGeek.net

# Establish destination directory
DESTDIR="/pwd/whatever"

# Define log file ensure writable
LOGFILE="/var/log/dbbackup.log"

# Establish output file name
FONAME="db_$(hostname)_$(date +%m-%d-%Y).tar.gz"

# Establish Databases To Backup
# For multiple databases seprate via space
DBACKUP="db1 db2"

# mySQL Database Credentials (can store in users my.cnf)
# Only user with 'read only' ensure this file script chmoded correctly
# Better security... use shcrypt to secure or keyless entry.
DUSER="username"
DPASS="password"

# Define e-mail attributes for error handling messages
EADDY="email@whatever.com"
ESUB="mySQL Backup Error on $(hostname)"

# Seperation
echo -e "--------------------------------------------------------------------------------------------------------" >> $LOGFILE

# Pregame check if we are the active  (for clustered DRBD systems)
# Simply checks to ensure we are running.
if [ -e /var/lib/mysql/mysql.sock ]
  then
     echo -e "$(hostname) is active beginning backup procedure..." >> $LOGFILE
  else
     echo -e "$(hostname) is not active, aborting..." >> $LOGFILE
     exit 1
fi

# Kickoff
echo -e "$(date +%D) $(basename $0) has started at: $(date +%l:%M%p)" >> $LOGFILE

# Define Error Handling.
function clean_up {
        #Perform actions on errors.
        EMESSAGE="tmpout.txt"
        echo -e "$(hostname) caught error on line $LINENO at $(date +%l:%M%p) via script $(basename $0)" >> $EMESSAGE
        /bin/mail -s "$ESUB" "$EADDY" < $EMESSAGE
        echo -e "Caught error on $LINENO at $(date +%l:%M%p)" >> $LOGFILE
        rm -f $EMESSAGE
        exit 1
}
# Define our trap conditions
trap clean_up ERR SIGHUP SIGINT SIGTERM

# Change into working directory
cd $DESTDIR

echo -e "Changed into directory $(pwd)" >> $LOGFILE

# Before we continue ensure we're in the right place

if echo $(pwd)| grep $DESTDIR >> /dev/null
        then
        echo -e "Good" >> /dev/null
else
        echo -e "ERROR: expecting directory $DESTDIR however in $(pwd)" >> $LOGFILE
        clean_up
fi

# Remove the previous backup
rm -f *.gz

# Begin creating SQL backup files.
for i in $DBACKUP
        do
        mysqldump --user $DUSER --password=$DPASS $i > $i.sql
        echo -e "Created mySQL dump for database $i" >> $LOGFILE
done

# Create simple report to include in backup
REPNAME="backup-report.txt"
echo -e "Database backups expecting the following files:" > $REPNAME
echo -e "$DBACKUP" >> $REPNAME
echo -e "\nGetting the following files:" >> $REPNAME
echo -e "$(ls)" >> $REPNAME
echo -e "The following files are getting backed up:" >> $LOGFILE
echo -e "$(ls)" >> $LOGFILE
# Create our GNU Zip
tar -czvf $FONAME *
echo -e "Created backup file with a size of $(du -h $FONAME)" >> $LOGFILE

# Cleanup the mess.
for i in $DBACKUP
        do
        rm -f $i.sql
done

# Finish
echo -e "$(basename $0) has finished at: $(date +%l:%M%p)" >> $LOGFILE

SSH Root Logon Notification Script.

Posted in Bash Scripts on February 22nd, 2011 by admin – Be the first to comment

This is a very simple script I wrote several years ago, a client wanted to be notified when a user logged in and switched to root. They also wanted some other basic information included, this was the quick solution I put together.

Login to your server and switch to root.

nano /root/.bashexec

Paste the following code in this file.

#!/bin/bash
tmpout=tmpout.txt
stringz="$(tail -n 1 /var/log/secure | grep root | grep opened | sed 's/.*by\(.*\)(.*/\1/' | awk '{print $1""$2}')"
echo -e "The local root account has been accessed by user $stringz" > $tmpout
echo -e "\nThe system last updated on: $(sed -n '/Updated:/h;${;g;p;}' < /var/log/yum.log | cut -dU -f1)" >> $tmpout
echo -e "\nThe last five users to access the system (including active):" >> $tmpout
echo -e "$(last -n 5 | sed '/^wtmp/d')" >> $tmpout
echo -e "\nUptime Report: $(uptime)" >> $tmpout
/bin/mail -s "$(hostname) root account accesssed by $stringz" youremail@whatever.com < $tmpout
rm -f $tmpout

Now execute it via .bashrc

nano /root/.bashrc

Paste the following.

sh /root/.bashexec

Trying to install an already registered mbean.

Posted in Uncategorized on February 22nd, 2011 by admin – 2 Comments

Another JAVA error here, another simple resolution. In this case two of our JAVA developers were trying to deploy compiled code to run withing JBoss, however were seeing the following error.

ERROR [org.jboss.deployment.MainDeployer] Could not create deployment:
file:/usr/local/EnterprisePlatform-4.3.0.GA/jboss-as/server/production/deploy/epjndi-ds.xml

org.jboss.deployment.DeploymentException:
Trying to install an already registered mbean: jboss.jca:service=LocalTxCM,name=jdbc/epjndi

The error lead us to believe (obviously) that we had two configurations conflicting with one another and this was the case. This was easily found by searching the root path of our JBoss deployment, via…

find . | xargs grep 'epjndi'

The return:

./apps/storefront/WEB-INF/epjndi-ds.xml:  <jndi-name>jdbc/epjndi</jndi-name>
./epjndi-ds.xml:  <jndi-name>jdbc/epjndi</jndi-name>

We moved the epjndi-ds.xml from WEB-INF and walla, issue resolved, hope this helps anyone seeing this issue :)

Problem in init java.io.EOFException: Unexpected end of ZLIB input stream

Posted in Uncategorized on February 22nd, 2011 by admin – Be the first to comment

Just spent an hour or so working with several JAVA developers trying to resolve this issue.

Our sever log was showing errors consistent with the following:

ERROR [URLDeploymentScanner] Incomplete Deployment listing
Problem in init java.io.EOFException: Unexpected end of ZLIB input stream

Initially believing this issue is caused by our deployed JAVA code, however after some tinkering on my end it was determined the issue is related to JBoss tmp/work files, so if you come across this issue it can be solved by delete (or preferably moving) any files contained withing the tmp and work directories contained withing the JBoss directory/deployment.

Cannot assign requested address errors.

Posted in Uncategorized on February 22nd, 2011 by admin – Be the first to comment

Recently spent sometime working on a few RHEL/CentOS servers, after a reboot each system would come up in an unresponsive state, after some KVM sessions something was preventing these systems from statically assigning their respective IP addresses as per the config(s), the logs indicated the following problem.

SIOCSIFBROADCAST: Cannot assign requested address SIOCSIFBRDADDR: Cannot assign requested address SIOCSIFFLAGS: Cannot assign requested address

I’ve ran into this error a couple of times and each time the problem was different as was the resolution. Luckily the problem was quickly identifiable, having dug through ‘ps’ a bit, I noticed the wpa_supplicant daemon running which is essentially a wireless component of the NetworkManager package, these are all packages you’ll typically find on a desktop system. It appeared as though these packages were essentially holding our static IP address hostage and not allowing us to bind it to our interface (eth0). Luckily solving this problem was quick and easy.

via YUM: First let’s check to see if it’s installed.

yum list | grep wpa_supplicant

via YUM: remove wpa_supplicant (Will also remove NetworkManager package).

yum remove wpa_supplicant

via RPM: First let’s check to see if it’s installed.

rpm -qi wpa_supplicant

via RPM: remove wpa_supplicant

rpm -e wpa_supplicant

Now try to bringup your interface.

ifup eth#