{"id":2357,"date":"2013-07-23T23:18:46","date_gmt":"2013-07-23T15:18:46","guid":{"rendered":"http:\/\/blog.pighead.cc\/whsyu\/?p=2357"},"modified":"2014-01-15T10:11:31","modified_gmt":"2014-01-15T02:11:31","slug":"rsync-server-over-ssh","status":"publish","type":"post","link":"https:\/\/blog.pighead.cc\/whsyu\/2013\/07\/23\/rsync-server-over-ssh\/","title":{"rendered":"rsync server over ssh"},"content":{"rendered":"<p class=\"bb-post-separator\"><strong>[\u83ef\u8a9e, cmn-Hant-TW]  <\/strong><\/p><p>\u4e4b\u524d\u5728<a title=\"zfs snapshot \u7ba1\u7406\" href=\"http:\/\/blog.pighead.cc\/whsyu\/2012\/07\/30\/zfs-snapshot-management\/\">\u9019\u7bc7<\/a>\u6709\u63d0\u5230 snapshot \u7ba1\u7406\u4e3b\u8981\u662f\u7528\u5728 backup \u4e0a\uff0c\u585e\u6771\u897f\u5230\u5099\u4efd\u4f3a\u670d\u5668\u4e0a\u6700\u7c21\u55ae\u7684\u65b9\u5f0f\u5c31\u662f rsync\uff0c\u4e4b\u524d\u4e3b\u8981\u662f\u7528 rsyncd\uff0c\u4f46\u662f rsyncd \u6709\u5e7e\u500b\u554f\u984c\uff0c\u9996\u5148\u662f\u50b3\u8f38\u672a\u52a0\u5bc6\uff0c\u5982\u679c\u7d93\u904e\u516c\u5171\u7db2\u8def\u5340\u9593\u6709\u9ede\u4e0d\u5b89\u5168\uff1b\u518d\u4f86\u5c31\u662f\u5404\u53f0\u5099\u4efd\u7684\u8a2d\u5b9a\u6a94\u4e0d\u5c0f\u5fc3\u5f04\u6210\u4e00\u6a23\u7684\u8a71\uff0c\u6703\u5f88\u641e\u7b11\u7684\u4e0d\u5c0f\u5fc3\u540c\u6b65\u5230\u53e6\u4e00\u53f0\u6a5f\u5668\u7528\u7684\u5099\u4efd\u5340\u57df (\u53ef\u4ee5\u7528\u9396 IP \u4f86\u8655\u7406\uff0c\u4e0d\u904e\u6d6e\u52d5 IP \u7684\u6a5f\u5668\u5c31\u9ebb\u7169\u4e86)\u3002\u6240\u4ee5\u5f8c\u4f86\u5c31\u7814\u7a76\u4e86\u4e00\u4e0b\u6709\u6c92\u6709\u9069\u7576\u89e3\uff0c\u7d50\u679c\u767c\u73fe\u6bd4\u8f03\u7c21\u55ae\u7684\u65b9\u5f0f\u9084\u662f\u7528 rsync + ssh\uff0c\u7136\u5f8c\u900f\u904e ssh\u00a0authorized_keys \u7684\u6a5f\u5236\u9054\u5230\u4e0d\u7528\u8f38\u5165\u5bc6\u78bc\u7684\u6548\u679c\uff0c\u6a5f\u5668\u7aef\u7684\u6307\u4ee4\u9577\u9019\u6a23<\/p>\n<pre lang=\"bash\">\r\nexport BACKUP_ARGS=\"-avHS --ignore-errors\"\r\nrsync $BACKUP_ARGS -e \"\/usr\/bin\/ssh -o UserKnownHostsFile=\/dev\/null -o StrictHostKeyChecking=no -o LogLevel=ERROR -o SendEnv=BACKUP_ARGS -i $KEY\" \/ $USER@$HOST:\r\n<\/pre>\n<p>\u5176\u4e2d -o \u53c3\u6578\u9664\u4e86 SendEnv \u5916\uff0c\u90fd\u662f\u70ba\u4e86\u907f\u514d\u52a0\u5165 known_hosts \u63d0\u793a\u7528\u7684\uff0c\u4e0d\u52a0\u4e5f\u7121\u59a8\uff0c$BACKUP_ARGS \u662f rsync \u7684\u53c3\u6578\uff0c\u9019\u500b\u4f8b\u5b50\u5c31\u662f\u5e38\u898b\u7684 -avHS \u4ee5\u53ca &#8211;ignore-errors\uff0c$USER \u662f\u4f7f\u7528\u8005\uff0c\u70ba\u4e86\u65b9\u4fbf\u901a\u5e38\u90fd\u662f root\uff0c\u7136\u5f8c $HOST \u5247\u662f\u5099\u4efd\u4f3a\u670d\u5668\u7684\u4f4d\u7f6e\uff0c$KEY \u7684\u8a71\u5247\u662f\u7531 ssh-keygen \u7522\u751f\u51fa\u4f86 .key \u6a94\u7684\u4f4d\u7f6e\uff0c\u53e6\u5916\u96a8\u9644\u7684 .pub \u8981\u628a\u4ed6\u8cbc\u5230\u5099\u4efd\u4f3a\u670d\u5668\u7aef $USER \u5bb6\u76ee\u9304\u4e0b\u7684 .ssh\/authorized_keys \u88e1\u9762\uff0c\u7136\u5f8c\u5728 ssh-rsa \u524d\u9762\u52a0\u4e0acommand\u6307\u4ee4\uff0c\u9577\u5f97\u50cf\u9019\u6a23<\/p>\n<pre lang=\"bash\">command=\"\/usr\/local\/bin\/rsync --server $BACKUP_ARGS . \/backup\/318\" ssh-rsa AAAA...c87 root@ck49th318<\/pre>\n<p>\u900f\u904e rsync \u7684 &#8211;server \u53c3\u6578\u5c31\u53ef\u4ee5\u5728\u4f3a\u670d\u5668\u63a5\u6536\u6a94\u6848\u4e86\uff0c\u4ee5\u9019\u884c\u4f86\u8b1b\uff0c\u6700\u5f8c\u7684 \/backup\/318 \u6703\u78ba\u4fdd\u6240\u6709\u50b3\u9032\u4f86\u7684\u6771\u897f\u90fd\u6703\u88ab\u4e1f\u5230 \/backup\/318 \u53bb\uff0c\u53c8\u56e0\u70ba authorized_key \u5b58\u7684\u662f\u6bcf\u53f0\u6a5f\u5668\u5404\u5225\u7522\u751f\u7684 key\uff0c\u6211\u5011\u53ef\u4ee5\u5f9e key \u5f88\u6e05\u695a\u7684\u5f97\u77e5\u9019\u6b21\u9023\u7dda\u662f\u5f9e\u54ea\u4f86\u6a5f\u5668\u9023\u904e\u4f86\u7684\uff0c\u642d\u914d command \u6307\u4ee4\u5c31\u53ef\u4ee5\u76f4\u63a5\u9694\u96e2\u5404\u53f0\u6a5f\u5668\uff0c\u4fdd\u8b49\u4ed6\u5011\u4e0d\u6703\u88ab\u585e\u5230\u4e00\u6a23\u7684\u5730\u65b9\u3002\u4e0d\u904e\u9019\u7a2e\u65b9\u5f0f\u7684\u7f3a\u9ede\u662f\u6c92\u8fa6\u6cd5\u8655\u7406\u50cf\u662f \/usr \/usr\/local \/usr\/local\/bin \u9019\u6a23\u7684\u968e\u5c64\uff0c\u6240\u4ee5\u8981\u5099\u4efd\u7684\u6642\u5019\uff0c\u6700\u597d\u4e00\u6b21\u5230\u4f4d\u76f4\u63a5\u5099\u4efd \/ \u7136\u5f8c\u518d\u7528 rsync \u7684\u6392\u9664\u6a5f\u5236\u8df3\u904e\u4e0d\u9700\u8981\u7684\u76ee\u9304\u3002<\/p>\n<p>\u53e6\u5916\u8981\u6ce8\u610f\u7684\u662f $BACKUP_ARGS \u5728\u6a5f\u5668\u4e0b\u7684\u6307\u4ee4\u548c\u5099\u4efd\u4f3a\u670d\u5668 command \u88e1\u7684\u6307\u4ee4\u6700\u597d\u4e00\u81f4\uff0c\u67d0\u4e9b\u53c3\u6578\u5169\u908a\u4e0d\u4e00\u6a23\u6703\u9020\u6210\u932f\u8aa4\uff0c\u50cf\u5728\u6a5f\u5668\u4e0a\u8dd1 rsync &#8211;dry-run \u4f46\u5099\u4efd\u4f3a\u670d\u5668\u6c92\u6709 &#8211;dry-run \u6307\u4ee4\u7684\u8a71\uff0c\u5c31\u53ef\u80fd\u6703\u51fa\u73fe\u9019\u7a2e\u932f\u8aa4<\/p>\n<pre lang=\"none\">Invalid packet at end of run (3984) [sender]\r\nrsync error: protocol incompatibility (code 2) at main.c(691) [sender=3.0.9]<\/pre>\n<p>\u4f46\u662f\u6bcf\u6b21\u53c3\u6578\u4e00\u6539\u5c31\u8981\u5169\u908a\u6539\u4e5f\u662f\u5f88\u7169\uff0c\u9019\u5c31\u662f\u70ba\u4ec0\u9ebc\u6a5f\u5668\u7aef\u9700\u8981 -o SendEnv \u4e86\uff0c\u9019\u500b\u53c3\u6578\u80fd\u5728\u9001\u6307\u4ee4\u7684\u6642\u5019\u9806\u4fbf\u628a $BACKUP_ARGS \u50b3\u904e\u53bb\uff0c\u7576\u7136\u5099\u4efd\u4f3a\u670d\u5668\u7684 sshd \u4e5f\u8981\u9858\u610f\u5403\u9019\u500b\u53c3\u6578\u624d\u884c\uff0c\u9019\u500b\u5728 sshd_config \u88e1\u9762\u8a2d\u3002\u9664\u4e86\u9019\u500b\u8981\u8a2d\u5b9a\u5916\uff0c\u9084\u8981\u653e\u5bec\u8b93 root \u53ef\u4ee5\u900f\u904e ssh \u767b\u5165\uff0c\u4e0d\u904e\u70ba\u4e86\u5b89\u5168\u8d77\u898b\u4e0d\u958b\u653e\u5bc6\u78bc\u767b\u5165\uff0c\u4e5f\u5c31\u662f\u4e00\u5b9a\u8981\u7528 key \u767b\u5165\u3002\u914d\u5408\u9019\u5169\u500b\u9700\u6c42 \/etc\/ssh\/sshd_config \u8981\u52a0\u9019\u5169\u884c<\/p>\n<pre lang=\"none\">\r\nPermitRootLogin without-password\r\nAcceptEnv BACKUP_ARGS\r\n<\/pre>\n<p>\u5176\u4e2d PermitRootLogin \u9084\u53ef\u4ee5\u66f4\u9032\u4e00\u6b65\u8a2d\u6210 forced-commands-only\uff0c\u66f4\u5b89\u5168\u4e9b\u3002<\/p>\n<p><strong> UPDATE 2014-01-15: <\/strong><br \/>\n\u5f8c\u4f86\u767c\u73fe\u5176\u5be6 rsync \u6709\u5167\u9644\u4e00\u500b\u53eb rrsync \u7684 script \u53ef\u4ee5\u5e6b\u5fd9\u8655\u7406\u9019\u500b\u554f\u984c\uff0c\u4e5f\u4e0d\u7528\u50b3\u74b0\u5883\u8b8a\u6578\u7d66 ssh \u4e86\uff0c\u53ea\u8981 authorized_keys \u88e1\u9762\u6539\u6210\u9019\u6a23\u5373\u53ef command=&#8221;\/usr\/local\/bin\/rrsync \/backup\/318&#8243; ssh-rsa AAAA&#8230;c87 root@ck49th318&#8243;<\/p>\n<p>rrsync \u8acb\u81ea\u5df1\u653e\u5230\u60f3\u653e\u7684\u5730\u65b9\uff0c\u53e6\u5916\u9019\u662f\u4e00\u500b perl script\uff0c\u6240\u4ee5 perl \u8ddf\u6703\u7528\u5230\u7684 module \u8981\u5148\u88dd\u597d\u5c31\u662f<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[\u83ef\u8a9e, cmn-Hant-TW] \u4e4b\u524d\u5728\u9019\u7bc7\u6709\u63d0\u5230 snapshot \u7ba1\u7406\u4e3b\u8981\u662f\u7528\u5728 backup \u4e0a\uff0c\u585e\u6771\u897f\u5230\u5099\u4efd\u4f3a\u670d\u5668\u4e0a\u6700\u7c21\u55ae\u7684\u65b9\u5f0f\u5c31\u662f rsync\uff0c\u4e4b\u524d\u4e3b\u8981\u662f\u7528 rsyncd\uff0c\u4f46\u662f rsyncd \u6709\u5e7e\u500b\u554f\u984c\uff0c\u9996\u5148\u662f\u50b3\u8f38\u672a\u52a0\u5bc6\uff0c\u5982\u679c\u7d93\u904e\u516c\u5171\u7db2\u8def\u5340\u9593\u6709\u9ede\u4e0d\u5b89\u5168\uff1b\u518d\u4f86\u5c31\u662f\u5404\u53f0\u5099\u4efd\u7684\u8a2d\u5b9a\u6a94\u4e0d\u5c0f\u5fc3\u5f04\u6210\u4e00\u6a23\u7684\u8a71\uff0c\u6703\u5f88\u641e\u7b11\u7684\u4e0d\u5c0f\u5fc3\u540c\u6b65\u5230\u53e6\u4e00\u53f0\u6a5f\u5668\u7528\u7684\u5099\u4efd\u5340\u57df (\u53ef\u4ee5\u7528\u9396 IP \u4f86\u8655\u7406\uff0c\u4e0d\u904e\u6d6e\u52d5 IP \u7684\u6a5f\u5668\u5c31\u9ebb\u7169\u4e86)\u3002\u6240\u4ee5\u5f8c\u4f86\u5c31\u7814\u7a76\u4e86\u4e00\u4e0b\u6709\u6c92\u6709\u9069\u7576\u89e3\uff0c\u7d50\u679c\u767c\u73fe\u6bd4\u8f03\u7c21\u55ae\u7684\u65b9\u5f0f\u9084\u662f\u7528 rsync + ssh\uff0c\u7136\u5f8c\u900f\u904e ssh\u00a0authorized_keys \u7684\u6a5f\u5236\u9054\u5230\u4e0d\u7528\u8f38\u5165\u5bc6\u78bc\u7684\u6548\u679c\uff0c\u6a5f\u5668\u7aef\u7684\u6307\u4ee4\u9577\u9019\u6a23 export BACKUP_ARGS=&#8221;-avHS &#8211;ignore-errors&#8221; rsync $BACKUP_ARGS -e &#8220;\/usr\/bin\/ssh -o UserKnownHostsFile=\/dev\/null -o StrictHostKeyChecking=no -o LogLevel=ERROR -o SendEnv=BACKUP_ARGS -i $KEY&#8221; \/ $USER@$HOST:&hellip; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[4591,4565,4564],"class_list":["post-2357","post","type-post","status-publish","format-standard","hentry","category-freebsd","tag-freebsd","tag-rsync","tag-ssh"],"_links":{"self":[{"href":"https:\/\/blog.pighead.cc\/whsyu\/wp-json\/wp\/v2\/posts\/2357","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.pighead.cc\/whsyu\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.pighead.cc\/whsyu\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.pighead.cc\/whsyu\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.pighead.cc\/whsyu\/wp-json\/wp\/v2\/comments?post=2357"}],"version-history":[{"count":15,"href":"https:\/\/blog.pighead.cc\/whsyu\/wp-json\/wp\/v2\/posts\/2357\/revisions"}],"predecessor-version":[{"id":2444,"href":"https:\/\/blog.pighead.cc\/whsyu\/wp-json\/wp\/v2\/posts\/2357\/revisions\/2444"}],"wp:attachment":[{"href":"https:\/\/blog.pighead.cc\/whsyu\/wp-json\/wp\/v2\/media?parent=2357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.pighead.cc\/whsyu\/wp-json\/wp\/v2\/categories?post=2357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.pighead.cc\/whsyu\/wp-json\/wp\/v2\/tags?post=2357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}