rsyncをcronで実行する場合にpasswordを聞かれて時の対処を考えてみた
$ rsyncをcronで実行する場合にpasswordを聞かれて時の対処を考えてみた
rsyncの「--password-file」 optionはremote側にも設定が必要な為、今回は断念しました 変わりにexpectを使ってshell scriptを書きました、それをcronで実行する形にしました
expectをinstall
今回はcentos6.4を使っているので、yumコマンドでinstall
yum install expect
今はサーバの構築にchefを使っているので上記のcommandに相当するrecipeを書いています
package "expect" do action :install end
shell script
expectを使って以下のbackup scriptを作る
#!/bin/sh PASSWORD="remote server password" expect -c " spawn rsync -av /sync/directory/pass/ [remote user]@[remote host]:/remote/sync/directory/ expect backup@111.222.333.444's\ password:\ sleep 1 send \"${PASSWORD}\r\" expect eof "
汎用的に作る意味もなかったのでベタ書き
上記をcronに設定してsyncするだけ