#!/bin/bash
version="1.0.0813"
echo " *** nas_quotas made easy version $version - by me *** "
echo
USAGE () # Description des arguments - affichage lors de l'utilisation de listes
{
echo -e " --------------------------------------------------------------------------------------------------------------\n Usage : $ ./easyquota.bash [--add | -a] 'input_file' [--quota | -q] 'quota' [--filesystem | -fs] 'filesystem'\n\n Adds 'input_file' list to 'filesystem' with specified 'quota'\n 'input_file' must have only one name per line. No special characters is allowed\n 'quota' is the lower limit in MB\n 'filesystem' is the filesystem where the quota must be set : could be [IDrives_Adm | A] , [IDrives_Fac | F] , [store_ata_1 | S1] , [store_ata_2 | S2] , [store_ata_3 | S3] , [Promo_1_3 | P13] , [Promo_2_4 | P24] , [Test | T]\n\n if 'filesystem' is not specified , the program will ask for it\n if 'quota' is not specified, the program will ask for it\n folders are created in CAPITAL case\n if no option is specified, first argument is considered as 'input_file'\n"
}
TEST_ARGS () # teste les arguments de la ligne de commande
{
if [ -f "$1" ]; then list=$1; fi
while [ $# -gt 0 ]; do
case "$1" in
"-a"|"-add"|"--add")
list=$2
;;
"-q"|"-quota"|"--quota")
allquota=$2
;;
"-fs"|"-FS"|"--filesystem")
fs=$2
;;
esac
shift
done
# Test if file exists
if [ -f "$list" ]
then echo " $list exists : Ok"
num=`cat $list | wc | awk {'print $1'}`
echo
echo " Processing $num shares"
else echo " file $list does not exist"; USAGE; exit 2
fi
# Test if quota is specified AND is an integer
if [ "$allquota" = "" ]
then ASKQUOTA
else echo $allquota | grep [^0-9] > /dev/null 2>&1
if [ "$?" -eq "0" ]
then echo "'quota' $allquota is not valid"
ASKQUOTA
fi
fi
# Test if filesystem is specified
menufs=$fs
if [ "$menufs" = "" ]; then echo "Please select filesystem :"
FS_MENU
else TEST_FS
fi
}
FILE_TEST () # testing file 'liste' : 1 name per line, no special character
{
error1=0
error2=0
numline=0
if [ -f /tmp/err ]; then rm /tmp/err; fi
if [ -f /tmp/err2 ]; then rm /tmp/err2; fi
echo "file is $list"
echo "testing $list for special characters"
badchar=`grep [^a-Z0-9_+-] $list`
while [ "$numline" != "$num" ]; do
numline=$(($numline+1))
charline=`head -n $numline $list | tail -n 1`
numword=`echo $charline | wc | awk {'print $2'}`
badname=`echo $charline | grep [^a-Z0-9_+-]`
if [ "$numword" != "1" ]
then echo "Error in file \"$list\" : $numword names found on line $numline"
echo "$numline" >> /tmp/err
error1=$(($error1+1))
fi
if [ "$badname" != "" ]
then echo "Invalid character found in file \"$list\" on line $numline : $badname"
echo "$numline" >> /tmp/err2
error2=$(($error2+1))
else echo "line $numline Ok"
fi
done
DISPLAY_ERRORS
}
DISPLAY_ERRORS () # affiche les erreurs par ligne du fichier 'liste'
{
echo
if [ "$error1" != "0" ]; then
echo "Error1 : $error1 line found empty or with multiple names"
echo "Press Enter to show errors"
read
for i in `cat /tmp/err`; do
echo "line $i : `cat $list | head -n $i |tail -n 1`"
done
echo
else echo "No multiple names found in file $list"
fi
if [ "$error2" != "0" ]; then
echo "Error2 : $error2 line(s) found with bad characters"
echo "Press Enter to show errors"
read
for j in `cat /tmp/err2`; do
echo "line $j : `cat $list | head -n $j |tail -n 1`"
done
echo
else echo "No bad character found in file $list"
fi
if [ "$error1" = "0" ]
then if [ "$error2" = "0" ]
then echo "No error found in $list. Going on with share creation"
else echo "Aborting"; exit 3
fi
else echo -e "\nPlease correct errors"; exit 3
fi
}
TEST_FS ()
{
menufs=`echo $menufs | tr a-z A-Z`
case "$menufs" in
"T" | "TEST" )
fileSystem=promo_1_3
storepath="/Promo_test/"
;;
"P24" | "PROMO_2_4" )
fileSystem=promo_2_4
while [ "$storepath" = "" ]; do
echo -e "\nPlease select path for $fileSystem : Promo_[2] , Promo_[4] :"
read storepath
storepath=`echo $storepath | tr a-z A-Z`
case "$storepath" in
"2")
storepath="/Promo_2/"
;;
"4")
storepath="/Promo_4/"
;;
* )
echo -e " Invalid path\n"
storepath=
;;
esac
done
;;
"P13" | "PROMO_1_3" )
fileSystem=promo_1_3
while [ "$storepath" = "" ]; do
echo -e "\nPlease select path for $fileSystem : Promo_[1] , Promo_[3] , Promo_[T]est :"
read storepath
storepath=`echo $storepath | tr a-z A-Z`
case "$storepath" in
"1")
storepath="/Promo_1/"
;;
"3")
storepath="/Promo_3/"
;;
"t" | "T" )
storepath="/Promo_test/"
;;
* )
echo -e " Invalid path\n"
storepath=
;;
esac
done
;;
"A" | "IDRIVES_ADM" )
fileSystem=IDrives_Adm
storepath="/IDrives_Adm/"
;;
"F" | "IDRIVES_FAC" )
fileSystem=IDrives_Fac
storepath="/IDrives_Fac/"
;;
"S1" | "STORE_ATA_1" )
fileSystem=store_ata_1
storepath="/Shares/"
;;
"S2" | "STORE_ATA_2" )
fileSystem=store_ata_2
storepath="/Share_Adm/"
;;
"S3" | "STORE_ATA_3" )
fileSystem=store_ata_3
while [ "$storepath" = "" ]; do
echo -e "\nPlease select path for $fileSystem : [K]iosk_MBA , [M]ix_Share_MBA , [P]romo_0 , [u]sr_edp :"
read storepath
storepath=`echo $storepath | tr a-z A-Z`
case "$storepath" in
"K")
storepath="/Kiosk_MBA/"
;;
"M")
storepath="/Mix_Share_MBA/"
;;
"P")
storepath="/Promo_0/"
;;
"U")
storepath="/usr_edp/"
;;
* )
echo -e " Invalid path\n"
storepath=
;;
esac
done
;;
"Q" | "q" )
echo -e "\n Bye\n"
exit 0
;;
* )
echo -e " \"$menufs\" not available\n"
menufs=
;;
esac
if [ "$storepath" = "" ]
then echo "Please select filesystem :"
else echo " Shares will be created on '$fileSystem' with path '$storepath'"
fi
}
FS_MENU () # Menu de selection du filesystem +fileSystem +path
{
path=
fileSystem=
menufs=
while [ "$menufs" = "" ]; do
echo -e "\n Available fileSystems\n ---------------------\n\n [A] IDrives_Adm\n [F] IDrives_Fac\n [S1] store_ata_1\n [S2] store_ata_2\n [S3] store_ata_3\n [P13] promo_1_3\n [P24] promo_2_4\n -------------------\n [Q] Quit"
read menufs
TEST_FS
done
echo
}
FINDID () # Recherche de l'ID +id ; +dispsh ; +nshare
{
if [ -f "/tmp/idtmp" ]
then rm "/tmp/idtmp"
fi
cmd=`nas_quotas -list -tree -fs $fileSystem | grep -i "$storepath$i " > /tmp/idtmp`
id=`cat /tmp/idtmp | awk {'print $2'}`
showsh=`cat /tmp/idtmp | awk {'print $4'} | sed 's/^\(.*\)\///'`
dispsh=`cat /tmp/idtmp | awk {'print $4'}`
}
ASKQUOTA ()
{
quota=$allquota
while [ "$minsize" = "" ]; do
echo -e "\n Enter quota soft limit in MB (HARD limit will be 10% more)"
read quota
if [ "$quota" != "" ]
then
minsize=$(($quota * 1024))
maxsize=$(($minsize + ($minsize * 10 / 100) ))
# check if quota is not equal to 0
if [ "$minsize" -lt "1024" ]
then echo -e " Wrong size\n"
quota=
fi
fi
echo " quota HARD limit size is $maxsize kB"
echo -e " quota soft limit size is $minsize kB\n"
done
}
ADDQUOTA ()
{
cancel=
nquota=`cat $list | wc | awk {'print $2'}`
if [ "$nquota" -le "1" ]
then i=$sharelist
fi
if [ "sharelist" = "" ] ; then sharelist=$share; fi
for i in $sharelist; do
echo -e "\n ----------------------------------------------------------"
echo " Making sure \"$i\" does not exist on $fileSystem$storepath"
FINDID
for j in $showsh; do
if [ `echo $i | tr a-z A-Z` = `echo $j | tr a-z A-Z` ]
then echo "'$i' already exists on $fileSystem as '$j'"
echo " Aborting"
doublon=$(($doublon+1))
cancel=1
echo "$i" >> /tmp/errorlist
else echo " Ok"
fi
done
if [ "$cancel" != "1" ]
then echo "Creating $storepath$i on $fileSystem"
nas_quotas -on -tree -fs $fileSystem -path `echo $storepath$i`
FINDID
id=`grep "$i " /tmp/idtmp | awk {'print $2'}`
DOQUOTA
echo "$i" >> /tmp/donelist
echo " $i created on $fileSystem with ID $id and a quota soft limit of $minsize kB"
fi
cancel=0
done
}
DOQUOTA () # Application de nouveaux quotas - PAUSE
{
ASKQUOTA
nas_quotas -edit -tree -fs $fileSystem -block $maxsize:$minsize $id
}
# START
if [ -f /tmp/errorlist ]; then rm /tmp/errorlist; fi
if [ -f /tmp/donelist ]; then rm /tmp/donelist; fi
doublon=0
nbargs=$#
if [ "$#" = "0" ]
then USAGE
else echo "Testing arguments"
TEST_ARGS $@
echo "Testing input list file"
FILE_TEST
echo "Setting '$list' entries in CAPITAL case"
#cat $list |tr a-z A-Z > /tmp/listtmp
#sharelist=`cat /tmp/listtmp`
sharelist=`cat $list |tr a-z A-Z`
echo -e "done\n"
ADDQUOTA
DOQUOTA
echo -e "\n Number of doublon(s) found : $doublon"
# Logging errors and creation in files
echo >> /home/nasadmin/donelist.log
date >> /home/nasadmin/donelist.log
cat /tmp/donelist >> /home/nasadmin/donelist.log
echo >> /home/nasadmin/errorlist.log
date >> /home/nasadmin/errorlist.log
cat /tmp/errorlist >> /home/nasadmin/errorlist.log
echo " Log files located in /home/nasadmin/donelist.log and /home/nasadmin/errorlist.log"
fi
echo -e "\n Bye\n"
Partager