{"id":451,"date":"2024-11-27T10:12:16","date_gmt":"2024-11-27T02:12:16","guid":{"rendered":"http:\/\/192.168.5.24\/?p=451"},"modified":"2025-04-30T15:23:48","modified_gmt":"2025-04-30T07:23:48","slug":"rsyncinotifywait-%e5%ae%9e%e7%8e%b0%e6%96%87%e4%bb%b6%e5%a4%b9%e8%bf%9c%e7%a8%8b%e5%ae%9e%e6%97%b6%e5%90%8c%e6%ad%a5","status":"publish","type":"post","link":"https:\/\/bigvip.dpdns.org\/?p=451","title":{"rendered":"rsync+inotifywait \u5b9e\u73b0\u6587\u4ef6\u5939\u8fdc\u7a0b\u5b9e\u65f6\u540c\u6b65"},"content":{"rendered":"\n<p class=\"has-large-font-size\"><strong>\u4e00\u3001\u4ecb\u7ecd<\/strong><\/p>\n\n\n\n<p>    1\u3001rsync<\/p>\n\n\n\n<p>     rsync\uff08Remote Sync\uff0c\u8fdc\u7a0b\u540c\u6b65\uff09\u662f\u4e00\u6b3e\u53ef\u4ee5\u5b9e\u73b0\u5feb\u901f\u589e\u91cf\u5907\u4efd\u7684\u5de5\u5177\u3002\u914d\u5408\u4efb\u52a1\u8ba1\u5212\uff0crsync\u80fd\u5b9e\u73b0\u5b9a\u65f6\u6216\u95f4\u9694\u540c\u6b65\uff0c\u914d\u5408inotify\uff0c\u53ef\u4ee5\u5b9e\u73b0\u89e6\u53d1\u5f0f\u7684\u5b9e\u65f6\u540c\u6b65\u3002rsync\u7cfb\u7edf\u9ed8\u8ba4\u5df2\u7ecf\u5b89\u88c5\u3002<\/p>\n\n\n\n<p>    2\u3001inotifywait<\/p>\n\n\n\n<p>    inotify \u662f\u4e00\u4e2a Linux \u5185\u6838\u7279\u6027\u7531inotify-tools\u5305\u63d0\u4f9b\uff0c\u5b83\u76d1\u63a7\u6587\u4ef6\u7cfb\u7edf\uff0c\u5e76\u4e14\u53ca\u65f6\u5411\u4e13\u95e8\u7684\u5e94\u7528\u7a0b\u5e8f\u53d1\u51fa\u76f8\u5173\u7684\u4e8b\u4ef6\u8b66\u544a\uff0c\u6bd4\u5982\u5220\u9664\u3001\u8bfb\u3001\u5199\u548c\u5378\u8f7d\u64cd\u4f5c\u7b49\u3002    <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> \u5b89\u88c5inotify-tools\uff1a  \n  \n&#91;root@localhost ~]# yum -y install inotify-tools<\/code><\/pre>\n\n\n\n<p class=\"has-large-font-size\"><strong>\u4e8c\u3001\u670d\u52a1\u7aefrsync\u914d\u7f6e<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;root@T1 ~]# grep -Ev \"^$\" \/etc\/rsyncd.conf\n# \/etc\/rsyncd: configuration file for rsync daemon mode\n# See rsyncd.conf man page for more options.\n# configuration example:\nuid = nobody\ngid = nobody\n#use chroot = yes\nmax connections = 4\npid file = \/var\/run\/rsyncd.pid\nexclude = lost+found\/\n#transfer logging = yes\n#timeout = 900\nignore nonreadable = yes\ndont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2\n#hosts allow =192.168.0.0\/16\n&#91;ftp]\n      path = \/var\/log\n#        comment = ftp export area\nauth users=shs\nsecrets file = \/etc\/rsyncd.user\nread only = no\n\n&#91;root@T1 ~]# cat \/etc\/rsyncd.user \nshs:123456abc\n&#91;root@T1 ~]# chmod 600\/etc\/rsyncd.user\n<\/code><\/pre>\n\n\n\n<p class=\"has-large-font-size\"><strong>\u4e09\u3001inotifywait\u4e0ersync\u914d\u5408\u5b9e\u73b0\u5b9e\u65f6\u8fdc\u7a0b\u540c\u6b65<\/strong><\/p>\n\n\n\n<p>rsync \u5e38\u7528\u53c2\u6570\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-a    \u5f52\u6863\n-r    \u9012\u5f52\n-z    \u538b\u7f29\u4f20\u8f93\n-v    \u663e\u793a\u8be6\u7ec6\u4fe1\u606f\n-e    \u8c03\u7528\u8fdc\u7aefsh\n-H    \u4f20\u8f93\u786c\u94fe\u63a5<\/code><\/pre>\n\n\n\n<p>rsync \u8d44\u6e90\u7684\u4e24\u79cd\u8868\u793a\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rsync:\/\/remote_user@remote_host\/sharemode\nremote_user@remote_host::sharemode \n\n\u6ce8\u610f::\u4e0e\u5355\uff1a\u7684\u533a\u522b \n  \u5f53rsync\u4f7f\u7528ssh\u4f5c\u4e3a\u4f20\u8f93\u901a\u9053\u65f6\uff0c\u8c03\u5165ssh\u7684\u8ba4\u8bc1\uff0c\u5e76\u4f7f\u7528\uff1a\u4f5c\u4e3a\u4e3b\u673a\u4e0e\u8def\u5f84\u7684\u5206\u9694\u7b26\uff0c\u5982\uff1a\nrsync -avz -e\"ssh\"   \/path\/to\/localfile  remote_sys_user@remote_host:\/path\/to\/file\n  \u800c::\u662f\u4f5c\u4e3arsync\u4e3b\u673a\u4e0e\u5171\u4eab\u6a21\u5757\u4e4b\u95f4\u7684\u5206\u9694\u7b26\uff0c\u8fd9\u65f6\u5019\u8c03\u7528rsync\u7684\u7528\u6237\u5bc6\u7801\u8fdb\u884c\u8ba4\u8bc1\u3002<\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 inotifywait \u548c rsync \u914d\u5408\u5b9e\u73b0\u5b9e\u65f6\u8fdc\u7a0b\u540c\u6b65\u7684\u793a\u4f8b\u811a\u672c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\n# \u672c\u5730\u76ee\u5f55\nlocal_dir=\"\/path\/to\/local\/directory\"\n\n# \u8fdc\u7a0b\u4e3b\u673a\u4fe1\u606f\nremote_user=\"your_username\"\nremote_host=\"your_remote_host\"\nremote_dir=\"\/path\/to\/remote\/directory\"\nrsyncd_pwd=\"\/path\/to\/rsyncd_pwd\"\n\n# \u76d1\u63a7\u672c\u5730\u76ee\u5f55\u7684\u4fee\u6539\u3001\u521b\u5efa\u548c\u5220\u9664\u4e8b\u4ef6\ninotifywait -mrq --timefmt '%Y-%m-%d %H:%M:%S' --format '%T %w%f %e' \"$local_dir\" | while read date time file event\ndo\n    # \u5f53\u6709\u4e8b\u4ef6\u53d1\u751f\u65f6\uff0c\u6267\u884c rsync \u540c\u6b65\n    rsync -avz --delete --password-file=$rsyncd_pwd \"$local_dir\" \"$remote_user@$remote_host::$remote_dir\"\n    echo \"&#91;$date $time] File: $file, Event: $event\"\ndone<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6ce8\u610f\uff1a<\/h3>\n\n\n\n<p>1\u3001rsync\u670d\u52a1\u5668\u7aef\u542f\u7528\u5bc6\u7801\u8ba4\u8bc1\u65f6\uff0c\u5bc6\u7801\u6587\u4ef6\u7684\u6743\u9650\u5fc5\u987b\u8bbe\u7f6e\u4e3a600\uff0c\u5426\u5219\u5ba2\u6237\u7aef\u65e0\u6cd5\u767b\u5f55\u3002<\/p>\n\n\n\n<p>2\u3001rsync\u5ba2\u6237\u7aef\u4f7f\u7528\u5bc6\u7801\u6587\u4ef6\u767b\u5f55\u65f6\uff0c\u5bc6\u7801\u6587\u4ef6\u7684\u6743\u9650\u5fc5\u987b\u8bbe\u7f6e\u4e3a600\u3002\u5bc6\u7801\u6587\u4ef6\u5185\u5bb9\u76f4\u63a5\u5199\u5bc6\u7801\u5373\u53ef\u3002<\/p>\n\n\n\n<p>3\u3001rsync\u5171\u4eab\u6587\u4ef6\u5939\u9ed8\u8ba4\u4e3a\u53ea\u8bfb\u6a21\u5f0f\uff0c\u5982\u9700\u8981\u5199\u5165\u6587\u4ef6\u9700\u8981\u5728\u5171\u4eab\u6a21\u5757\u8bbe\u7f6e\u4e0b\u4fee\u6539\uff1aread only = no<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u4ecb\u7ecd 1\u3001rsync rsync\uff08Remote Sync\uff0c\u8fdc\u7a0b\u540c\u6b65\uff09\u662f\u4e00\u6b3e\u53ef\u4ee5\u5b9e\u73b0\u5feb\u901f\u589e\u91cf\u5907\u4efd\u7684\u5de5\u5177\u3002\u914d\u5408 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"footnotes":""},"categories":[20,36],"tags":[76,75],"class_list":["post-451","post","type-post","status-publish","format-aside","hentry","category-linux","category-36","tag-inotify","tag-rsync","post_format-post-format-aside"],"_links":{"self":[{"href":"https:\/\/bigvip.dpdns.org\/index.php?rest_route=\/wp\/v2\/posts\/451","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bigvip.dpdns.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bigvip.dpdns.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bigvip.dpdns.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bigvip.dpdns.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=451"}],"version-history":[{"count":15,"href":"https:\/\/bigvip.dpdns.org\/index.php?rest_route=\/wp\/v2\/posts\/451\/revisions"}],"predecessor-version":[{"id":1137,"href":"https:\/\/bigvip.dpdns.org\/index.php?rest_route=\/wp\/v2\/posts\/451\/revisions\/1137"}],"wp:attachment":[{"href":"https:\/\/bigvip.dpdns.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bigvip.dpdns.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bigvip.dpdns.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}