133 lines
3.2 KiB
Bash
Executable File
133 lines
3.2 KiB
Bash
Executable File
#!/bin/sh
|
|
#
|
|
# Make backup copies of a specified file ( arg. 1 ). A second argument, <n>
|
|
# is optional and specifies that you want to keep <file>.0 through <file>.<n>
|
|
# as backups. If no number is given then the default, 6, will be used.
|
|
#
|
|
# - wheelan
|
|
#
|
|
# Heavily Modified by lucb for compression and presentation. Backwards
|
|
# compatible to Bill's version
|
|
#
|
|
usage () {
|
|
cat <<EOC
|
|
|
|
Usage: $0 [-v] [-d directory] [-c] [-n] file [number]
|
|
|
|
$0 makes backup copies of a specified file. A second argument,
|
|
\`number' is optional and specifies that you want to keep \`file.0'
|
|
through \`file.n' as backups. If no number is given then the default
|
|
\`9' will be used.
|
|
|
|
Compressed files [files ending in .Z or .gz] are checked for and moved
|
|
appropriately. In addition, if a compressed file is found, the newly
|
|
moved file will be compressed. The appropriate compression program is
|
|
used.
|
|
|
|
EOC
|
|
exit 2
|
|
}
|
|
|
|
#
|
|
# parse the command line arguments:
|
|
#
|
|
while [ $# -gt 1 ] ; do
|
|
case $1 in
|
|
-d) shift; directory=$1;;
|
|
-c) compression="y";;
|
|
-n) compression="n";;
|
|
-v) verbose="y" ;;
|
|
-x) set -x ;;
|
|
-h) usage;; # call the usage funtion
|
|
[a-zA-Z0-9]*) break;;
|
|
*) # Catch anything that doesn't match the
|
|
# previous flags
|
|
echo "Unknown option [$1]";
|
|
usage;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
if [ $# -lt 1 -o $# -gt 2 ] ; then
|
|
usage;
|
|
fi
|
|
|
|
if [ ! -f $1 ] ; then
|
|
echo "File $1 does not exist"
|
|
exit 1
|
|
fi
|
|
|
|
debug()
|
|
{
|
|
if [ x$verbose = xy ] ; then
|
|
echo "$1"
|
|
fi
|
|
}
|
|
|
|
index()
|
|
{
|
|
i=1;
|
|
while [ $i -le ${1:-$MIDX} ] ; do
|
|
echo -n "$i ";
|
|
i=`expr $i + 1`;
|
|
done
|
|
}
|
|
|
|
rindex()
|
|
{
|
|
i=${1:-$MIDX};
|
|
while [ $i -gt 0 ] ; do
|
|
echo -n "$i ";
|
|
i=`expr $i - 1`;
|
|
done
|
|
}
|
|
|
|
directory=${directory:-.}
|
|
if [ "${directory}" != "." ] ; then
|
|
if [ ! -d $directory ] ; then
|
|
echo "Can't find directory [$directory]"
|
|
exit 2
|
|
fi
|
|
fi
|
|
|
|
umask 000
|
|
compress=none
|
|
for n in `rindex ${2:-9}` ; do
|
|
src=${directory}/$1.`expr $n - 1`
|
|
dst=${directory}/$1.$n
|
|
if [ -d $src -o -f $src ] ; then
|
|
/bin/mv -f $src $dst && debug "/bin/mv -f $src $dst"
|
|
compress=none
|
|
elif [ -f $src.Z ] ; then
|
|
/bin/mv -f $src.Z $dst.Z && debug "/bin/mv -f $src.Z $dst.Z"
|
|
compress=compress
|
|
elif [ -f $src.gz ] ; then
|
|
/bin/mv -f $src.gz $dst.gz && debug "/bin/mv -f $src.gz $dst.gz"
|
|
compress=gzip
|
|
fi
|
|
done
|
|
|
|
# A copy would keep the file permissions the same, but will fill in "holes"
|
|
# in the file.
|
|
|
|
dst=$directory/$1.0
|
|
perm=`perl -e '$mode=((stat("'$1'"))[2] & 07777); printf ("%o\n", $mode)'`
|
|
mv $1 $dst && debug "mv $1 $dst"
|
|
cat /dev/null > $1
|
|
chmod $perm $1 && debug "chmod $perm $1"
|
|
|
|
if [ x${compression:-} = xn ] ; then
|
|
debug "No compression requested";
|
|
elif [ x${compression:-} = xy ] ; then
|
|
gzip $dst && debug "gzip $dst (explicitely)" ;
|
|
else
|
|
case "$compress" in
|
|
"none")
|
|
debug "No compression";;
|
|
"compress")
|
|
compress $dst && debug "compress $dst" ;;
|
|
"gzip")
|
|
gzip $dst && debug "gzip $dst" ;;
|
|
esac
|
|
fi
|