linux实时备份,51CTO博客-专业IT技术博客创作平台-技术成就梦想
rsync缺點/不足:
1.rsync在同步數據時,需要掃描所有文件后進行比對,進行差量傳輸。如果文件數量達到了百萬甚至千萬量級,掃描所有文件將是非常耗時的,并且正在發生變化的往往是其中很少的一部分,這是非常低效的方式。
2.rsync不能實時的去監測、同步數據,雖然它可以通過linux守護進程的方式進行觸發同步,但是兩次觸發動作一定會有時間差,這樣就導致了服務端和客戶端數據可能出現不一致,無法在應用故障時完全的恢復數據。linux內核從2.6.13起,加入了inotify支持,通過inotify可以監控文件系統中添加、刪除、修改、移動等各種事件,利用這個內核接口,第三方軟件就可以監控文件系統下文件的各種變化情況,而inotify-tools正是實施監控的軟件。
在使用rsync首次全量同步后,結合inotify對源目錄進行實時監控,只有有文件變動或新文件產生,就會立刻同步到目標目錄下,非常高效使用!
inotify :創建一個文件描述符,附加一個或多個監視器(一個監視器是一個路徑和一組事件),然后用read方法從描述符獲取事件,read并不會用完整個周期,它是事件發生之前是被阻塞的。
文件描述符:linux內核為了更優秀的管理被打開的文件創建的索引,是一個非負整數。用于指代被打開的文件,所有執行io的操作 系統調用都是通過文件描述符。0表示標準輸入,1表示標準輸出,2表示標準錯誤輸出。
查看linux內核版本
需要看當前linux是否支持inotify[root@Rsync-139?~]#?uname?-a
Linux?Rsync-139?2.6.32-431.el6.x86_64?#1?SMP?Fri?Nov?22?03:15:09?UTC?2013?x86_64?x86_64?x86_64?GNU/Linux
[root@Rsync-139?~]#?ls?-lsart?/proc/sys
sys/???????????sysrq-trigger??sysvipc/
[root@Rsync-139?~]#?ls?-lsart?/proc/sys/fs/inotify/
總用量?0
0?dr-xr-xr-x?0?root?root?0?11月??5?15:31?..
0?dr-xr-xr-x?0?root?root?0?11月??6?16:12?.
0?-rw-r--r--?1?root?root?0?11月??6?16:12?max_user_watches
0?-rw-r--r--?1?root?root?0?11月??6?16:12?max_user_instances
0?-rw-r--r--?1?root?root?0?11月??6?16:12?max_queued_events
安裝[root@Rsync-139?~]#?tar?-zxvf?inotify-tools-3.14.tar.gz?-C?/usr/local/src/
[root@Rsync-139?~]#?./configrue?--prefix=/usr/local/inotify
[root@Rsync-139?~]#?make?&&make?install
需要加環境變量vim?/etc/profile
最低行加:/usr/local/inotify/bin/
重新加載配置文件.?/etc/profile?或者
source?/etc/profile
查看幫助信息
inotifywait --help[root@WebA-136?script]#?inotifywait?--help
inotifywait?3.14
Wait?for?a?particular?event?on?a?file?or?set?of?files.
Usage:?inotifywait?[?options?]?file1?[?file2?]?[?file3?]?[?...?]
Options:可用選項
-h|--help???????Show?this?help?text.
@?????????Exclude?the?specified?file?from?being?watched.
--exclude?
Exclude?all?events?on?files?matching?the
extended?regular?expression?.
--excludei?
Like?--exclude?but?case?insensitive.
-m|--monitor????Keep?listening?for?events?forever.??Without
this?option,?inotifywait?will?exit?after?one
event?is?received.
-d|--daemon?????Same?as?--monitor,?except?run?in?the?background
logging?events?to?a?file?specified?by?--outfile.
Implies?--syslog.
-r|--recursive??Watch?directories?recursively.
--fromfile?
Read?files?to?watch?from??or?`-'?for?stdin.
-o|--outfile?
Print?events?to??rather?than?stdout.
-s|--syslog?????Send?errors?to?syslog?rather?than?stderr.
-q|--quiet??????Print?less?(only?print?events).
-qq?????????????Print?nothing?(not?even?events).
--format???Print?using?a?specified?printf-like?format
string;?read?the?man?page?for?more?details.
--timefmt??strftime-compatible?format?string?for?use?with
%T?in?--format?string.
-c|--csv????????Print?events?in?CSV?format.
-t|--timeout?
When?listening?for?a?single?event,?time?out?after
waiting?for?an?event?for??seconds.
If??is?0,?inotifywait?will?never?time?out.
-e|--event??[?-e|--event??...?]
Listen?for?specific?event(s).??If?omitted,?all?events?are
listened?for.
Exit?status:可以監控的事件
0??-??An?event?you?asked?to?watch?for?was?received.
1??-??An?event?you?did?not?ask?to?watch?for?was?received
(usually?delete_self?or?unmount),?or?some?error?occurred.
2??-??The?--timeout?option?was?given?and?no?events?occurred
in?the?specified?interval?of?time.
Events:
access??????????file?or?directory?contents?were?read
modify??????????file?or?directory?contents?were?written
attrib??????????file?or?directory?attributes?changed
close_write?????file?or?directory?closed,?after?being?opened?in
writeable?mode
close_nowrite???file?or?directory?closed,?after?being?opened?in
read-only?mode
close???????????file?or?directory?closed,?regardless?of?read/write?mode
open????????????file?or?directory?opened
moved_to????????file?or?directory?moved?to?watched?directory
moved_from??????file?or?directory?moved?from?watched?directory
move????????????file?or?directory?moved?to?or?from?watched?directory
create??????????file?or?directory?created?within?watched?directory
delete??????????file?or?directory?deleted?within?watched?directory
delete_self?????file?or?directory?was?deleted
unmount?????????file?system?containing?file?or?directory?unmounted
常用
-r 遞歸(目錄)
-m 永久監聽
-d 后臺運行
-q 靜默,只輸出較少的信息
編寫腳本進行實時備份vim?inotify.sh
inotifywait?-mrq?--timefmt?'%d%m%y?%H:%M'?--format?'%T?%w%f%e'?-e?close_write,modify,delete,create,attrib,move?/var/log/?|while?read?file
do
rsync?-az?--delete-before?/var/log/?rsync_WebA@192.168.146.139::WebA/?--password-file=/etc/WebA.pass
done
./inotify?&放入后臺執行即可
注:
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at
main.c(1505)是因為在同步的時候,源目錄下有軟鏈接文件!
rsync同步軟鏈接文件,應該加參數-l
總結
以上是生活随笔為你收集整理的linux实时备份,51CTO博客-专业IT技术博客创作平台-技术成就梦想的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux 生成字母序列,Python序
- 下一篇: linux专业术语中英文,Linux专业