Register
It is currently Wed Jul 30, 2014 1:02 am

Clean up script


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Mon Apr 27, 2009 2:25 pm   

Joined: Mon Apr 27, 2009 2:24 pm
Posts: 10
Hello, beginner bash scripter here.. I was able to write a script and it works just fine. I'm just wondering if someone could chime in or contribute any suggestions to make it cleaner or tighter so to speak. I have a disk to disk backup solution which uses 250GB disks. When one gets full I just pop in a new one. the backup disk is also shared via NFS automount.



#!/bin/bash
#Unexport all nfs directories
echo unexporting nfs directories!
exportfs -au
#check to make sure unexport was successful!
exportfs -v
#umount BACKUPS
umount -v /BACKUPS
#replace the old drive with a new one
echo "Do you wish to continue? (y/n)"
read ans

case $ans in
Y|y) ;;
[Yy][Ee][Ss]) ;;
N|n) exit ;;
[Nn][Oo]) exit ;;
*) echo "Invalid command"
esac
#partiton and create new filesystem on the drive
# run fdisk to make sure we are using /dev/sdc
echo "RUNNING FDISK TO SEE IF /DEV/SDC IS THE CORRECT DRIVE!"
fdisk -l

echo "Are you ready to run fdisk? (y/n)"
read ans

case $ans in
Y|y) ;;
[Yy][Ee][Ss]) ;;
N|n) exit ;;
[Nn][Oo]) exit ;;
*) echo "Invalid command"
esac
echo "ABOUT TO CREATE THE NEW PARTITION!!!!!!!!!!!!!"
fdisk /dev/sdc < ~/250GB_format.txt

#check to see if the OS automounted the disk
echo #CHECKING TO SEE IF FEDORA AUTOMOUNTED THE DRIVE!!!!!!!!!!!!!!!!"
umount -v /dev/sdc1

#create the new filesystem
echo "ABOUT TO CREATE THE FILESYSTEM!!!!!!!!!!!!!!!!!!!"
mkfs.ext3 /dev/sdc1

#mount the new file system
echo "MOUNT THE FILESYSTEM!!!!!!!!"
mount -v /dev/sdc1 /BACKUPS


#run df to see that the filesystem is mounted an reporting the correct size
df -h

echo "Is the disk mounted and showing the correct partition size? (y/n)"
read ans

case $ans in
Y|y) ;;
[Yy][Ee][Ss]) ;;
N|n) exit ;;
[Nn][Oo]) exit ;;
*) echo "Invalid command"
esac
#get the UUID to add to fstab
echo "GRAB THE BLOCKID"
blkid /dev/sdc1

#export the filesystems
echo "EXPORTING /BACKUPS"
exportfs -av


Top
 Profile  
 PostPosted: Mon Apr 27, 2009 11:35 pm   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
I don't have time at the moment to write fixes for everything. But you cut down on your yes/no answer thingie by using a function.

Code:
function ask_yn {
   message=$1
   read -p "$message" answer
   case $answer in
      [Yy]|[Yy][Ee][Ss]) ;;
      [Nn]|[Nn][Oo]) exit ;;
      *) ask_yn "$message" ;;
   esac
}


This will also loop if the answer is not matched.
To use it just write "ask_yn" and your message within quotes (to keep it from falling into $2 $3 and so on).

Best regards
Fredrik Eriksson


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP