0

I am trying to create a script that need to archive text logs from last week.

Basically we have a path where we have folders on date as follows, Each folder contains csv files containing data for that day

2015-02-01
2015-02-02
2015-02-03
2015-02-04
2015-02-05

We want to run a script that runs every sunday and archives all folders and files with in an archive for all the folders last week.

Any idea how I can achieve that?

I am using this command as a test which should archive all folders older then 5 days but its just compressing one folder

find . -type d -mtime +5 -print -exec tar -zvcf test.tar.gz {} \;

1 Answer 1

0

If you are running that script on a specific day and if you have access to GNU date and GNU tar (i.e. Linux) you can use something like this (I take it you're using bash, not tested on other shells):

for i in {1..7}; do
    FOLDER=`date +%Y-%m-%d -d "$i days ago"`
    tar zcfv /var/tmp/${FOLDER}.tgz ${FOLDER}/
done

GNU date is important as it understands the -d "N days ago" param.

As your folder names are predictable, this is usually also faster than a find

/Edit: if you need one archive for a whole week, just add to the tar sources.

WEEK=`date +%U`
SOURCE=""
for i in {1..7}; do
    FOLDER=`date +%Y-%m-%d -d "$i days ago"`
    SOURCE=${SOURCE}" "${FOLDER}/
done
tar zcfv /var/tmp/week${WEEK}.tgz ${SOURCE}

Output of my testscript (as of today):

tar zcfv /var/tmp/week07.tgz 2015-02-17/ 2015-02-16/ 2015-02-15/ 2015-02-14/ 2015-02-13/ 2015-02-12/ 2015-02-11/
3
  • Depends on the current workdir ($PWD) of your script. Ex: All your subdirectories are in /d01, and you need to have your output in /opt/result. So just add a SRCDIR="/d01/" and DSTDIR="/opt/result/" to the script and use SOURCE=${SOURCE}" "${SRCDIR}${FOLDER} and a tar zcfv ${DSTDIR}week${WEEK} ${SOURCE} line. Feb 18, 2015 at 21:18
  • I want to make the script generic so it picks up all directories modified in last week . It is not necessary that the folder name will be of that format. also to have the folders deleted after tar has been done
    – Junaid388
    Feb 18, 2015 at 21:20
  • For that you need to only examine the contents of the source, not the source itself. The source itself is modified with each new directory creation. Instead of "find . -type d -mtime -7" (it is -7 by the way) use "find /d01/* -type d -mtime -7", that will find what you want. Feb 18, 2015 at 22:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .