lsyncd

 

介紹:

Homepage: https://code.google.com/p/lsyncd/

lsyncd 的同步觸發條件預設為 20sec / 累積 1000 次寫入

lsyncd 透過 inotify | fsevents 作為監測目錄寫入的機制

 

Centos6: EPEL

Debian7:

apt-get install lsyncd

File:

  • /usr/bin/lsyncd

Version:

lsyncd -version

Usage:

# default rsync behaviour:

lsyncd [OPTIONS] -rsync SOURCEDIR TARGET ...

# local file:

lsyncd [OPTIONS] -direct SOURCEDIR TARGETDIR ...

# rync+ssh

lsyncd [OPTIONS] -rsyncssh SOURCEDIR TARGETHOST TARGETDIR ...

Note:

under normal configuration Lsyncd will delete pre-existing files in the target directories  that are not present in the respective source directory.

亦即是:

sync {   
   .....
   delete = true
}

每一次 lsyncd 啟動時都會 Full sync 的 !

Thu Jan  9 10:48:27 2014 Normal: recursive startup rsync: /data/ -> /backup/
Thu Jan  9 10:48:27 2014 Normal: Startup of "/data/" finished.

當 rsync "test.bin" 時會有 ".test.bin.WOvBne"

 

設定檔: (lua syntax)

/etc/init.d/lsyncd

CONFIG=/etc/lsyncd/lsyncd.conf.lua

 

/etc/lsyncd/lsyncd.conf.lua

settings = {   
   logfile = "/var/log/lsyncd/lsyncd.log",    
   statusFile = "/var/run/lsyncd.status",
   statusInterval = 1,    
   nodaemon = false,
   inotifyMode = CloseWrite             
}

sync {   
   default.rsync,    
   source = "/data",    
   target = "/backup",
   delay = 10    
}

 

inotifyMode

Default 是 CloseWrite, 另有 Modify

Delay:

If no delay is specified, this means immediate actions for Lsyncd

sync 的方式

  • default.rsync    <-- spawn one Rsync with a filter of all files that changed.
    ( /usr/bin/rsync -ltsd --delete --include-from=- --exclude=* SOURCE TARGET )
     
  • default.rsyncssh
     
  • default.direct
    ( 它會用 /bin/cp, /bin/rm and /bin/mv)

 

default.rsyncssh:

sync {   
   default.rsyncssh,

   source = "/data",    
   target = "192.168.1.2:/data",

   delay = 10,

   rsync = {
       archive  = true,
       compress = true
   }
}

 

exclude

sync {   
   .....
   # matches the file "/bin/foo/bar" !
   # exclude = "foo",
   exclude={ ".*", "*.tmp" },
   # excludeFrom="/etc/lsyncd.exclude"
   .....
}

 

Testing

 

cd /source/

touch file{1..100}

 

log:

Wed Jan  8 17:50:34 2014 Normal: Calling rsync with filter-list of new/modified files/dirs
/file1
/
/file2
/file3
/file4
/file100
Wed Jan  8 17:50:34 2014 Normal: Finished a list = 0

!! 在 log 上是分不到 ADD / DELETE !!

-log <LEVEL>

# Default: Normal,Error

  • scarce   <--  Error
  • all
settings = {
   ......
   log = all,
   }

 

Remark

sysctl fs.inotify.max_user_watches

fs.inotify.max_user_watches = 8192

增加 watches /etc/sysctl.conf

fs.inotify.max_user_watches = 40000

 

inotify_init(2) creates an inotify instance and returns a file
descriptor referring to the inotify instance.

inotify_add_watch(2) manipulates the "watch list" associated with an
inotify instance.  Each item ("watch") in the watch list specifies
the pathname of a file or directory, along with some set of events
that the kernel should monitor for the file referred to by that
pathname.

/proc/sys/fs/inotify/max_user_instances
/proc/sys/fs/inotify/max_user_watches

Creative Commons license icon Creative Commons license icon