Archive directory script with tar/compress

Hi all

I need to write a script that archives all files with a certain date in the filename, to another location.

It has to run on a AIX using tar/compress or another standard AIX tool.

The directory will have x files, each prefixed with a date like yyyymmdd_desc.csv.

I need all to be able to call the script with 4 parameters:

  1. Directory to start in (read from)
  2. Prefixed date (used for file selection)
  3. Directory to place/write the archive in (write to)
  4. Archive filename

After the compress the script also needs to delete all files from the directory that was added to the new archive.

I have been trying to use find, grep, do loops - but has not been able to find a solution :confused:

while getopts S: D: T: F: para
do

case $(para) in
S) SOURCE DIRECOTRY=$OPTARG;;
D) DATE=$OPTARG;;
T) TARGET_DIRECTORY=$OPTARG;;
F) FILENAME=$OPTARG;;
*)ERR = echo "use correct parameters please";;

esac
done

echo "${SOURCE_DIRECOTRY} this is the directory u want to do tar"
for file in `ls ./${SOURCE_DIRECTORY}
do 
mv file `date`.file
done
for dir in `ls -ld/${SOURCE_DIRECOTRY}`
do
tar -cvf ./${SOURCE_DIRECTORY} {FILENAME} 
done

i havent checked this. but i think it will help u a little. this script will take 4 parameters. namely source_directory, date,filename,target_directory.

and will put date in front of the files u want to tar and then u can do it.

Hi dazdseg

Tx for your fast reply.

I need to archive only the files witch has a filename that is prefixed with a certain date - (the files already has got this date in the filename). The directory can contain similar files with other dates.

ie.
20100630_abced.csv
20100630_efghi.csv
20100630_mkiujn.csv
20100629_efghi.csv
etc..

I only want to add the files with 20100630_*.csv to the archive

then u can use

tar -cvf 20100630_*

this will only take those files.. which are beginning with 20100630. u can also have to tweak the above script a little.

while getopts S:T: F: para
do

case $(para) in
S) SOURCE DIRECOTRY=$OPTARG;;
T) TARGET_DIRECTORY=$OPTARG;;
F) FILENAME=$OPTARG;;
*)ERR = echo "use correct parameters please";;

esac
done

echo "${SOURCE_DIRECOTRY} this is the directory u want to do tar"
for file in `ls ./${SOURCE_DIRECTORY}
do 
tar - cvf 2010030_* 
done
for file in ./${SOURCE_DIRECTORY}`ls -*.tar`
do
mv file ./${TARGET_DIRECTORY}
done

i think this will get you going.

I'm getting an error when I try to run your script

' is not expected.tax error at line 5 : `in

And I need to get the DATE parameter in again and to be used with the mv/rm command.

for file in `ls ./${SOURCE_DIRECTORY}`

sorry i missed the last tilde sign at the end of the line. this error will go away. when u use that.

can you explain me ... why do u need date parameter for. are u gona check the files with date.
in ur directory are files being made with different dates.. and u want to tar them according to date. let me know if my understanding is correct or can you please explain what u exactly trying to do. because i am not sure why u want date parameter. ??

Sorry, but I'm getting the same error regarding line 5 :confused:

My files are named with a date as the first part of the filename (not the date they are created)

ie.
20100630_file1.csv
20100629_file1.csv
20100630_file2.csv
20100701_file3.csv
20100630_file3.csv
etc..

But I'm only interessted in compressing certain files for my archive.

ie. in the above list I only want to compress files with 20100630 in the filename (and delete them afterwards). On another day I might want to compress all files with 20100701 in the filename.

ohh okie then ur script will take 4 parameters as i have shown in 1st code. i will nt be testing it. bt i can tell u a rough idea hw it will go ...

while getopts S: D: T: F: para
do

case $(para) in
S) SOURCE DIRECOTRY=$OPTARG;;
D) DATE=$OPTARG;;
T) TARGET_DIRECTORY=$OPTARG;;
F) FILENAME=$OPTARG;;
*)ERR = echo "use correct parameters please";;

esac
done

for file in `ls {D}_*./${SOURCE_DIRECTORY}
do 
tar - cvf file
done

this shud work.. for u...