{"id":156097,"date":"2019-08-29T08:10:30","date_gmt":"2019-08-29T00:10:30","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=156097"},"modified":"2019-08-19T10:12:06","modified_gmt":"2019-08-19T02:12:06","slug":"remote-synchronization-via-rsync","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/remote-synchronization-via-rsync.html","title":{"rendered":"\u901a\u8fc7rsync\u5b9e\u73b0\u8fdc\u7a0b\u540c\u6b65"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u6b63\u786e\u3001\u6709\u6548\u7684\u5907\u4efd\u65b9\u6848\u662f\u4fdd\u969c\u7cfb\u7edf\u53ca\u6570\u636e\u5b89\u5168\u7684\u91cd\u8981\u624b\u6bb5\uff0c\u5728\u670d\u52a1\u5668\u4e2d\uff0c\u901a\u5e38\u4f1a\u7ed3\u5408\u8ba1\u5212\u4efb\u52a1\u3001Shell\u811a\u672c\u6765\u6267\u884c\u672c\u5730\u5907\u4efd\uff0c\u4e3a\u4e86\u8fdb\u4e00\u6b65\u63d0\u9ad8\u5907\u4efd\u7684\u53ef\u9760\u6027\uff0c\u4f7f\u7528\u5f02\u5730\u5907\u4efd\u4e5f\u662f\u975e\u5e38\u6709\u5fc5\u8981\u7684\u3002\u6bd4\u5982\u9488\u5bf9web\u7ad9\u70b9\u7684\u540c\u6b65\u5907\u4efd\uff0c\u5982\u679c\u60f3\u8981\u5b9e\u73b0\u5feb\u901f\u3001\u5b89\u5168\u3001\u9ad8\u6548\u7684\u5f02\u5730\u5907\u4efd\uff0c\u5c31\u9700\u8981\u4f7f\u7528\u5230\u2014\u2014rsync\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u4e00\u3001rsync\u7b80\u4ecb<\/strong><\/div>\n

rsync\uff08Remote Sync\uff0c\u8fdc\u7a0b\u540c\u6b65\uff09\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u5feb\u901f\u5907\u4efd\u5de5\u5177\uff0c\u53ef\u4ee5\u5728\u4e0d\u540c\u4e3b\u673a\u4e4b\u95f4\u955c\u50cf\u540c\u6b65\u6574\u4e2a\u76ee\u5f55\u6811\uff0c\u652f\u6301\u589e\u91cf\u5907\u4efd\uff0c\u4fdd\u6301\u94fe\u63a5\u548c\u6743\u9650\uff0c\u4e14\u91c7\u7528\u4f18\u5316\u7684\u540c\u6b65\u7b97\u6cd5\uff0c\u4f20\u8f93\u524d\u6267\u884c\u538b\u7f29\uff0c\u56e0\u6b64\u975e\u5e38\u9002\u7528\u4e8e\u5f02\u5730\u5907\u4efd\u3001\u955c\u50cf\u670d\u52a1\u5668\u7b49\u5e94\u7528\u3002<\/p>\n

rsync\u7684\u5b98\u65b9\u7ad9\u70b9\uff1ahttp:\/\/rsync.samba.org\/ \uff0c\u76ee\u524d\u6700\u65b0\u7248\u672c\u662f3.1.3\uff0c\u7531Wayne Davison.\u8fdb\u884c\u7ef4\u62a4\uff0c\u4f5c\u4e3a\u4e00\u79cd\u6700\u5e38\u7528\u7684\u6587\u4ef6\u5907\u4efd\u5de5\u5177\uff0crsync\u5f80\u5f80\u662fLinux\u548cUNIX\u7cfb\u7edf\u9ed8\u8ba4\u5b89\u88c5\u7684\u57fa\u672c\u7ec4\u4ef6\u4e4b\u4e00\u3002<\/p>\n

rsync\u662f\u4e00\u6b3e\u5feb\u901f\u589e\u91cf\u5907\u4efd\u5de5\u5177\uff0c\u652f\u6301\uff1a<\/p>\n

\uff081\uff09\u672c\u5730\u590d\u5236\uff1b\r\n\uff082\uff09\u4e0e\u5176\u4ed6SSH\u540c\u6b65\uff1b\r\n\uff083\uff09\u4e0ersync\u4e3b\u673a\u540c\u6b65\u3002\r\n<\/pre>\n

\u5728\u8fdc\u7a0b\u540c\u6b65\u4efb\u52a1\u4e2d\uff0c\u8d1f\u8d23\u53d1\u8d77rsync\u540c\u6b65\u64cd\u4f5c\u7684\u5ba2\u6237\u673a\u79f0\u4e3a\u53d1\u8d77\u7aef\uff0c\u800c\u8d1f\u8d23\u76f8\u5e94\u6765\u81ea\u5ba2\u6237\u673a\u7684rsync\u540c\u6b65\u64cd\u4f5c\u7684\u670d\u52a1\u5668\u79f0\u4e3a\u540c\u6b65\u6e90\u3002\u5728\u540c\u6b65\u8fc7\u7a0b\u4e2d\uff0c\u540c\u6b65\u6e90\u8d1f\u8d23\u63d0\u4f9b\u6587\u6863\u7684\u539f\u59cb\u4f4d\u7f6e\uff0c\u53d1\u8d77\u7aef\u5e94\u5bf9\u8be5\u4f4d\u7f6e\u6709\u8bfb\u53d6\u6743\u9650\u3002\u5982\u56fe\uff1a
\n\"\"<\/p>\n

\u4e8c\u3001\u914d\u7f6ersync\u6e90<\/strong><\/div>\n
\u914d\u7f6ersync\u6e90\u670d\u52a1\u5668\u5927\u81f4\u5206\u4e3a\u4e09\u6b65\uff1a\r\n\uff081\uff09\u5efa\u7acbrsync\u914d\u7f6e\u6587\u4ef6\uff1b\r\n\uff082\uff09\u4e3a\u5907\u4efd\u8d26\u6237\u521b\u5efa\u6570\u636e\u6587\u4ef6\uff1b\r\n\uff083\uff09\u542f\u52a8rsync\u670d\u52a1\u3002\r\n<\/pre>\n
\uff081\uff09\u5efa\u7acbrsync\u914d\u7f6e\u6587\u4ef6<\/strong><\/span><\/div>\n

\u5728CentOS 7\u7cfb\u7edf\u4e4b\u524d\/etc\/rsyncd.conf\u6587\u4ef6\u9ed8\u8ba4\u4e0d\u5b58\u5728\uff0cCentOS 7\u5f00\u59cb\u5df2\u7ecf\u6709\u8fd9\u6837\u7684\u6587\u4ef6\uff0c\u5e76\u4e14\u5176\u4e2d\u653e\u7f6e\u4e86\u6a21\u677f\u53c2\u8003\u4fe1\u606f\uff08\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u7f16\u5199\u9700\u8981\u7684\u5185\u5bb9\uff0c\u6ce8\u610f\u683c\u5f0f\uff09\u3002<\/p>\n

[root@localhost ~]# vim \/etc\/rsyncd.conf \r\nuid = nobody                                                                \/\/\u542f\u7528\u533f\u540d\u7528\u6237\r\ngid = nobody                                                               \r\nuse chroot = yes                                                          \/\/\u7981\u9522\u5728\u6e90\u76ee\u5f55\r\naddress = 192.168.1.1                                                \/\/\u76d1\u542c\u5730\u5740\r\nport 873                                                                      \/\/\u76d1\u542c\u7aef\u53e3\r\nlog file = \/var\/log\/rsyncd.log                                       \/\/\u65e5\u5fd7\u6587\u4ef6\u5b58\u653e\u4f4d\u7f6e\r\npid file = \/var\/run\/rsyncd.pid                                      \/\/\u5b58\u653e\u8fdb\u7a0bID\u7684\u6587\u4ef6\u4f4d\u7f6e\r\nhosts allow = 192.168.1.0\/24                                    \/\/\u5141\u8bb8\u8bbf\u95ee\u7684\u5ba2\u6237\u673a\u5730\u5740\r\n[wwwroot]                                                                  \/\/\u5171\u4eab\u6a21\u5757\u540d\u79f0\r\n        path = \/var\/www\/html                                        \/\/\u6e90\u76ee\u5f55\u7684\u5b9e\u9645\u8def\u5f84\r\n        comment = aaa                                                 \/\/\u63cf\u8ff0\uff08\u53ef\u4ee5\u7701\u7565\uff09\r\n        read only = no                                                   \/\/\u662f\u5426\u4e3a\u53ea\u8bfb\r\n        dont compress = *.gz *.bz2 *.rar *.zip               \/\/\u540c\u6b65\u65f6\u4e0d\u518d\u538b\u7f29\u7684\u6587\u4ef6\u7c7b\u578b\r\n        auth users = backuper                                      \/\/\u6388\u6743\u8d26\u6237\r\n        secrets file = \/etc\/rsyncd_users.db                   \/\/\u5b58\u653e\u8d26\u6237\u4fe1\u606f\u7684\u6570\u636e\u6587\u4ef6\r\n<\/pre>\n

\u57fa\u4e8e\u5b89\u5168\u6027\u8003\u8651\uff0c\u5bf9\u4e8ersync\u7684\u540c\u6b65\u6e90\u6700\u597d\u4ec5\u5141\u8bb8\u4ee5\u53ea\u8bfb\u65b9\u5f0f\u505a\u540c\u6b65\u3002\u53e6\u5916\uff0c\u540c\u6b65\u53ef\u4ee5\u91c7\u7528\u533f\u540d\u7684\u65b9\u5f0f\uff0c\u53ea\u8981\u5c06\u5176\u4e2d\u7684\u201cauth users\u201d\u548c\u201csecrets file\u201d\u914d\u7f6e\u9879\u53bb\u9664\u5373\u53ef\uff01<\/p>\n

\uff082\uff09\u4e3a\u5907\u4efd\u8d26\u53f7\u521b\u5efa\u6570\u636e\u6587\u4ef6<\/strong><\/span><\/div>\n

\u6839\u636ersync\u7684\u914d\u7f6e\u6587\u4ef6\u5185\u5bb9\uff0c\u521b\u5efa\u8d26\u53f7\u6570\u636e\u6587\u4ef6\u3002\u6bcf\u884c\u4e00\u4e2a\u7528\u6237\uff0c\u7528\u6237\u548c\u5bc6\u7801\u4e4b\u95f4\u7528\u5192\u53f7\u8fdb\u884c\u5206\u9694\u3002<\/p>\n

[root@localhost ~]# vim \/etc\/rsyncd_users.db\r\nbackuper:123456\r\n<\/pre>\n

\u7531\u4e8e\u8d26\u53f7\u4fe1\u606f\u91c7\u7528\u660e\u6587\u5b58\u653e\uff0c\u56e0\u6b64\u9700\u8981\u8c03\u6574\u6587\u4ef6\u6743\u9650\uff0c\u907f\u514d\u8d26\u53f7\u4fe1\u606f\u6cc4\u9732\u3002<\/p>\n

[root@localhost ~]# chmod 600 \/etc\/rsyncd_users.db\r\n<\/pre>\n

\u5907\u4efd\u7528\u6237\u5e94\u5bf9\u6e90\u76ee\u5f55\u5177\u6709\u8bfb\u53d6\u6743\u9650\u3002<\/p>\n

[root@localhost ~]# ls -ld \/var\/www\/html\r\ndrwxr-xr-x. 2 root root 6 11\u6708 15 2016 \/var\/www\/html\r\n<\/pre>\n
\uff083\uff09\u542f\u52a8rsync\u670d\u52a1<\/strong><\/span><\/div>\n
[root@localhost ~]# rsync --daemon\r\n[root@localhost ~]# netstat -anpt | grep rsync\r\ntcp        0      0 192.168.1.1:873         0.0.0.0:*               LISTEN      44001\/rsync\r\n<\/pre>\n

\u5982\u679c\u9700\u8981\u91cd\u542frsync\u670d\u52a1\uff0c\u9700\u8981\uff1a<\/p>\n

[root@localhost ~]# kill  $(cat \/var\/run\/rsyncd.pid)\r\n\/\/\u505c\u6b62\u670d\u52a1\r\n[root@localhost ~]# rsync --daemon\r\n\/\/\u542f\u52a8\u670d\u52a1\r\n[root@localhost ~]# kill -9 $(cat \/var\/run\/rsyncd.pid)\r\n<\/pre>\n

\u6216\u8005\u76f4\u63a5\u4f7f\u7528\u201cnetstat -anpt | grep rsync\u201d\u547d\u4ee4\u67e5\u51fa\u8fdb\u7a0b\u53f7\uff0c\u4f7f\u7528\u201ckill \u8fdb\u7a0b\u53f7\u201d\u4e00\u6837\u3002
\n\u4f7f\u7528\u7b2c\u4e00\u79cd\u65b9\u6cd5\u505c\u6b62rsync\u670d\u52a1\u5fc5\u987b\u5220\u9664\u5b58\u653ersync\u670d\u52a1\u8fdb\u7a0b\u7684\u6587\u4ef6\uff1a<\/p>\n

[root@localhost ~]# rm -rf \/var\/run\/rsyncd.pid\r\n<\/pre>\n
\u4e09\u3001\u4f7f\u7528rsync\u5907\u4efd\u5de5\u5177<\/strong><\/div>\n

\u914d\u7f6e\u597drsync\u540c\u6b65\u6e90\u670d\u52a1\u5668\u4e4b\u540e\uff0c\u5ba2\u6237\u7aef\u5c31\u53ef\u4ee5\u4f7f\u7528rsync\u5de5\u5177\u6765\u6267\u884c\u8fdc\u7a0b\u540c\u6b65\u4e86\u3002<\/p>\n

rsync\u547d\u4ee4\u7684\u9009\u9879\uff1a\r\n-r\uff1a\u9012\u5f52\u6a21\u5f0f\uff0c\u5305\u542b\u76ee\u5f55\u53ca\u5b50\u76ee\u5f55\u4e2d\u6240\u6709\u6587\u4ef6\r\n-l\uff1a\u5bf9\u4e8e\u7b26\u53f7\u94fe\u63a5\u6587\u4ef6\u4ecd\u7136\u590d\u5236\u4e3a\u7b26\u53f7\u94fe\u63a5\u6587\u4ef6\r\n-p\uff1a\u4fdd\u7559\u6587\u4ef6\u7684\u6743\u9650\u6807\u8bb0\r\n-t\uff1a\u4fdd\u7559\u6587\u4ef6\u7684\u65f6\u95f4\u6807\u8bb0\r\n-g\uff1a\u4fdd\u7559\u6587\u4ef6\u7684\u5c5e\u7ec4\u6807\u8bb0\uff08\u4ec5\u8d85\u7ea7\u7528\u6237\u4f7f\u7528\uff09\r\n-o\uff1a\u4fdd\u7559\u6587\u4ef6\u7684\u5c5e\u4e3b\u6807\u8bb0\uff08\u4ec5\u8d85\u7ea7\u7528\u6237\u4f7f\u7528\uff09\r\n-D\uff1a\u4fdd\u7559\u8bbe\u5907\u6587\u4ef6\u53ca\u5176\u4ed6\u7279\u6b8a\u6587\u4ef6\r\n-a\uff1a\u5f52\u6863\u6a21\u5f0f\uff0c\u9012\u5f52\u5e76\u4fdd\u7559\u5bf9\u8c61\u5c5e\u6027\uff0c\u7b49\u540c\u4e8e -rlptgoD\r\n-v\uff1a\u663e\u793a\u540c\u6b65\u8fc7\u7a0b\u7684\u8be6\u7ec6\uff08verbose\uff09\u4fe1\u606f\r\n-z\uff1a\u5728\u4f20\u8f93\u6587\u4ef6\u65f6\u8fdb\u884c\u538b\u7f29\uff08compress\uff09\r\n-H\uff1a\u4fdd\u7559\u786c\u8fde\u63a5\u6587\u4ef6\r\n-A\uff1a\u4fdd\u7559ACL\u5c5e\u6027\u4fe1\u606f\r\n--delete\uff1a\u5220\u9664\u76ee\u6807\u4f4d\u7f6e\u6709\u800c\u539f\u59cb\u4f4d\u7f6e\u6ca1\u6709\u7684\u6587\u4ef6\r\n--checksum\uff1a\u6839\u636e\u5bf9\u8c61\u7684\u6821\u9a8c\u548c\u6765\u51b3\u5b9a\u662f\u5426\u8df3\u8fc7\u6587\u4ef6\r\n<\/pre>\n

rsync\u662f\u4e00\u6b3e\u5feb\u901f\u589e\u91cf\u5907\u4efd\u5de5\u5177\uff0c\u652f\u6301\uff1a
\n\uff081\uff09\u672c\u5730\u590d\u5236\uff1b
\n\uff082\uff09\u4e0e\u5176\u4ed6SSH\u540c\u6b65\uff1b
\n\uff083\uff09\u4e0ersync\u4e3b\u673a\u540c\u6b65\u3002<\/p>\n

\uff081\uff09\u672c\u5730\u590d\u5236<\/strong><\/span><\/div>\n
[root@localhost ~]# rsync \/etc\/passwd 123.txt\r\n\/\/\u7c7b\u4f3c\u4e8ecp\u547d\u4ee4\r\n<\/pre>\n
\uff082\uff09\u4e0e\u5176\u4ed6SSH\u540c\u6b65<\/strong><\/span><\/div>\n
[root@localhost ~]# rsync -av root@192.168.1.2:\/root\/123.txt .\r\nroot@192.168.1.2's password: \r\n<\/pre>\n
\uff083\uff09\u4e0ersync\u4e3b\u673a\u540c\u6b65<\/strong><\/span><\/div>\n
[root@localhost ~]# rsync -avz backuper@192.168.1.1::wwwroot \/root\r\n\u6216\u8005\r\n[root@localhost ~]# rsync -avz rsync:\/\/backuper@192.168.1.1\/wwwroot \/root\r\n<\/pre>\n

\u8fd9\u4e24\u79cd\u547d\u4ee4\u6548\u679c\u662f\u4e00\u6837\uff01
\n\u4e0a\u4f20\u53ea\u9700\u5c06\u76ee\u5f55\u8c03\u6362\u987a\u5e8f\u5373\u53ef\uff08\u786e\u4fdd\u5bf9\u4e0a\u4f20\u7684\u76ee\u5f55\u5177\u6709\u5199\u5165\u6743\u9650\uff09\uff01
\n\u5728\u540c\u6b65\u6e90\u7aef\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff0c\u65b9\u53ef\u6267\u884c\u5199\u5165\u6743\u9650<\/p>\n

[root@localhost ~]# chmod o+w \/var\/www\/html\r\n[root@localhost ~]# ls -ld \/var\/www\/html\r\ndrwxr-xrwx. 2 root root 6 8\u6708  17 16:47 \/var\/www\/html\r\n[root@localhost ~]# rsync -avz \/root backuper@192.168.1.1::wwwroot \r\n<\/pre>\n

\u4f46\u662f\u5728\u73b0\u5b9e\u5de5\u4f5c\u73af\u5883\u4e2d\uff0c\u5907\u4efd\u5de5\u4f5c\u901a\u5e38\u662f\u6309\u8ba1\u5212\u91cd\u590d\u6267\u884c\u7684\uff0c\u6bd4\u5982\uff1a<\/p>\n

[root@localhost ~]# vim \/root\/123.pass\r\n123456\r\n\/\/\u4efb\u610f\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\u7528\u4e8e\u5b58\u653ersync\u6388\u6743\u7528\u6237\u7684\u5bc6\u7801\u4fe1\u606f\r\n[root@localhost ~]# chmod 600 \/root\/123.pass\r\n\/\/\u5fc5\u987b\u8bbe\u5b9a600\u6743\u9650\uff0c\u5426\u5219\u6267\u884c\u65f6\u5c06\u4f1a\u62a5\u9519\r\n[root@localhost ~]# crontab -e\r\n\/\/\u521b\u5efa\u8ba1\u5212\u4efb\u52a1\r\n30 22 * * * \/usr\/bin\/rsync -az --delete --password-file=\/root\/123.pass backuper@192.168.1.1::wwwroot \/a\r\n\/\/\u6bcf\u5929\u665a\u4e0a22\uff1a30\u6267\u884c\u811a\u672c\r\n[root@localhost ~]# systemctl restart crond\r\n\/\/\u91cd\u65b0\u542f\u52a8crond\u670d\u52a1\r\n<\/pre>\n

\u5173\u4e8ecrond\u8ba1\u5212\u4efb\u52a1\u914d\u7f6e\u6587\u4ef6\u7684\u683c\u5f0f\uff08\u4ece\u4e0a\u5230\u4e0b\uff09\uff1a
\n\"\"<\/p>\n","protected":false},"excerpt":{"rendered":"

rsync\uff08Remote Sync\uff0c\u8fdc\u7a0b\u540c\u6b65\uff09\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u5feb\u901f\u5907\u4efd\u5de5\u5177\uff0c\u53ef\u4ee5\u5728\u4e0d\u540c\u4e3b\u673a\u4e4b\u95f4\u955c\u50cf\u540c\u6b65\u6574\u4e2a\u76ee\u5f55\u6811\uff0c […]<\/p>\n","protected":false},"author":1329,"featured_media":156104,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-156097","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/156097","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1329"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=156097"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/156097\/revisions"}],"predecessor-version":[{"id":156105,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/156097\/revisions\/156105"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/156104"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=156097"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=156097"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=156097"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}