admin-stuff/disk-space_usage.sh.00
2017-02-21 02:14:51 +01:00

191 lines
3.8 KiB
Bash
Executable File

#!/usr/bin/env bash
right_tabstop=30
warn (){
echo ""
echo -e " [ Warning ] $*"
echo ""
}
function usage() {
echo
[ -n "$1" ] && echo -e "Error: $1\n"
cat<<EOF
Usage: `basename $0` [-oq] <dir>
Prints out a summary of disk space usage for a given directory.
The Output contains also the amount of diskspace usage of the
subdirectories.
Options:
-o <directory>
A summary file will be stored in the given directory. The filename contains
the directory name of the determined directory and also a timestamp in human
readable form.
-q
Suppresses the output to stdout. Only useful in conjunction with "-o"
EOF
exit 1
}
_be_quiet=false
_output_to_file=false
while getopts o:q opt ; do
case $opt in
q) _be_quiet=true
;;
o) _output_to_file=true
if [ -d "$OPTARG" ]; then
_out_dir=$OPTARG
else
usage "Output directory not found"
fi
;;
h) usage
;;
\?) usage
esac
done
shift `expr $OPTIND - 1`
if [ $# -eq 0 ]; then
usage "Missing parameter for \"<dir>\""
elif [ $# -gt 1 ]; then
usage "To many parameters given"
fi
base_dir=$1
if [ ! -d $base_dir ]; then
usage "Directory \"$base_dir\" not found!"
fi
if $_output_to_file ; then
_out_file="${_out_dir}/`basename \"$base_dir\"`_space_usage.`date +%Y%m%d-%H%M`"
touch $_out_file
fi
if $_be_quiet && ! $_output_to_file ; then
warn "You have told me to be quiet (-q), but gives no output directory."
exit
fi
## - print out
## -
outnl(){
echo X\\c > /tmp/shprompt$$
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
if $_output_to_file ; then
echo -e "$*\\c" >> $_out_file
fi
if ! $_be_quiet ; then
echo -e "$*\\c" 1>&2
fi
else
if $_output_to_file ; then
echo -e "$*" >> $_out_file
fi
if ! $_be_quiet ; then
echo -e "$*" 1>&2
fi
fi
rm /tmp/shprompt$$
}
outnl ""
outnl "Disk Space Usage Directory \"$base_dir\":"
outnl ""
cd $backup_base_dir
## - Read list of (sub)directories inside $base_dir into
## - an array.
## -
dir_list_arr=()
while IFS='' read -r -d '' _dir; do
dir_list_arr+=($_dir);
done < <(find $base_dir -mindepth 1 -maxdepth 1 -type d -print0)
## - Sort the array
## -
dir_list_sorted_arr=()
readarray -t dir_list_sorted_arr < <(for a in "${dir_list_arr[@]}"; do echo "$a"; done | sort)
for _dir in "${dir_list_sorted_arr[@]}"; do
## - Determin space usage of $_dir
## -
_out_du=$(du -sh "$_dir")
_space=`echo ${_out_du} | cut -d ' ' -f1`
_host=`basename "$_dir"`
## - Formatted Output
## -
## - Determin number of signs between directory name
## - and directory size
## -
_strlen=${#_host}
_count_blank=`expr $right_tabstop - $_strlen`
_str_blanks=""
while [ $_count_blank -gt 2 ]; do
_str_blanks="${_str_blanks}."
_count_blank=`expr $_count_blank - 1`
done
_str_blanks="${_str_blanks}: "
_outstring="\t${_host}${_str_blanks}${_space}"
outnl "$_outstring"
done
## - Summary
## -
## - overall usage of directory $$base_dir
## -
_overall_usage=`du -sh $base_dir | awk '{print$1}'`
## - Print Summary Line
## -
_strlen=${#_overall_usage}
_count_blank=`expr $right_tabstop + $_strlen`
_str_blanks=""
_outstring="\t"
while [ $_count_blank -gt 0 ]; do
_str_blanks="${_str_blanks}. "
_count_blank=`expr $_count_blank - 1`
_outstring="${_outstring}="
done
outnl ""
outnl "$_outstring"
## - Print Summary
## -
_strlen=${#base_dir}
_count_blank=`expr $right_tabstop - $_strlen`
_str_blanks=""
while [ $_count_blank -gt 0 ]; do
_str_blanks="${_str_blanks} "
_count_blank=`expr $_count_blank - 1`
done
_outstring="\t${base_dir}${_str_blanks}${_overall_usage}"
outnl "$_outstring"
outnl
exit 0