Wednesday, 11 June 2014

Bash Exercises - 10

Bash Exercises - 10

Script 46: Bulk user creation |

This script is used to create number of users at a time.
 #!/bin/bash  
 HOSTS="/tmp/test/serverlist"  
 DDID="IDENTIFY"  
 HOMEPATH="/home/$UNAME"  
 for i in `cat $HOSTS` ;  
 do  
 UNIQUE=`awk -F " " '{print $1 }' $DDID`  
 RUID=`ssh $i 'grep "$UNIQUE" /etc/passwd'`  
 if [[ -ne "$RUID" ]]  
     then  
         echo "User ID is currently available on $i, ready to add new user"  
         UNAME=`awk -F " " '{print $2 }' $DDID`  
         PASSWORD=`awk -F " " '{print $3 }' $DDID`  
         ROLE=`awk -F " " '{print $4 }' $DDID`  
         `ssh $i useradd -u "$UNIQUE" -d "$HOMEPATH" -s /bin/bash -c "$ROLE" -m -k /etc/skel/ "$UNAME"`  
         `ssh $i echo "$PASSWORD" | passwd --stdin "$UNAME"`  
 else  
         echo "User ID exist on $i, check new ID"  
 fi  
 done  

Script 47: Disk usage |
This script will be useful to analyze the disk usage and if the reported disk space is more than 90 % an email will be sent to the administrator.

 #!/bin/sh  
 # set -x  
 # Shell script to monitor or watch the disk space  
 # It will send an email to $ADMIN, if the (free available) percentage of space is # >= 90%.  
 # -------------------------------------------------------------------------  
 # Set admin email so that you can get email.  
 ADMIN="root"  
 # set alert level 90% is default  
 ALERT=90  
 # Exclude list of unwanted monitoring, if several partions then use "|" to  
 # separate the partitions.  
 # An example: EXCLUDE_LIST="/dev/hdd1|/dev/hdc5"  
 EXCLUDE_LIST="/auto/ripper"  
 #  
 #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::  
 #  
 function main_prog() {  
 while read output;  
 do  
 #echo $output  
  usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1)  
  partition=$(echo $output | awk '{print $2}')  
  if [ $usep -ge $ALERT ] ; then  
    echo "Running out of space \"$partition ($usep%)\" on server $(hostname), $(date)" | \  
    mail -s "Alert: Almost out of disk space $usep%" $ADMIN  
  fi  
 done  
 }  
 if [ "$EXCLUDE_LIST" != "" ] ; then  
  df -H | grep -vE "^Filesystem|tmpfs|cdrom|${EXCLUDE_LIST}" | awk '{print $5 " " $6}' | main_prog  
 fi  
Script 48: Incremental Backup Scripts |
This script will do the incremental backup into an external mounted hard-drive. It is to take a backup of the /home directory. However, it can be modified to suit the requirements.

 #!/bin/bash  
 # ----------------------------------------------------------------------  
 # mikes handy rotating-filesystem-snapshot utility  
 # ----------------------------------------------------------------------  
 # this needs to be a lot more general, but the basic idea is it makes  
 # rotating backup-snapshots of /home whenever called  
 # ----------------------------------------------------------------------  
 unset PATH # suggestion from H. Milz: avoid accidental use of $PATH  
 # ------------- system commands used by this script --------------------  
 ID=/usr/bin/id;  
 ECHO=/bin/echo;  
 MOUNT=/bin/mount;  
 RM=/bin/rm;  
 MV=/bin/mv;  
 CP=/bin/cp;  
 TOUCH=/bin/touch;  
 RSYNC=/usr/bin/rsync;  
 # ------------- file locations -----------------------------------------  
 MOUNT_DEVICE=/dev/hdb1;  
 SNAPSHOT_RW=/root/snapshot;  
 EXCLUDES=/usr/local/etc/backup_exclude;  
 # ------------- the script itself --------------------------------------  
 # make sure we're running as root  
 if (( `$ID -u` != 0 )); then { $ECHO "Sorry, must be root. Exiting..."; exit; } fi  
 # attempt to remount the RW mount point as RW; else abort  
 $MOUNT -o remount,rw $MOUNT_DEVICE $SNAPSHOT_RW ;  
 if (( $? )); then  
 {  
   $ECHO "snapshot: could not remount $SNAPSHOT_RW readwrite";  
   exit;  
 }  
 fi;  
 # rotating snapshots of /home (fixme: this should be more general)  
 # step 1: delete the oldest snapshot, if it exists:  
 if [ -d $SNAPSHOT_RW/home/hourly.3 ] ; then     \  
 $RM -rf $SNAPSHOT_RW/home/hourly.3 ;        \  
 fi ;  
 # step 2: shift the middle snapshots(s) back by one, if they exist  
 if [ -d $SNAPSHOT_RW/home/hourly.2 ] ; then     \  
 $MV $SNAPSHOT_RW/home/hourly.2 $SNAPSHOT_RW/home/hourly.3 ; \  
 fi;  
 if [ -d $SNAPSHOT_RW/home/hourly.1 ] ; then     \  
 $MV $SNAPSHOT_RW/home/hourly.1 $SNAPSHOT_RW/home/hourly.2 ; \  
 fi;  
 # step 3: make a hard-link-only (except for dirs) copy of the latest  
 # snapshot, if that exists  
 if [ -d $SNAPSHOT_RW/home/hourly.0 ] ; then     \  
 $CP -al $SNAPSHOT_RW/home/hourly.0 $SNAPSHOT_RW/home/hourly.1 ; \  
 fi;  
 # step 4: rsync from the system into the latest snapshot (notice that  
 # rsync behaves like cp --remove-destination by default, so the   
 # destination is unlinked first. If it were not so, this would copy over   
 # the other snapshot(s) too!  
 $RSYNC               \  
   -va --delete --delete-excluded       \  
   --exclude-from="$EXCLUDES"       \  
   /home/ $SNAPSHOT_RW/home/hourly.0 ;  
 # step 5: update the mtime of hourly.0 to reflect the snapshot time  
 $TOUCH $SNAPSHOT_RW/home/hourly.0 ;  
 # and thats it for home.  
 # now remount the RW snapshot mountpoint as readonly  
 $MOUNT -o remount,ro $MOUNT_DEVICE $SNAPSHOT_RW ;  
 if (( $? )); then  
 {  
   $ECHO "snapshot: could not remount $SNAPSHOT_RW readonly";  
   exit;  
 } fi;  

No comments :

Post a Comment