How to scp/tar files that are in between specific days?
Solution 1
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
Solution 2
You can check the find
command to get a list of the files that needs to be tared.
You can specify a start and end date (up to seconds precision) using the normal -atime
, -btime
, -mtime
... arguments in combination with the -not
argument.
You can then pipe the output to xargs
and then to tar
. Check the man page of find
for details about time arguments.
Update:
As Anthon suggested, you may use the +/- modifiers with -mtime
to specify the period without using -not
. Here is an example:
find . -mtime -5d2h3m10s -mtime +4d0h15m20s -print0 | xargs -0 tar cjvf mytar.tar.bz2
Where d, h, m, s
corresponds to days, hours, minutes and seconds respectively. This will give files modified newer than 5d2h3m10s
and older than 4d0h15m20s
Related videos on Youtube
Radek
Updated on September 18, 2022Comments
-
Radek almost 2 years
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. -
Anthon about 11 yearsthe argument to
-mtime
has to be a offset in days that is not always unless you that offset is fixed between running times. You can also use-mtime +X
and-mtime -X
to specify before and after X days ago instead of using -not. -
Radek about 11 yearsCan I specify hours and minutes when using your code?
-
Radek about 11 yearsWhat would be the syntax to specify absolute date & time?
-
Anthon about 11 years@Radek thanks for pointing the error out, since I could not edit my comment, I reinserted a new one with a
T
inserted between the Days and Hours. That I tested and works. Somehowfind
does not know how to split the string otherwise (probably because it could be YYMMDDhhmmss instead of YYYYMMDDhhmm. I thought that date would need a dot for the seconds (.ss
) but that is probably only for setting the date.