Moving data between quota protected directories on Isilon, take II

My previous post about moving data between quota’d directories on Isilon is probably one of the most popular I have made, garnering up to 5 hits per week.

But that script has a big issue: it can’t handle special characters or–worse yet–spaces in file and directory names. So here’s an improved version:


!/bin/bash

#Tests whether there is a valid path
testexist () {
        if [ ! -r "$1" ] || [[ "$1" != *ifs* ]]; then
                echo "$1" " is an invalid path. Please check the path and/or use a full path including /ifs."
        exit 1
        fi
}

#Iterates through path backwards to find most closely related quota
findquota () {
        RIGHTPATH=0
        QUOTADIR="$1"
        while [ $RIGHTPATH -eq 0 ]; do
                QUOTADIR=$(echo "$QUOTADIR" | sed 's|/[^/]*$||')
                isi quota list | grep -q "$QUOTADIR" && RIGHTPATH=1
        done
        echo $QUOTADIR
}

testquota () {
        if [ "$1" = "-" ] || [ -z $1 ]; then
                echo "No hard directory quota on this directory. Just use mv."
                exit
        fi
}

if [[ $# -ne 2 ]]; then
        #Gets paths from user
        echo "Enter source: "
        read SOURCE
        echo "Enter target: "
        read TARGET
else
        SOURCE="$1"
        TARGET="$2"
fi

testexist "$SOURCE"
testexist "$TARGET"

#Verifies paths with user
echo -en "Moving $SOURCE to $TARGET\nIs this correct? (y/n): "
read ANSWER
if [ $ANSWER != 'y' ] ; then
        exit
fi

#Defines quotas
SOURCEQUOTA=$(findquota "$SOURCE")
TARGETQUOTA=$(findquota "$TARGET")

#Gets size of hard threshold from quota

SOURCETHRESH=$(isi quota view --path=$SOURCEQUOTA --type=directory | awk -F": " '$1~/Hard Threshold/ {print $2}' | sed s/.00//)
TARGETTHRESH=$(isi quota view --path=$TARGETQUOTA --type=directory | awk -F": " '$1~/Hard Threshold/ {print $2}' | sed s/.00//)
testquota $SOURCETHRESH
testquota $TARGETTHRESH

echo -e "\nOriginal values:"
echo $SOURCEQUOTA $SOURCETHRESH
echo $TARGETQUOTA $TARGETTHRESH

isi quota quotas delete --type=directory --path=$SOURCEQUOTA -f
isi quota quotas delete --type=directory --path=$TARGETQUOTA -f

mv "$SOURCE" "$TARGET"

isi quota quotas create $SOURCEQUOTA directory --hard-threshold=$SOURCETHRESH --container=yes
isi quota quotas create $TARGETQUOTA directory --hard-threshold=$TARGETTHRESH --container=yes

echo -e "\nNew values:"
isi quota list | grep $SOURCEQUOTA
isi quota list | grep $TARGETQUOTA

echo -e "\nls -l $TARGET"
ls -l "$TARGET"

This version uses a ton of quotes to handle all the crazy stuff SMB users put on Isilons.

I’ve also cleaned up the findquota () function. It now doesn’t bother with incrementing stuff, it just backs through the path using a sed command. Once it sets a path, it checks that against the quota list on the Isilon. If that command returns true, it will then change the $RIGHTPATH variable to 1 and exit the loop.

The outputs at the end are also a little cleaner so you can tell what the heck it’s doing when it spews out its final output.