i am having a problem with writing a shell script to back up files for a specific month. the month and year are specified as paramters. so a backup of all the files modified during the specified month have to be made. for example if specify 200406 as my parameter, it should back up files that have been modified during the month of june in year 2004
i know u can use the find -atime -n. but it aonly finds files that were accesed within n days. but i would like to find files for a specific month and back it up. need help please
thanks
I think u will need to find the diff of current that with the start and end date of the month u want backup.
and u may use the find . -mtime -60 -mtime +30
-mtime -60,...less than 60 days ago
-mtime +30...more than 30 days ago..
this way u will have to look for yours month duration...
but there should be better ways...may be someone else in group can help
An alternative is to try to match the Month and year in a directory listing - tho the change from "dd Mmm hh:mm" format to "dd Mmm yyyy" when a file gets to six months old complicates this.
I would be a good idea to log the details of files deleted too!
Code
# note proper parameter validation (e.g. mon between 1 and 12 inclusive)
# script also assumes that you have read/write access to all subdirectories
scriptname=$(basename $0)
if [ $# -ne 3 ]; then
print "usage: $scriptname MonthNumber YearNumber directory, e.g. '$scriptname 4 2006 /tmp'"
exit 0
fi
set -A monnames Zero Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
typeset -i mon=$1
typeset -i yr=$2
typeset -i thisyr=$(date +%Y)
typeset -i lastyr=$(($thisyr -1))
typeset -i thismon=$(date +%m)
typeset -i fmon
print "Print Deleting files from ${monnames[$mon]} $yr in directory $3"
find $3 -type f -ls | sed -e's/ *//' | tr -s ' ' | cut -d' ' -f8- | while read fmonname fday fyrtime ffilename
do
# Get month number from month name in file list
fmon=1
while :
do
if [ $fmonname = ${monnames[$fmon]} ]; then
break
fi
fmon=$(($fmon + 1))
done
# if the last part of the timestamp is a time rather than a yr then
# we need to find the yr it belongs to
if [[ $fyrtime = *:* ]]; then
# if month number is not greater than current month number then it is in this yr ...
if [ $fmon -le $thismon ]; then
fyrtime=$thisyr
else
# ... otherwise it's last yr
fyrtime=$lastyr
fi
fi
if [ $fmon = $mon ]&&[ $fyrtime = $yr ]; then
print "deleting $fname dated $fday $fmon $fyrtime $ffilename"
-- backup $ffilename somehow --
fi
done
~
any help?
cheers
thanks but i wanted to copy the files for a specilfy month. is it possible to search through the directory list for the specific month specified