How to scp/tar files that are in between specific days

datefilesscptar

I need to copy db log files between two suse servers where I am interested in files ONLY in between 10.3.2013 – 13.3.2013

It is desired to compress the files before copying so I tar them and scp. Currently I am using tar -cvzf /tmp/saas_archive_logs.tar.gz /var/lib/edumate/backup/archive_logs/db2inst1/SAAS --newer-mtime=2013-03-10 that gives me all files from 10.3.2013 till now. But I don't need all of them. And I didn't find any tar switch.

Best Answer

As Bichoy indicated you can use the find command to find files with a specific access, create and modification time. However -mtime takes an offset in 24 hour increments and is not always convenient to calculate unless you want something from a specific amount of numbers of 'days' ago. You will need to combine that with -daystart to 'round' that to the beginning of the day.

I think more convenient in your case, is the -newermt option which takes a datestring (and not the name of a reference file like most -newerXY versions)

Combine that with find's -print0 option to handle files with spaces in the name and optionally -type f not to get any directories in the period you are interested in:

find /var/lib/edumate/backup/archive_logs/db2inst1/SAAS \
   -newermt 20130310 -not -newermt 20130314 -type f -print0 \
   | xargs -0 tar -cvzf /tmp/saas_archive_logs.tar.gz 

There is one big problem with that: in case the number of files found becomes to long, xargs will invoke its command (in this case tar) multiple times as xargs needs to fit the arguments on the commandline which is not infinite. To circumvent that I always use cpio, which reads filenames from stdin. With the --format=ustar parameter to get a POSIX tar file, and in your case you would need to pipe the output through gzip to get the desired result:

find /var/lib/edumate/backup/archive_logs/db2inst1/SAAS \
   -newermt 20130310 -not -newermt 20130314 -type f -print0 \
   | cpio --create --null  --format=ustar \
   | gzip > /tmp/saas_archive_logs.tar.gz
Related Question