Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

backup few files from one directory script required.

 
Highlighted
Khalid Shakoor
Regular Advisor

backup few files from one directory script required.

Hi,

I would like to backup only few files from one directory to another on the same box based on date when files were created.

Scenario:

I have some logs file which overwrite on daily basis and I want to take backup using tar or zip on same machine before overwrite. But the issue is there are many other logs which are normally not in active condition, and I don’t want to take backup of these logs.

I really appreciated if someone shares with me the Script, to sort the logs by date and take backup of latest logs.

Thanks in advance
Khalid
7 REPLIES 7
Highlighted
Horia Chirculescu
Honored Contributor

Re: backup few files from one directory script required.

Hello,

If your logs are overwritten on daily basis, you could use a cron job to tar/gzip that log files on another location.

Assuming that your logs are named like this: *.log, and you are using sh as your shell, you can use something like this to sort the logs by date:

set -A SAVEDSET `ls -t *.log`

You can access the items from SAVEDSET (contain the file names) with:

${SAVEDSET[]}

in a while loop.


Horia.
Best regards from Romania,
Horia.
Highlighted
Kapil Jha
Honored Contributor

Re: backup few files from one directory script required.

I think you can write the crontab, which would tar and zip ur log file before ur log files going to be overwrite.


http://forums11.itrc.hp.com/service/forums/questionanswer.do?threadId=938732

may help.

BR,
Kapil+

I am in this small bowl, I wane see the real world......
Highlighted
Khalid Shakoor
Regular Advisor

Re: backup few files from one directory script required.

Hi Horia,

Can you please share with me any example,or script?
Actually i have not good knowledge of scripting.

Thanks
Highlighted
Dennis Handly
Acclaimed Contributor

Re: backup few files from one directory script required.

>based on date when files were created.

This info isn't available, unless encoded in the filename. The best you can do is look at the modification date or possibly the contents of each file.

>backup only few file

Is there a particular name/suffix for each? How many files would be in the directory? (If too many, you would have to use find(1) vs ls(1) or use ls(1) and grep.)
Highlighted
James R. Ferguson
Acclaimed Contributor

Re: backup few files from one directory script required.

Hi Khalid:

One option could be to cron a script daily which does something like:

#!/usr/bin/sh
REF=/tmp/refpoint
touch -amt $(date "+%m%d"0000) ${REF}
cd /path || exit
FILES=$(find . -xdev -type f -newer ${REF})
tar -cvf /tmp/MYARCHIVE ${FILES}
exit

This creates a reference file whose mtime is that of the current day at 00:00 hours. You then 'cd' to the directory containing the files you want to collect. The 'find()' looks for files modified since the day's beginning and 'tar's then into a tarball of you choice.

Regards!

...JRF...
Highlighted
Horia Chirculescu
Honored Contributor

Re: backup few files from one directory script required.

Hello, Khalid

Please pay attention to Dennis about date of creation. You only can access the time when the file was last modified.

ls -t sort the files by time modified (latest first) before sorting alphabetically.

Horia.
Best regards from Romania,
Horia.
Highlighted
Khalid Shakoor
Regular Advisor

Re: backup few files from one directory script required.

Thanks

Khalid