|
#!/usr/bin/ksh93
################################################################
function usagemsg {
print ""
print "Program: getdirstruct.sh"
print ""
print "This script retrieves a directory structure from a remote"
print "machine and builds the commands to recreate it."
print ""
print "Usage: ${1} [-v|-V] [-c] machineName TopDir"
print ""
print " Where '-v' = Verbose mode"
print " '-V' = Very Verbose mode"
print " '-c' = Generate directory building commands"
print " as output (Default: array definitions)"
print ""
print " machineName = The IP name of a machine for which to "
print " build commands"
print ""
print " TopDir = The full path directory name from which"
print " extract the directory structure"
print ""
print "Author: Dana French (dfrench@mtxia.com)"
print ""
print "\"AutoContent\" enabled"
print ""
}
################################################################
####
#### Description:
####
#### This is a generic script that retrieves directory structures and
#### attributes from a remote machine.
####
#### This script can operate in two modes: Array or Command generating.
#### In "Array" mode, Korn Shell 93 code is generated is used
#### to define an array of values containing the directory information
#### and attributes. In "Command" mode, unix style commands are generated
#### to recreate the directory structure and attributes. The default is
#### "Array" mode.
####
#### Assumptions:
####
#### The "getdirstruct.sh" script assumes it has "rsh" access
#### to the remote machine from which it is retrieving the directory
#### structure.
####
#### Dependencies:
####
#### No dependencies other than network connectivity to the machine
#### from which it is extracting the directory structure.
####
#### Products:
####
#### This script generates Korn Shell 93 Compliant commands that can
#### be used to define an array of values, or this script can generate
#### Unix commands to create the directory structure and its attributes.
####
#### Configured Usage:
####
#### This script can be run from the command line, scheduled, or called
#### from another script.
####
#### Details:
####
################################################################
TRUE="1"
FALSE="0"
VERBOSE="${FALSE}"
VERYVERB="${FALSE}"
GENCMDS="${FALSE}"
while getopts ":vVc" OPTION
do
case "${OPTION}" in
'c') GENCMDS="${TRUE}";;
'v') VERBOSE="${TRUE}";;
'V') VERBOSE="${TRUE}"
VERYVERB="${TRUE}";;
'?') usagemsg "${0}" && exit 1 ;;
esac
done
shift $(( ${OPTIND} - 1 ))
trap "usagemsg ${0}" EXIT
MACHNAME="${1:?ERROR: Machine Name not specified}"
DIRID1="${2:?ERROR: Top level directory not specified}"
trap "-" EXIT
JCNT="0"
DIRIDX=$( basename "${DIRID1}" )
(( VERBOSE == TRUE )) && print -u2 "# Processing ${MACHNAME}:${DIRIDX}"
################################################################
####
#### The first thing the "getdirstruct" script does is to execute
#### a "find" command on the remote machine to retrieve all the
#### directories under the top level directory specified on the
#### command line when the script was run. The "find" command
#### produces "ls -ld" style output for the purpose of extracting
#### and processing the attributes associated with each directory.
####
################################################################
rsh ${MACHNAME} "find ${DIRID1%/} -type d -exec ls -ld {} \;" |
while read -r -- DIRID2
do
################################################################
####
#### The file owner and group are easily
#### extracted from the "ls -ld" output, however the permissions require
#### some processing.
####
################################################################
# Parse the ls -l output for the directory
set -- ${DIRID2}
# extract the owner for the directory from the ls -l output
DOWNER="${3}"
# extract the group for the directory from the ls -l output
DGROUP="${4}"
# extract the permission settings for the directory from the ls -l output
PERMS="${1:1:9}"
# extract the name of the directory from the ls -l output
eval DIRNAME="\"\${${#}}\""
[[ "_${DIRNAME}" = *lost+found ]] && continue
################################################################
####
#### The permission settings are contained within the first 10 characters
#### of the "ls -ld" output and are processed in 3 steps. The first character
#### is the file type and is ignored, the next 3 characters are associated
#### with the "user" or file owner permissions. The permissions are
#### checked to see if the tacky bit or SUID bit is set. If so
#### the appropriate permission setting is added to the permission string
#### which will be used when directory permissions are set.
####
################################################################
# extract the user permission settings for the directory from the ls -ld output
UPERMS="${PERMS:0:3}"
# remove the dashes "-" from the user permissions
UPERMS="${UPERMS//\-/}"
# Convert lowercase "s" to "xs" in the user permissions
UPERMS="${UPERMS//s/xs}"
# Convert uppercase "S" to "s" in the user permissions
UPERMS="${UPERMS//S/s}"
# Convert lowercase "t" to "xt" in the user permissions
UPERMS="${UPERMS//t/xt}"
# Convert uppercase "T" to "t" in the user permissions
UPERMS="${UPERMS//T/t}"
################################################################
####
#### The next set of 3 characters are associated with the "group"
#### category of permissions. The permissions are checked to see
#### if the tacky bit or SUID bit is set. If so the appropriate
#### permission setting is added to the permission string which will
#### be used when the directory permissions are set.
####
################################################################
# extract the group permission settings for the directory from the ls -ld output
GPERMS="${PERMS:3:3}"
# remove the dashes "-" from the group permissions
GPERMS="${GPERMS//\-/}"
# Convert lowercase "s" to "xs" in the group permissions
GPERMS="${GPERMS//s/xs}"
# Convert uppercase "S" to "s" in the group permissions
GPERMS="${GPERMS//S/s}"
# Convert lowercase "t" to "xt" in the group permissions
GPERMS="${GPERMS//t/xt}"
# Convert uppercase "T" to "t" in the group permissions
GPERMS="${GPERMS//T/t}"
################################################################
####
#### The last set of 3 characters are associated with the "other"
#### category of permissions. Again, the permissions are checked to see
#### if the tacky bit or SUID bit is set. If so the appropriate permission
#### setting is added to the permission string which will be used when the
#### directory permissions are set.
####
################################################################
# extract the other permission settings for the directory from the ls -ld output
OPERMS="${PERMS:6:3}"
# remove the dashes "-" from the other permissions
OPERMS="${OPERMS//\-/}"
# Convert lowercase "s" to "xs" in the other permissions
OPERMS="${OPERMS//s/xs}"
# Convert uppercase "S" to "s" in the other permissions
OPERMS="${OPERMS//S/s}"
# Convert lowercase "t" to "xt" in the other permissions
OPERMS="${OPERMS//t/xt}"
# Convert uppercase "T" to "t" in the other permissions
OPERMS="${OPERMS//T/t}"
################################################################
####
#### With the permission setting strings for each user category now known
#### and extracted from the "ls -ld" output, a mnemonic "exact setting"
#### string is constructed for use with the "chmod" command.
####
################################################################
# Build the mnemonic mode exact permission setting command
DMODE="u=${UPERMS},g=${GPERMS},o=${OPERMS}"
print ""
(( VERYVERB == TRUE )) && print -u2 "# ${MACHNAME}:${DMODE}:${DOWNER}:${DGROUP}:${DIRNAME}"
################################################################
#### Depending upon the options specified when this script was run,
#### either Korn Shell 93 compliant commands or Unix commands will
#### be generated.
####
#### The generated Korn Shell 93 Compliant commands can be used to
#### define a variable array of values which contain the directory name
#### and various attributes associated with each directory.
####
#### The Unix commands generated consist of mkdir, chown, chgrp, and
#### chmod, which can be used to create the directory and change its
#### attributes to match those of the original directory.
####
################################################################
if (( GENCMDS == TRUE ))
then
print "mkdir -p \"\${ALT_ROOT}${DIRNAME}\""
print "chown ${DOWNER} \"\${ALT_ROOT}${DIRNAME}\""
print "chgrp ${DGROUP} \"\${ALT_ROOT}${DIRNAME}\""
print "chmod \"${DMODE}\" \"\${ALT_ROOT}${DIRNAME}\""
else
print "DIR_${DIRIDX}_NAME[${JCNT}]=\"\${ALT_ROOT}${DIRNAME}\""
print "DIR_${DIRIDX}_OWNER[${JCNT}]=\"\${ALT_ROOT}${DOWNER}\""
print "DIR_${DIRIDX}_GROUP[${JCNT}]=\"\${ALT_ROOT}${DGROUP}\""
print "DIR_${DIRIDX}_MODE[${JCNT}]=\"\${ALT_ROOT}${DMODE}\""
fi
JCNT=$(( ${JCNT} + 1 ))
done
|
|
|