LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > CentOS
User Name
Password
CentOS This forum is for the discussion of CentOS Linux. Note: This forum does not have any official participation.

Notices


Reply
  Search this Thread
Old 08-23-2017, 10:19 AM   #1
robertkwild
Member
 
Registered: Feb 2015
Posts: 382

Rep: Reputation: Disabled
set up a watch folder or rsync


hi all,

i have a folder on my linux server and i have created a new folder on the same linux server so its like this -

old folder - /mnt/ceta_invoices_2

new folder - /mnt/ceta_invoices_3

i want to be able to watch anything new that goes in " /mnt/ceta_invoices_2" to go straight away ie copy to "/mnt/ceta_invoices_3"

is it best to set up an rsync on a crontab for this or use the inotifywait command?

many thanks,

rob
 
Old 08-23-2017, 10:41 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Quote:
Originally Posted by robertkwild View Post
i want to be able to watch anything new that goes in " /mnt/ceta_invoices_2" to go straight away ie copy to "/mnt/ceta_invoices_3"

is it best to set up an rsync on a crontab for this or use the inotifywait command?
If you want it to get moved straight away then incrontab is the best way to do it and I'd suggest using IN_CLOSE_WRITE so something like:

Code:
/mnt/ceta_invoices_2 IN_CLOSE_WRITE mv $@/$# /mnt/ceta_invoices_3/$#
Good reference material here: https://www.howtoforge.com/tutorial/...s-with-incron/ which despite the title isn't Debian specific.
 
Old 08-23-2017, 10:49 AM   #3
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
inotify was designed for this purpose. You'd have to write a shell script with a while loop running forever. And start this script at system boot. It is quite efficient as a process in wait state does not consume any processor cycles. Once you get the signal you can perform an rsync action. Because you are watching a directory but you don't know which file has changed. That is for rsync to find out and handle.

The inotify man page provides useful examples.

OTOH if you call rsync from cron you have some time between the creation of the file and sync action. Moreover, rsync use is not free. It takes processor and disk cycles. If you run it very frequently (every 5 minutes or so) and your directory contains many files it is inefficient. But calling rsync from cron is quite simple.

So it boils down to simplicity vs. efficiency. (as usual)

jlinkels
 
Old 08-23-2017, 10:52 AM   #4
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
@TenTenths: darn, you posted while I still was writing. Your solution seems to be quite adequate. To bad I did not see it before I started answering!

jlinkels
 
Old 08-23-2017, 11:07 AM   #5
robertkwild
Member
 
Registered: Feb 2015
Posts: 382

Original Poster
Rep: Reputation: Disabled
i dont want to move it i want to copy the data, can it do it
 
Old 08-23-2017, 04:55 PM   #6
robertkwild
Member
 
Registered: Feb 2015
Posts: 382

Original Poster
Rep: Reputation: Disabled
ok i have edited incrontab, my incrontab looks like this -

[root@script ~]# incrontab -l
/source/ IN_CREATE,IN_DELETE,IN_CLOSE_WRITE /usr/bin/rsync /dest/
[root@script ~]# ls /source/
qwerty qwerty.csv
[root@script ~]# ls /dest/
[root@script ~]#

can anyone suggest why it isnt working as it should rsync stuff in source to dest?
 
Old 08-24-2017, 02:45 AM   #7
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Quote:
Originally Posted by robertkwild View Post
ok i have edited incrontab, my incrontab looks like this -

[root@script ~]# incrontab -l
/source/ IN_CREATE,IN_DELETE,IN_CLOSE_WRITE /usr/bin/rsync /dest/
[root@script ~]# ls /source/
qwerty qwerty.csv
[root@script ~]# ls /dest/
[root@script ~]#

can anyone suggest why it isnt working as it should rsync stuff in source to dest?
You're not specifying a source for your rsync command.
If you use IN_CREATE then the event will "fire" when the file is created, before data has finished writing to it so may have unpredictable results. You also never originally mentioned anything about deletion.

Try:
Code:
/source IN_CLOSE_WRITE cp -r $@/$# /dest/$#
/source IN_DELETE rm -f /dest/$#
 
Old 08-24-2017, 03:24 AM   #8
robertkwild
Member
 
Registered: Feb 2015
Posts: 382

Original Poster
Rep: Reputation: Disabled
thanks tentenths, i will try this and get back to you
 
Old 08-24-2017, 04:12 PM   #9
robertkwild
Member
 
Registered: Feb 2015
Posts: 382

Original Poster
Rep: Reputation: Disabled
ok looks like this now -

[root@script ~]# incrontab -l
/source IN_CLOSE_WRITE cp -r $@/$# /dest/$#
/source IN_DELETE rm -f /dest/$#
[root@script ~]# ls /source
qwerty qwerty.csv
[root@script ~]#

you can see i have a file/folder in source but when i ls /dest/ i see nothing in there, i was hoping to see whats in my source
 
Old 08-24-2017, 04:15 PM   #10
robertkwild
Member
 
Registered: Feb 2015
Posts: 382

Original Poster
Rep: Reputation: Disabled
oh wow, it works, didnt copy the files/folders previously in there but when i touch a file now it copies it straight to dest folder

how do i get the previous files/folders copied in there aswell

i have noticed it copies files only not folders, how do i get it to do folders aswell

Last edited by robertkwild; 08-24-2017 at 04:18 PM.
 
Old 08-24-2017, 04:17 PM   #11
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
if those files were there before you set up the incrontab entries then they wouldnt have triggered the relevant event so your jobs wouldnt get activated.
 
Old 08-24-2017, 04:23 PM   #12
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
You'd have to write something to the files to set off the in_close_write

Folders are different, they have to be set up individually
 
Old 08-24-2017, 04:26 PM   #13
robertkwild
Member
 
Registered: Feb 2015
Posts: 382

Original Poster
Rep: Reputation: Disabled
mmm... it doesnt delete from dest the files when i delete the original in source

Last edited by robertkwild; 08-24-2017 at 04:42 PM.
 
Old 08-24-2017, 05:08 PM   #14
robertkwild
Member
 
Registered: Feb 2015
Posts: 382

Original Poster
Rep: Reputation: Disabled
ive sused it, i can only write one incrontab at a time so if i do this

/source IN_CLOSE_WRITE cp -r $@/$# /dest/$#

or this

/source IN_DELETE rm -f $@/$# /dest/$#

it works but when i do this -

/source IN_CLOSE_WRITE cp -r $@/$# /dest/$#
/source IN_DELETE rm -f $@/$# /dest/$#


it doesnt work

any help please

thanks

Last edited by robertkwild; 08-24-2017 at 05:18 PM.
 
Old 08-24-2017, 05:36 PM   #15
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Ok, so if you look at the tutorial I linked to you should be able to work out how to run a script and pass both the "actions" by either word or number

Textually is $%


So you could have something like

Code:
/src IN_CLOSE_WRITE,IN_DELETE /path/to/my/script.sh "$@" "$#" "%%"
Then in your script $1 is the source folder, $2 is the file, and $3 is the action so you could:

Code:
case $3
  IN_CLOSE_WRITE)
    cp blah blah blah
  ;;
  IN_DELETE)
    rm blah blah
  ;;
esac
If that doesn't give you enough of a clue as to how to proceed then let me know and I'll look at it in more detail in the morning when I'm actually at a machine.

Last edited by TenTenths; 08-24-2017 at 05:39 PM. Reason: Had IN_WRITE_CLOSE instead of IN_CLOSE_WRITE, It's late and my eyes are tired :(
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Transmission watch folder not working Vodkaholic1983 Linux - Software 1 05-25-2013 08:45 AM
How to set up public folder/ share folder for my network? Kiwi89 Linux - Server 6 10-31-2011 05:10 AM
rsync local web folder to remote web folder? Almost working... Ultrus Linux - General 2 03-25-2009 02:49 PM
rtorrent watch folder, NFS cormack Linux - Software 2 05-19-2007 01:47 PM
Rhythmbox and a folder to watch? fictos Linux - Software 5 11-17-2005 10:35 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > CentOS

All times are GMT -5. The time now is 09:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration