Sunday, November 06, 2011

IP Changing notification

Because sometimes the dyndns updater goes to hell, a little crontab script executed every couple of hours it's a life saver:

#!/bin/sh
#
# Send an email message to notify about the external IP.
#

#set a file where we can store the ip
logfile=/var/log/extip
#to whom we send the email
recipient="my_account@provider.tld"
#if we are using an external smtp relay
smtp=relay.smtp.provider.tld
export smtp=$smtp
#put a nice sender address on the email
sender="IP Checker <ipcheck@server.tld>"
#find the name of of this server
HOSTNAME=`hostname -a | cut -f1 -d" "`
# format a nice subject
subj="Change of IP on $HOSTNAME"
#now let's get the external ip
ip=`/usr/bin/lynx -dump 'http://whatismyip.org'`
#and create a message body
mesg="$HOSTNAME IP address on `date  +%a' '%b' '%e' '%H.%M.%S' '%Z' '%Y` is $ip"
#see if the ip has changed
oldip=`cat $logfile`
#or if the last message has been sent more than one week ago
age=`find $logfile -mtime +7`

if [ ! "$oldip" == "$ip" ] || [ ! "$age" == "" ]
then
 #keep the new ip into the logfile

 echo $ip &gt; $logfile
 #send email about it
 echo "$mesg" | mail -r"$sender" -s"$subj" "$recipient"
fi

exit 0


Of course, having an external web server, capable of running a little php script will allow us to replace http://whatismyip.org with our own webserver. The php script is really simple:


<?php
$ip = getenv('REMOTE_ADDR');
#$ip = $_SERVER['REMOTE_ADDR'];
echo '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
' . $ip . '
</body>
</html>';
?>

Sunday, October 16, 2011

Sunday, August 07, 2011

AUTOTBACK 3.0

A new version of the backup utility. 
This is an evolution of the old AUTOTBACK script.
If the script is called without any command-line parameters it will check if "dialog" is installed on the system and it will start the "semi-graphical" interface, otherwise it will display the "usage parameters" and it will exit.

 The command line parameters are still the same:
$autotback ACTION DIRECTORY ARCHIVE MAIL PRINTER, where
- ACTION is backup or restore
- DIRECTORY represent, in case of ACTION=backup the source (what we will backup), or the destination (where the backup will be restored) when ACTION=restore. The default value is "/".
- ARCHIVE represent the location of backup. It is usually the tape device (by default is /dev/st0), but it can also be a simple file (/mnt/disk/archive_name.cpio - if we use an external drive mounted on /mnt/disk)
 - MAIL represent the email address(es) that will receive the backup report. If more than one email address is required, then, on the command line, those must be surrounded by double quotes ("aa@bb.c dd@ee.f gg@hh.i"). The default emails are "backupreport@xxxxxxxx.com root@localhost"
-PRINTER represents the name of the printer used for the report. The default value is empty. The printer MUST be already installed on the system!

#!/bin/sh
#       @(#) AUTOTBACK 3.0 18/07/11
#
#    30/07/2001 (sorin@xxxxxxxx.com) create dialog driven interface
#    18/07/2011 (sorin@xxxxxxxx.com) add the restore section, add command-line parmeters
#    10/08/2010 (sorin@xxxxxxxx.com) autoremove lock after 8hrs and reenable backup. remove hit report
#    28/08/2008 (sorin@xxxxxxxx.com) modified for rhel5. skip /proc /sys. send mail with the result
#    modified 9/14/2000 to provide hit reporting on web site
#
#       Copyright (C) 1991-2011  xxxxxxxx Inc.
#                               Montreal, Quebec, Canada.
#               All Rights Reserved.
#
#       Usage  $0 backup /source /dev/destination email@notification.address (printer)
#              $0 restore /destination /dev/source email@notification.address (printer)
#        when restore is called, the script  will ask you to select a partial restore
#        by typing a file mask. Just type Enter to do a full restore.
#        if no destination is provided, the restore is performed on /
#               All the reports and temp files are in /var/autotback directory
#
#

# If not provided on the commandline, set default Variables
unset SRC DES REC PRN
if [ ! "$2" = "" ]; then SRC=$2; else SRC=/; fi
if [ ! "$3" = "" ]; then DES=$3; else DES=/dev/st0; fi
if [ ! "$4" = "" ]; then REC=$4; else REC="backupreport@xxxxxxxx.com root@localhost"; fi
if [ ! "$5" = "" ]; then PRN=$5; fi

# System variables
DEFAULTDIR=/var/autotback
LOCK=$DEFAULTDIR/backlock
REPORTFILE=$DEFAULTDIR/backrep$$
TEMPFILE=$DEFAULTDIR/backtmp$$
TEMPFILE2=$DEFAULTDIR/backtemp$$
TEMPFILEV=$DEFAULTDIR/vertmp$$
TEMPFILEV2=$DEFAULTDIR/vertemp$$
LIST=$DEFAULTDIR/list$$
HEADER="Backup Report for $HOSTNAME
(c)1991-2011 Lauserco Services Inc.
================================================================================
`date`
"

# verify if we have the necessary binary
for bins in cpio grep cat rm date tail mkdir
 do
    if [ "$(whereis $bins | cut -d: -f2)" = "" ]; then echo "Fatal ERROR: $bins not found. Exit!"; exit 3; fi
done




# Define main functions

## If there were no commandline parameters start the dialog-driven menu
void ()
{
# Check if another job is running
if [ -r $LOCK ]
then
    # if we have wall, announce the trouble
    if [ ! "$(whereis wall | cut -d: -f2)" = "" ]; then wall " We have a little issue with the BACKUP ! Please verify $DEFAULTDIR"; fi
    echo "`date` - Another job is running or the last backup did not finished well. Please check $DEFAULTDIR !" >> $REPORTFILE
    # check the age of the lockfile
    ((DIFF=`date +%s`-`cat $LOCK`))
    if [ $DIFF -gt 28800 ]; then echo "`date` - Lockfile more than 8 hours old, autocleaning... the backup will run!" >> $REPORTFILE; rm -f $LOCK; fi
    if [ -r $LOCK ]
    then
        if [ ! "$(whereis mail | cut -d: -f2)" = "" ]; then cat $REPORTFILE | mail -s "Restore Failure on $HOSTNAME" $REC 2>/dev/null; fi
    if [ "$PRN" = "" ]; then echo "Skip print, no printer defined" >> $REPORTFILE; elif [ ! "$(whereis lp |cut -d: -f2)" = "" ]; then lp -d $PRN $REPORTFILE; fi
    exit 5
    fi
fi

# Cleanup obsolete traces
rm -rf $DEFAULTDIR 2> /dev/null
mkdir -p $DEFAULTDIR 2> /dev/null
if [ ! -d $DEFAULTDIR ] ; then echo "CAN'T MAKE $DEFAULTDIR"; exit 2; fi
touch $REPORTFILE $TEMPFILE $TEMPFILE2 $TEMPFILEV $TEMPFILEV2 $LIST

# Create lockfile
echo "`date +%s`">$LOCK
# check binary existance
for bins in whoami crontab head cp touch sed
 do
    if [ "$(whereis $bins | cut -d: -f2)" = "" ]; then echo "Fatal ERROR: $bins not found. Can't run dialog interface. Exit!"; exit 4; fi
done

# make the background blue if it is possible
if [ ! "$(whereis setterm | cut -d: -f2)" = "" ]; then setterm -clear all -foreground black -background blue ; fi

# present the user the choice of backup, restore or scheduler
dialog --title "AUTOTBACK 3.0" --backtitle "A dialog driven backup utility  (press [ESC] twice to exit)" --nocancel \
--menu "Choose an Option" 11 30 9 1 "Create a Backup" 2 "Restore from Backup" 3 "Schedule a Backup" 2>$DEFAULTDIR/input.$$
sel=$?
choice=`cat $DEFAULTDIR/input.$$`
rm -f $DEFAULTDIR/input.$$
case $sel in
    0)
    if [ "$choice" = "1" ]; then
    #Choose what to backup
    dialog --title "AUTOTBACK 3.0  (SPACE to select what to save)" --backtitle "A dialog driven backup utility  (press [ESC] twice to exit)" --nocancel \
    --dselect $SRC 12 46 2>$DEFAULTDIR/input.$$
        sel=$?
        SRC=`cat $DEFAULTDIR/input.$$`
        rm -f $DEFAULTDIR/input.$$
        case $sel in
        0);;
        255) echo "[ESC] Pressed, exiting..." && exit 1;;
        esac
        #Choose where to backup
    dialog --title "AUTOTBACK 3.0  (SPACE to select where to save)" --backtitle "A dialog driven backup utility  (press [ESC] twice to exit)" --nocancel \
    --fselect $DES 12 46 2>$DEFAULTDIR/input.$$
        sel=$?
        DES=`cat $DEFAULTDIR/input.$$`
        rm -f $DEFAULTDIR/input.$$
        case $sel in
        0);;
        255) echo "[ESC] Pressed, exiting..." && exit 1;;
        esac
        #Choose to whom to send notification email
    dialog --title "AUTOTBACK 3.0" --backtitle "A dialog driven backup utility  (press [ESC] twice to exit)" --nocancel \
    --inputbox "Type the notification emails" 8 47 "$REC" 2>$DEFAULTDIR/input.$$
        sel=$?
        REC=`cat $DEFAULTDIR/input.$$`
        rm -f $DEFAULTDIR/input.$$
        REC=\"$REC\"
        case $sel in
        0);;
        255) echo "[ESC] Pressed, exiting..." &&  exit 1;;
        esac
        # Choose where to print (if we print)
    dialog --title "AUTOTBACK 3.0" --backtitle "A dialog driven backup utility  (press [ESC] twice to exit)" --nocancel \
    --inputbox "Type the Printer name" 8 25 2>$DEFAULTDIR/input.$$
        sel=$?
        PRN=`cat $DEFAULTDIR/input.$$`
        rm -f $DEFAULTDIR/input.$$
        case $sel in
        0);;
        255) echo "[ESC] Pressed, exiting..." && exit 1;;
        esac
    dialog --title "AUTOTBACK 3.0" --backtitle "A dialog driven backup utility  (press [ESC] twice to exit)" --nocancel \
        --infobox "Performing the Backup procedure. Please wait..." 6 25
        # call the backup function with the right command line options
        backup

    fi
   
    if [ $choice = 2 ]; then
        #Choose where to restore (destination)
    dialog --title "AUTOTBACK 3.0  (SPACE to select where to save)" --backtitle "A dialog driven backup utility  (press [ESC] twice to exit)" --nocancel \
    --dselect "/" 12 47 2>$DEFAULTDIR/input.$$
        sel=$?
        SRC=`cat $DEFAULTDIR/input.$$`
        rm -f $DEFAULTDIR/input.$$
        case $sel in
        0);;
        255) echo "[ESC] Pressed, exiting..." && exit 1;;
        esac
        #Choose where to restore from (archive location)
    dialog --title "AUTOTBACK 3.0  (SPACE to select the source)" --backtitle "A dialog driven backup utility  (press [ESC] twice to exit)" --nocancel \
    --fselect "/dev/st0" 12 46 2>$DEFAULTDIR/input.$$
        sel=$?
        DES=`cat $DEFAULTDIR/input.$$`
        rm -f $DEFAULTDIR/input.$$
        case $sel in
        0);;
        255) echo "[ESC] Pressed, exiting..." && exit 1;;
        esac
        #Choose to whom to send notification email
    dialog --title "AUTOTBACK 3.0" --backtitle "A dialog driven backup utility  (press [ESC] twice to exit)" --nocancel \
    --inputbox "Type the notification emails" 8 47 "$REC" 2>$DEFAULTDIR/input.$$
        sel=$?
        REC=`cat $DEFAULTDIR/input.$$`
        rm -f $DEFAULTDIR/input.$$
        REC=\"$REC\"
        case $sel in
        0);;
        255) echo "[ESC] Pressed, exiting..." &&  exit 1;;
        esac
        # Choose where to print (if we print)
    dialog --title "AUTOTBACK 3.0" --backtitle "A dialog driven backup utility  (press [ESC] twice to exit)" --nocancel \
    --inputbox "Type the Printer name" 8 25 2>$DEFAULTDIR/input.$$
        sel=$?
        PRN=`cat $DEFAULTDIR/input.$$`
        rm -f $DEFAULTDIR/input.$$
        case $sel in
        0);;
        255) echo "[ESC] Pressed, exiting..." && exit 1;;
        esac
    dialog --title "AUTOTBACK 3.0" --backtitle "A dialog driven backup utility  (press [ESC] twice to exit)" --nocancel \
        --infobox "Performing the Restore procedure. Please wait..." 6 25
        # call the restore function with the right command line options
        restore
    fi

    if [ $choice = 3 ]; then
    ##we need to clean the name of the script if it was called with full path or locally as ./$name
    myname=`echo $0 | sed 's/\.\///g'`
        shortname=`echo $myname | sed 's/.*\///g'`
    ##before schedule a backup check is there is another one alredy programmed
    if [ ! `crontab -l | grep -c $myname` = 0 ]; then
    dialog --title "AUTOTBACK 3.0" --backtitle "A dialog driven backup utility  (press [ESC] twice to exit)" \
    --yes-label "Keep" --no-label "Delete" --yesno "A backup is alredy scheduled, What do you want to do with it? \n `crontab -l | grep $myname`" 8 72
    sel=$?
        case $sel in
        0) # keep the old scheduled backups in crontab
        ;;
        1)
        # delete old crontab
        cp -f /var/spool/cron/`whoami` $DEFAULTDIR/cron.bak
    cat /var/spool/cron/`whoami` | grep -v $shortname > $DEFAULTDIR/`whoami`; mv -f $DEFAULTDIR/`whoami` /var/spool/cron/`whoami`
        ;;
        255) echo "[ESC] Pressed, exiting..." && exit 1;;
        esac
    fi

    ##start to schedule. choose days of week (MON-FRI alredy selected)
        dialog --title "AUTOTBACK 3.0" --backtitle "A dialog driven backup utility  (press [ESC] twice to exit)" --nocancel \
        --checklist "Choose days of Backup (Use SPACE to select)" 15 26 13 0 Sunday off 1 Monday on 2 Tuesday on 3 Wednesday on 4 Thursday on 5 Friday on 6 Saturday off --and-widget 2>$DEFAULTDIR/input.$$
        sel=$?
        sed -i -e 's/\"//g' -e 's/\ /\,/g' $DEFAULTDIR/input.$$
        week=`cat $DEFAULTDIR/input.$$`
        rm -f $DEFAULTDIR/input.$$
        case $sel in
        0);;
        255) echo "[ESC] Pressed, exiting..." && exit 1;;
        esac
    ##choose time of backup (23:30 seems to be a good default choice)
        dialog --title "AUTOTBACK 3.0" --backtitle "A dialog driven backup utility  (press [ESC] twice to exit)" --nocancel \
        --time-format %M\ %H --timebox "Choose time for Backup (Use arrows to modify)" 5 26 23 30 00 2>$DEFAULTDIR/input.$$
        sel=$?
        time=`cat $DEFAULTDIR/input.$$`
        rm -f $DEFAULTDIR/input.$$
        case $sel in
        0);;
        255) echo "[ESC] Pressed, exiting..." && exit 1;;
        esac

    #Choose what to backup
    dialog --title "AUTOTBACK 3.0  (SPACE to select what to save)" --backtitle "A dialog driven backup utility  (press [ESC] twice to exit)" --nocancel \
    --dselect "/" 12 46 2>$DEFAULTDIR/input.$$
        sel=$?
        SRC=`cat $DEFAULTDIR/input.$$`
        rm -f $DEFAULTDIR/input.$$
        case $sel in
        0);;
        255) echo "[ESC] Pressed, exiting..." && exit 1;;
        esac
        #Choose where to backup
    dialog --title "AUTOTBACK 3.0  (SPACE to select what to save)" --backtitle "A dialog driven backup utility  (press [ESC] twice to exit)" --nocancel \
    --fselect "/dev/st0" 12 46 2>$DEFAULTDIR/input.$$
        sel=$?
        DES=`cat $DEFAULTDIR/input.$$`
        rm -f $DEFAULTDIR/input.$$
        case $sel in
        0);;
        255) echo "[ESC] Pressed, exiting..." && exit 1;;
        esac
        #Choose to whom to send notification email
    dialog --title "AUTOTBACK 3.0" --backtitle "A dialog driven backup utility  (press [ESC] twice to exit)" --nocancel \
    --inputbox "Type the notification emails" 8 47 "$REC" 2>$DEFAULTDIR/input.$$
        sel=$?
        REC=`cat $DEFAULTDIR/input.$$`
        rm -f $DEFAULTDIR/input.$$
        REC=\"$REC\"
        case $sel in
        0);;
        255) echo "[ESC] Pressed, exiting..." &&  exit 1;;
        esac
        # Choose where to print (if we print)
    dialog --title "AUTOTBACK 3.0" --backtitle "A dialog driven backup utility  (press [ESC] twice to exit)" --nocancel \
    --inputbox "Type the Printer name" 8 25 2>$DEFAULTDIR/input.$$
        sel=$?
        PRN=`cat $DEFAULTDIR/input.$$`
        rm -f $DEFAULTDIR/input.$$
        case $sel in
        0);;
        255) echo "[ESC] Pressed, exiting..." && exit 1;;
        esac

    #insert the line in crontab
        echo "$time * * $week $myname backup $SRC $DES $REC $PRN" >> /var/spool/cron/`whoami`
    dialog --title "AUTOTBACK 3.0" --backtitle "A dialog driven backup utility  (press [ESC] twice to exit)" --nocancel \
        --infobox "New crontab is installed...\n `crontab -l | grep $shortname`" 10 45

    fi
    ;;
    255) echo "[ESC] Pressed, exiting..." && exit 1;;
esac
#restore normal colors
if [ ! "$(whereis setterm | cut -d: -f2)" = "" ]; then setterm -default ; fi
sleep 1
# remove lock
rm $LOCK

}



# Define the backup function
backup ()
{
# Check if another backup is running
if [ -r $LOCK ]
then
    # if we have wall, announce the trouble
    if [ ! "$(whereis wall | cut -d: -f2)" = "" ]; then wall " We have a little issue with the BACKUP ! Please verify $DEFAULTDIR"; fi
    echo "$HEADER Another backup is running or the last backup has not finished well. Please check $DEFAULTDIR !" >> $REPORTFILE
    # check the age of the lockfile
    ((DIFF=`date +%s`-`cat $LOCK`))
    if [ $DIFF -gt 28800 ]; then echo "`date` - Lockfile more than 8 hours old, autocleaning... the backup will run!" >> $REPORTFILE; rm -f $LOCK; fi
    if [ -r $LOCK ]
    then
    if [ ! "$(whereis mail | cut -d: -f2)" = "" ]; then cat $REPORTFILE | mail -s "Backup Failure on $HOSTNAME" $REC 2>/dev/null; fi
    if [ "$PRN" = "" ]; then echo "Skip print, no printer defined" >> $REPORTFILE; elif [ ! "$(whereis lp |cut -d: -f2)" = "" ]; then lp -d $PRN $REPORTFILE; fi
    exit 5
    fi
fi

# Cleanup obsolete traces
rm -rf $DEFAULTDIR 2> /dev/null
mkdir -p $DEFAULTDIR 2> /dev/null
if [ ! -d $DEFAULTDIR ] ; then echo "CAN'T MAKE $DEFAULTDIR"; exit 2; fi
touch $REPORTFILE $TEMPFILE $TEMPFILE2 $TEMPFILEV $TEMPFILEV2 $LIST

# Create lockfile
echo "`date +%s`">$LOCK

# Start backup
cd $SRC

##

echo "$HEADER The command line was: $0 backup $SRC $DES $REC $PRN
Backup of $SRC started on `date`
Enumerating the list of files and removing special folders
 ">>$REPORTFILE

# Exclude /proc /sys
find . -depth -print | grep -v "./sys/" | grep -v "./proc/" >$LIST

# Start saving
echo "Start WRITING to $DES on `date`">>$REPORTFILE
cat $LIST | cpio -ocvB >$DES 2>>$TEMPFILE
backstat=$?
echo "
Following are the last lines recorded during saving:" >>$REPORTFILE
tail $TEMPFILE | head -9 >>$REPORTFILE
echo "
WRITE exit status = $backstat
">>$REPORTFILE
if [ $backstat -ne 0 ]
then
    echo "*** Backup may contain an ERROR ***">>$REPORTFILE
fi
echo "Tape WRITING completed on `date`">>$REPORTFILE
set `tail -1 $TEMPFILE`
BACKTOT=$1
echo "
Total of amount of Backup blocks was $BACKTOT
">>$REPORTFILE

#Tape rewind / Verify
echo "Starting VERIFY on $DES `date`">>$REPORTFILE
cpio -icvtB <$DES >>$TEMPFILEV 2>>$TEMPFILEV2
verstat=$?
echo "
Following are the last lines recorded during verify:" >>$REPORTFILE
tail $TEMPFILEV | head -9 >>$REPORTFILE
tail $TEMPFILEV2 | head -9 >>$REPORTFILE
echo "VERIFY exit status = $verstat
">>$REPORTFILE
if [ $verstat -ne 0 ]
then
    echo "***  Verify may contain an ERROR ***
    ">>$REPORTFILE
fi
echo "Verify completed for $HOSTNAME on `date`
">>$REPORTFILE
set `tail -1 $TEMPFILEV2`
VERTOT=$1
echo "Total of amount of verified blocks was $VERTOT
">>$REPORTFILE

if [[ $backstat -ne 0 || ($verstat -ne 0 || "$BACKTOT" != "$VERTOT") ]]
then
    # put a warning
    echo "*** WARNING!!! BACKUP TRANSFER TOTALS DID NOT MATCH ***
***                 PLEASE VERIFY                   ***
">>$REPORTFILE
    # Send the report
    if [ ! "$(whereis mail | cut -d: -f2)" = "" ]; then cat $REPORTFILE | mail -s "Backup ERROR on $HOSTNAME" $REC 2>/dev/null; fi
else
    # Gives the OK
    echo "*** Transfer Totals Concur ***
">>$REPORTFILE
    # Send the report
    if [ ! "$(whereis mail | cut -d: -f2)" = "" ]; then cat $REPORTFILE | mail -s "Backup OK on $HOSTNAME" $REC 2>/dev/null; fi

fi
# Print the report
if [ "$PRN" = "" ]; then echo "Skip print, no printer defined" >> $REPORTFILE; elif [ ! "$(whereis lp |cut -d: -f2)" = "" ]; then lp -d $PRN $REPORTFILE; fi

# Eject if mt is installed
if [ ! "$(whereis mt |cut -d: -f2)" = "" ]; then mt -f $DES rewoffl; fi
sleep 1
# remove lock
rm $LOCK

##

}



# Define the restore function
restore ()
{
# Check if another job is running
if [ -r $LOCK ]
then
    # if we have wall, announce the trouble
    if [ ! "$(whereis wall | cut -d: -f2)" = "" ]; then wall " We have a little issue with the BACKUP ! Please verify $DEFAULTDIR"; fi
    echo "`date` - Another job is running or the last backup did not finished well. Please check $DEFAULTDIR !" >> $REPORTFILE
    # check the age of the lockfile
    ((DIFF=`date +%s`-`cat $LOCK`))
    if [ $DIFF -gt 28800 ]; then echo "`date` - Lockfile more than 8 hours old, autocleaning... the backup will run!" >> $REPORTFILE; rm -f $LOCK; fi
    if [ -r $LOCK ]
    then
        if [ ! "$(whereis mail | cut -d: -f2)" = "" ]; then cat $REPORTFILE | mail -s "Restore Failure on $HOSTNAME" $REC 2>/dev/null; fi
    if [ "$PRN" = "" ]; then echo "Skip print, no printer defined" >> $REPORTFILE; elif [ ! "$(whereis lp |cut -d: -f2)" = "" ]; then lp -d $PRN $REPORTFILE; fi
    exit 5
    fi
fi

# Cleanup obsolete traces
rm -rf $DEFAULTDIR 2> /dev/null
mkdir -p $DEFAULTDIR 2> /dev/null
if [ ! -d $DEFAULTDIR ] ; then echo "CAN'T MAKE $DEFAULTDIR"; exit 2; fi
touch $REPORTFILE $TEMPFILE $TEMPFILE2 $TEMPFILEV $TEMPFILEV2 $LIST

# Create lockfile
echo "`date +%s`">$LOCK

# Start restore
echo "$HEADER The command line was: $0 restore $SRC $DES $REC $PRN
Restore started on `date`
Choosing the files to restore...">>$REPORTFILE

#choose the files to restore
echo "Type the name of the files you want to restore (CR = all the archive)
(ex: to restore everything from /etc/sysconfig type /etc/sysconfi*):"
read RES

echo "Restoring $RES from $DES to $SRC." >> $REPORTFILE
mkdir -p $SRC 2>/dev/null
cd $SRC

cpio -icvdmB $RES < $DES >>$TEMPFILE 2>>$TEMPFILE2

# Prepare the report
echo "Following are the special cases reported during restore:" >>$REPORTFILE
tail $TEMPFILE | head -9 >> $REPORTFILE
tail $TEMPFILE2 | head -9 >> $REPORTFILE
set `tail -1 $TEMPFILE2`
RESTOT=$1
echo "Total of amount of blocks was $RESTOT
">>$REPORTFILE
# Send the report
if [ ! "$(whereis mail | cut -d: -f2)" = "" ]; then cat $REPORTFILE | mail -s "Restore report for $HOSTNAME" $REC 2>/dev/null; fi
# Print the report
if [ "$PRN" = "" ]; then echo "Skip print, no printer defined">> $REPORTFILE; elif [ ! "$(whereis lp |cut -d: -f2)" = "" ]; then lp -d $PRN $REPORTFILE; fi

# Eject if mt is installed
if [ ! "$(whereis mt |cut -d: -f2)" = "" ]; then mt -f $DES rewoffl; fi
sleep 1
# remove lock
rm $LOCK

}




# Starting the script. If it was called without parameters check if dialog exist and call the interface


case "$1" in
backup)
    backup;;
restore)
    restore;;
*)
    if [ "$(whereis dialog | cut -d: -f2)" = "" ]; then
     echo "Usage: $0 backup (source) (destination) (email) (printer)
       $0 restore (destination) (source) (email) (printer)
       "
    else
    void
    fi
    ;;
esac
exit 0

Monday, July 11, 2011

rotate kde cube using the accelerometer

ain't that fun? tilt the laptop and the cube rotates :) all thanks to the ST LIS3LV02DL Accelerometer
#!/bin/bash
# kde-cube-rotate
#

while true ; do
# vertical axis
#POS1=`cat /sys/devices/platform/lis3lv02d/position | awk -F , '{print$2}'`
#sleep .1
#POS2=`cat /sys/devices/platform/lis3lv02d/position | awk -F , '{print$2}'`
#horizontal axis
POS1=`cat /sys/devices/platform/lis3lv02d/position | awk -F , '{print$1}' | awk -F "(" '{print$2}'`
#sleep .1
POS2=`cat /sys/devices/platform/lis3lv02d/position | awk -F , '{print$1}' | awk -F "(" '{print$2}'`

DIS=$( wmctrl -d | grep "*" | awk '{sub(/x[0-9]+/, "", $1); print $1}'| tail -n1)
((POS=$POS1-$POS2))

#echo "pos1=$POS1 pos2=$POS2 pos=$POS display=$DIS"

if [ $POS -ge 20 ]; then
wmctrl -s $(( ${DIS} - 1 )) 2>/dev/null
sleep 1

elif [ $POS -le -20 ]; then
wmctrl -s $(( ${DIS} + 1 )) 2>/dev/null
sleep 1

fi
done

Saturday, May 28, 2011

/etc/udev/rules.d/80-automount.rules

KERNEL=="sd[a-z]", NAME="%k", SYMLINK+="usb%m", GROUP="users", OPTIONS="last_rule"
ACTION=="add", KERNEL=="sd[a-z][0-9]", SYMLINK+="usb%n", GROUP="users", NAME="%k"
ACTION=="add", KERNEL=="sd[a-z][0-9]", RUN+="/bin/mkdir -p /mnt/usb%n"
ACTION=="add", KERNEL=="sd[a-z][0-9]", PROGRAM=="/lib/udev/vol_id -t %N", RESULT=="vfat", RUN+="/bin/mount -t vfat -o rw,noauto,flush,quiet,nodev,nosuid,noexec,noatime,dmask=000,fmask=111 /dev/%k /mnt/usb%n", OPTIONS="last_rule"
ACTION=="add", KERNEL=="sd[a-z][0-9]", PROGRAM=="/lib/udev/vol_id -t %N", RESULT=="ntfs", RUN+="/bin/mount -t ntfs-3g -o rw,noauto,flush,quiet,nodev,nosuid,noexec,noatime,dmask=000,fmask=111 /dev/%k /mnt/usb%n", OPTIONS="last_rule"
ACTION=="add", KERNEL=="sd[a-z][0-9]", RUN+="/bin/mount -t auto -o rw,noauto,sync,dirsync,noexec,nodev,noatime /dev/%k /mnt/usb %n", OPTIONS="last_rule"
ACTION=="remove", KERNEL=="sd[a-z][0-9]", RUN+="/bin/umount -l /mnt/usb%n"
ACTION=="remove", KERNEL=="sd[a-z][0-9]", RUN+="/bin/rmdir /mnt/usb%n", OPTIONS="last_rule"

Sunday, April 24, 2011

VPN to Sonicwall's Virtual Office

#!/bin/bash
# 2011/04/22 sorin@XXXXXXXX.com
# A script to start the Sonicwall netExtender and create the necessary routes
# Sonicwall's netExtender binary is available from the VirtualOffice web interface.
#
# Define variables. In the config file the data format is TAB separated as follow:
# username password domainname ExternalIP port
#testusr tespassword LocalDomain 123.45.67.89 443
#
# Alternatively, you can define $user $pass $dom $ip $port here

prog="/usr/sbin/netExtender"
conf="/etc/sysconfig/gvpn.conf"
log="/dev/null"
if [ ! -f $prog ]; then
echo "No binary - fatal!"
exit 2
fi
if [ ! -f $conf ]; then
echo "No config, trying to set some defaults"
user=testusr
pass=testpassword
dom=LocalDomain
ip=123.45.67.89
port=443
fi

# Let's see how this script has been called
case "$1" in
start)
# if we have a conf file read the data from there
if [ -f $conf ]; then val=`sed '/^ *#/d;s/#.*//' $conf`; read user pass dom ip port < <( echo $val ); fi echo -e "Starting $prog..." $prog -u $user -p $pass -d $dom $ip:$port & >$log 2>&1
sleep 3
# after a few seconds we can try to add the route, but first \
# if we're in the same IP range, die gracefully, otherwise add the route
routenet=`sudo ifconfig | grep P-t-P | cut -d: -f2 | cut -d. -f1-3` >$log 2>&1
grp=`sudo ifconfig | grep -v P-t-P | grep '$routenet.255'` >$log 2>&1
if [ -z "$grp" ]; then
# mac users does not have "dev" in route's options, so let's add the route via a gateway
# routedev=`sudo ifconfig | grep ppp | cut -dL -f1` >$log 2>&1
# sudo route add -net $routenet netmask 255.255.255.0 dev $routedev >$log 2>&1
newip=`sudo ifconfig | grep P-t-P | cut -d: -f2 | cut -d" " -f1` >$log 2>&1
sudo route add -net $routenet.0 netmask 255.255.255.0 gw $newip >$log 2>&1
else
echo "We are in the same IP range, can't assign a route to the remote network!"
$0 stop
exit 1
fi
;;
stop)
echo -e "Stopping $prog..."
# first let's bring down the ppp interface
routedev=`sudo ifconfig | grep ppp | cut -dL -f1` >$log 2>&1
sudo ifconfig $routedev down >$log 2>&1
# then ask the pogram to terminate
sudo killall $prog >$log 2>&1
sleep 5
# if netExtender is still alive after 5 sec, force kill
if [ `ps xau | grep -v grep | grep $prog | awk '{print $2}' | wc -l` -ne "0" ]; then
sudo killall -9 $prog >$log 2>&1
fi
;;
status)
if [ `ps xau | grep -v grep | grep $prog | awk '{print $2}' | wc -l` -ne "0" ]; then
echo "$prog running with pid(s) `ps xau | grep -v grep | grep $prog | awk '{print $2}' | tr '
' ', '`"
echo "Showing route via ppp devices..." ; sudo route | grep ppp | grep -v 255.255.255.255
else
echo "$prog NOT running"
fi
;;
restart)
$0 stop
sleep 3
$0 start
;;
*)
printf "Usage: %s\n" "$(basename $0) {start|stop|status|restart}"
exit 1
esac
exit 0

Tuesday, April 19, 2011

VT6421 IDE RAID Controller "hotswap"

#!/bin/bash
case "$1" in
start)
echo "Starting sata-via module..."
modprobe sata_via
echo "sleep 5 sec..."
sleep 5
lsmod | if grep sata_via; then echo -e "sata-via module \e[01;32mIS\e[00m inserted"; else echo -e "sata-via module \e[01;31mNOT\e[00m loaded"; fi
echo -e "Module inserted. HDD \e[01;32mavailable\e[00m"
cat /proc/partitions
;;
stop)
echo "Removing sata-via module..."
rmmod sata_via 2>/dev/null
echo "sleep 5 sec..."
sleep 5
lsmod | if grep sata_via; then echo -e "sata-via module \e[01;32mIS\e[00m inserted"; else echo -e "sata-via module \e[01;31mNOT\e[00m loaded"; fi
echo -e "Module removed. \e[01;32mSafe\e[00m to extract HDD."
cat /proc/partitions
;;
status)
lsmod | if grep sata_via; then echo -e "sata-via module \e[01;32mIS\e[00m inserted"; else echo -e "sata-via module \e[01;31mNOT\e[00m loaded"; fi
;;
restart|reload)
$0 stop
$0 start
;;
*)
printf $"Usage: %s {start|stop|restart|status}\n"
exit 1
esac