System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

copy with data retention for 5 days then overwrite

SOLVED
Go to solution
noe
Occasional Advisor

copy with data retention for 5 days then overwrite

hi again, someone was kind to help me with a copy command script which seems to work. now i need to keep the copied data for 5 days (m-f) but on the following week the data needs to be overwriten. here is the copy .sh that im running:
#!/usr/bin/ksh
timestamp=`date | awk {'print $2 $3 $6'}`
for dir in /mytest/test1
do
cp -rp ${dir} /DATA/BACKUP/${dir}.${timestamp}
done
8 REPLIES
OldSchool
Honored Contributor

Re: copy with data retention for 5 days then overwrite

"following week the data needs to be overwriten"

doubtful...the "destination" directory in the script posted includes a "timestamp" in the form of "".

A) Surely you don't want to place "new" data in the old directory, which is what "overwrite" implies.

B) Do you want to continue to generate new directories with current "timestamp" and remove the old one?

C) Are you sure you only want to keep data 5 days, and not 7 (i.e. on Monday delete the prior Monday????)

D) Or would simply creating a /DATA/BACKUP/.mon thru /DATA/BACKUP/.fri work, overwriting those?
noe
Occasional Advisor

Re: copy with data retention for 5 days then overwrite

Thanks for the reply. the timestamp really isnt needed. as long as i can keep the data 5 days i will be happy. your option D seems logical but im not sure how to rewrite the sh
thanks
OldSchool
Honored Contributor
Solution

Re: copy with data retention for 5 days then overwrite

original script becomes:

#!/usr/bin/ksh
timestamp=`date +%a`
for dir in /mytest/test1
do
rm -rf /DATA/BACKUP/${dir}.${timestamp}
cp -rp ${dir} /DATA/BACKUP/${dir}.${timestamp}
done


timestamp will now have a value of mon, tue, wed, thu, fri (sat or sun would be there if run on weekends

it will attempt to remove an existing /DATA/BACKUP/${dir}.${timestamp} and then recreate it.
noe
Occasional Advisor

Re: copy with data retention for 5 days then overwrite

thanks, will try tonight will let you know how it goes
Dennis Handly
Acclaimed Contributor

Re: copy with data retention for 5 days then overwrite

>OldSchool: timestamp=`date +%a`

%a gives the weekday names. If you want numbers that sort, you can use %u (1-7) or %w (0-6)
noe
Occasional Advisor

Re: copy with data retention for 5 days then overwrite

hi the script failed because it didnt find /mytest/test1 dir's under /DATA/BACKUP So i created the sub dir test1 and run it manually it seems to work. but if i want to have say /mytest2/test2 , i guess i will have manually create it under /DATA/BACKUP/mytest2/test2 for the script to work right?
Dennis Handly
Acclaimed Contributor

Re: copy with data retention for 5 days then overwrite

>I guess I will have manually create it under /DATA/BACKUP/mytest2/test2 for the script to work right?

You can just do "mkdir -p" for the directory path then use cp.
noe
Occasional Advisor

Re: copy with data retention for 5 days then overwrite

worked