Operating System - HP-UX
1826345 Members
4145 Online
109692 Solutions
New Discussion

Script to check the latest file in directory and rsync the file to other server

 
HP-UX_Ali
Regular Advisor

Script to check the latest file in directory and rsync the file to other server

Hello Experts,

Required your suggestion in making a good script.

Scenario: Every day a file is coming from FTP server to a particular director, it contains the files with timestamp. and from here i have to copy the files to other server with cron set. using rsync

xxxxx_xxxx_xxxxxx_xxxxx.csv_20110207_100007_0566
The file name is same like xx_xx_xx_xx.csv._<>_<>
Every time there is change in file timestamp , it copies to directory. and i have to copy to server.

Pls suggest .


Regards
Ali.


7 REPLIES 7
James R. Ferguson
Acclaimed Contributor

Re: Script to check the latest file in directory and rsync the file to other server

Hi Ali:

It would seem that you can use the '--include=PATTERN' and perhaps the '--exclude=PATTERN' to copy only files matching your format. See the manpages for 'rsync'.

Regards!

...JRF...
HP-UX_Ali
Regular Advisor

Re: Script to check the latest file in directory and rsync the file to other server

Thanks for the reply, but how you will incluse or exclude the pattern. here is the scenario of timestamp. When time is changed, File is recieved by directory and from their i have to copy the to other servers.

In this scenario there is no include or exclude, if you see the file name.
It is generated in the same manner.

How yo will calcluate the timestamp here.

Regards
Ali
Zinky
Honored Contributor

Re: Script to check the latest file in directory and rsync the file to other server

No need to have a script.

RSYNC already has the smarts to "rsync" only changed files. See it's syntax/guide.



Hakuna Matata

Favourite Toy:
AMD Athlon II X6 1090T 6-core, 16GB RAM, 12TB ZFS RAIDZ-2 Storage. Linux Centos 5.6 running KVM Hypervisor. Virtual Machines: Ubuntu, Mint, Solaris 10, Windows 7 Professional, Windows XP Pro, Windows Server 2008R2, DOS 6.22, OpenFiler
Zinky
Honored Contributor

Re: Script to check the latest file in directory and rsync the file to other server

See http://www.thegeekstuff.com/2010/09/rsync-command-examples/ for a syntax that best suites your need.

Hakuna Matata

Favourite Toy:
AMD Athlon II X6 1090T 6-core, 16GB RAM, 12TB ZFS RAIDZ-2 Storage. Linux Centos 5.6 running KVM Hypervisor. Virtual Machines: Ubuntu, Mint, Solaris 10, Windows 7 Professional, Windows XP Pro, Windows Server 2008R2, DOS 6.22, OpenFiler
HP-UX_Ali
Regular Advisor

Re: Script to check the latest file in directory and rsync the file to other server

The files names are not same. and i dont want to copy the whole files to the destination.

Like from today i want to send only latest file to the destination.
NOTE : Every day file name is changed.
and i dont want all files to copy on destination servers

Regards
Ali.
James R. Ferguson
Acclaimed Contributor

Re: Script to check the latest file in directory and rsync the file to other server

Hi (again):

> The files names are not same. and i dont want to copy the whole files to the destination. Like from today i want to send only latest file to the destination.

If this is as simple as fetching the newest file in a particular directory, you don't need 'rsync'. You could do something like this:

Create or update the timestamp of a reference file in another directory every time your script runs *and* FTP's a file. You simply do this with 'touch'.

When your FTP script is launched, look for the most recent file in the directory (with an 'ls -1t|head -1') and if that file's modification timestamp is newer than that of the reference file, FTP it and update the reference file timestamp to that of the file just moved. You can use tests like 'if [ ${myfile} -nt ${reffile} ]; then ...'. If the most recent file isn't newer than the reference file, exit without further processing.

Regards!

...JRF...
Regards!

...JRF...
HP-UX_Ali
Regular Advisor

Re: Script to check the latest file in directory and rsync the file to other server

Thanks all, Better option to use rsync with --exclude-from= and run.

So its working....

Regards
Ali.