Logrotate RFE (2)
Harry Putnam
reader at newsguy.com
Fri Mar 5 00:34:50 UTC 2004
Keith Lofstrom <keithl at kl-ic.com> writes:
> On Thursday, March 4 Keith Lofstrom clarifies:
>
> Forgive me if I was unclear. I will explain in more detail. Indeed,
> the current logrotate renames files; all the rotated log files get
> renamed every day. rsync is not smart enough to notice that somelog.2
> today was yesterday's somelog.1; it may be the same size, but it has
> a different name and a different date. This is true for every numbered
> extension. Rsync can't detect this, and will move a lot of files
> because of it.
Ok, thanks ... this sounds a lot more interesting with a little more
explanation. I think I'll try it out with some logs. Because lime
most everbody here I commonly date files in a similar way.... Live
before editing a stock rc file I'll usually say:
cp rc.file rc.file_$(date +"%m%d%y_%T")
Thereby keeping a handlily recognizable dated orignal.
I got to doing it so much I finally broke down and wrote a script to
do it cpv. Inlined here in case anyone finds it usefull.
Beware though it is not polished at all. It has some flags you can
ignore that went with a directory tree Iused to keep of work in
progress.
The simple basic use is pretty handy though
cpv -c some.rc pre_tinkerage
copying some.rc to:
some.rc-030404_18:32:41_pre_tinkerage
so if desired you get a dated copy with some keyword or footnote
appended.
-----8< snip --------------
#!/bin/ksh
# Keywords: cpv - copy important files to uniqu names
# copy revisions of work-in-progress to a revisions managing setup
# Nov 07 03:58:20 2000
# &&
## BEGIN USAGE/FUNCTION[S] ===========================================
usage () {
cat >&2 <<EOM
Usage: \`cpv -[cmr] ARGUMENTS'
Generic examples:
\`cpv -c FILETOSAVE [<optional>COMMENT]' (copy)
\`cpv -m FILETOSAVE [<optional>COMMENT]' (move)
\`cpv -r FILETOSAVE SUBDIRECTORY [<optional>COMMENT]' (copy)
NOTES: A -[cmr] flag plus arguments is required
-r usage assumes the user has created a directory and subdirectory
Type \`$(basename $0) help' for details
Date format used is MONTH DAY HOUR MINUTE SECOND all slammed together.
EOM
} ## end
help () {
more >&2 <<EOM
Purpose:
\`$(basename $0)' with -c flag is a quick way to copy an original file
to a unique name for later reference.
\`$(basename $0)' with -r flag is a sort of rudimentary cvs with no
advanced features. It can save quick and dirty revisions of a
work in progress to a special directory and with a unique
name, to prevent work being lost or duplicated.
This allows later review on basis of time (the unique name
contains a date spec). Either usage is capable of
concatenating the filename with a date spec and optional
comment (appended to file name)
Usage: \`$(basename $0) -[cmr] ARGUMENTS
Generic examples:
\`$(basename $0) -c FILETOSAVE [<optional>COMMENT]
\`$(basename $0) -r FILETOSAVE SUBDIRECTORY [<optional>COMMENT]
\`$(basename $0) -m FILETOSAVE SUBDIRECTORY [<optional>COMMENT]
NOTES: Date format used is MONTH DAY HOUR MINUTE SECOND all
slammed together
See REV_CENTRAL [line 133] in getopts r section to set base
directory.
The first usage (-c) is fairly obvious so no further info on
that. The second usage (-r) expects the user to have created
a directory to save revisions in and a/some subdirectory[ies]
for various source files. Something like: ~/revisions/misc or
~/revisions/etc. Or the way I do:
~/revisons/scripts
~/revisions/rc_files
~/revisions/programming_projecta
~/revisions/programming_projectb
etc etc.
The idea here is to have a semi-fine granularity as to the
purpose or origins of the FILETOSAVE. But since all files
copied will end up with unique names, (Assuming copying is
done in at least 1 second increments) then the subdir could
be just one catchall and you could use the COMMENT feature as
a way to tag files.
Real examples:
\`$(basename $0) -c /etc/fstab ex_mount_points'
Will copy /etc/fstab to fstab-0815100905_ex_mount_points.
(in current directory)
In case you wanted to keep track of when some experimental mount
points were added.
\`$(basename $0) -m /etc/fstab ex_mount_points'
Ditto above only with -m it is a move rather than a copy.
\`$(basename $0) -r new.sh scripts'
Will copy new.sh to ~/revisions/scripts/new.sh-0815100905
\`$(basename $0) -r new.sh scripts add_f_g_flags
Will copy new.sh to ~/revisions/scripts/new.sh-0817122123_add_f_g_flags
In case you wanted to have a copy of new.sh before adding the new
flags to facilitate reverting to an older (working ..he he) version.
(above assumes you've created that directory and subdirectory)
(See REV_CENTRAL [line 133] in getopts r section to set base directory)
EOM
} #end usage
## BEGIN SCRIPT BODY ===========================================
# if we have a real file/directory put that value in file_dir
# by stripping the first character from ls -l
file_dir=$(ls -ld $2 2>/dev/null|cut -c1)
if [[ -z $1 ]]; then
usage
echo "Arguments are required"
echo "type \`$(basename $0) help' for details"
exit 1
elif [[ $1 = help ]]; then
help
exit
elif [[ $1 != -[cmr] ]]; then
usage
echo "A -c or -r flag plus arguments are required"
echo "type \`$(basename $0) help' for details"
exit 1
elif [[ -z "$file_dir" ]];then
usage
echo "Can't find a file/directory named <$2>"
echo "type \`$(basename $0) help for details"
exit 1
fi
while getopts "c:m:r:" opt; do
case "$opt" in
c) # copy file adding a date extension and optional comment
## Array elements set to newmonic or recognizable variables
## If we have 3 arguments, it means the last one is a comment to add
if [ $# = 3 ];then
sep="_"
fi
file=$2
comment="$3"
echo
echo "copying $file to:"
echo "$file$(date +"-%m%d%y_%T"$sep$comment)"
echo
cp -Rp "$file" "$file$(date +"-%m%d%y_%T"$sep$comment)"
;;
m) # move file adding a date extension and optional comment
## If we have 3 arguments, it means the last one is a comment to add
if [ $# = 3 ];then
sep="_"
fi
file=$2
comment="$3"
echo
echo "moving $file to:"
echo "$file$(date +"-%m%d%y_%T"$sep$comment)"
echo
mv "$file" "$file$(date +"-%m%d%y_%T"$sep$comment)"
;;
r) # cp files into revisions setup using date plus optional comment
## Set the base directory where revision subdirs are kept
REV_CENTRAL=/home/reader/projects/revisions/
file=$2
subdir=$3
comment="$4"
## If we have 4 args, it means a the last arg is a comment to add
if [ $# = 4 ]; then
sep="_"
fi
## Make sure we have both base and subdirectory
if [ -d $REV_CENTRAL${subdir##*/} ];then
echo ""
echo " copying $file-$(date +"%m%d%y_%T"$sep$comment) to:"
echo " $REV_CENTRAL${subdir##*/}"
echo ""
cp -i $file "$REV_CENTRAL${subdir##*/}/${file##*/}-$(date +"-%m%d%y_%T"$sep$comment)"
else
echo " No \"$subdir\" directory in $REV_CENTRAL"
echo " To save revisions you'll need to:"
echo
echo " \`mkdir $REV_CENTRAL${subdir##*/}' first'"
echo
exit 1
fi
;;
esac
done
More information about the fedora-devel-list
mailing list