ローカルファイルを更新したら、SFTPを利用して自動でファイルをサーバーにアップする。
ローカルファイルを更新したら、SFTPを利用して自動でファイルをサーバーにアップするgemを作りました。
pikonori/auto_sftp · GitHub
rsyncとかが使えない時に利用できるのではないかと思います。
Auto SFTPの導入
まず、Gemをインストールします。
$ gem install autosftp
Auto SFTPの初期設定
Gemをインストールしたらヘルプを確認してみましょう。
$ autosftp -h autosftp delete [remote name] # remove the configuration. autosftp help [COMMAND] # Describe available commands or one specific command autosftp init # Creating the initial file. file called '.autosftp' is created autosftp list # setting list autosftp set [remote name] # add information to the '.autosftp'. Please put the name of any [remote name] autosftp start [remote name] # Automatic monitoring start
まず、設定ファイルを準備します。
この設定ファイルは何処に配置してもいいですが、プロジェクト毎にサーバーが変わると思いますので、プロジェクトのディレクトリまで移動して、設定ファイルを準備します。
$ cd ~/workspase/project $ autosftp init
「autosftp init 」をコマンドで叩くとyaml形式で.autosftpというファイルが作成されます。
.autosftpはSSHの情報を登録するファイルになります。直接ファイルを操作しても問題ありません。
次に設定ファイルに接続先の情報を追加します。
接続先の情報はいくつでも登録可能です。
$ autosftp set [remote name]
[remote name]は自由な名称をつけて下さい。
対話型で入力を促されるので、それ通りに入力していきます。
$ autosftp set default [username@host:port] username@sample.com:22 #portが無しの場合は自動で22が入ります。 password: # 表には表示されません remote path: /remote/path # リモート先にアップするディレクトリを指定します。 # この時に一度認証を行い、接続できるか確認します。 local path: --If you enter a blank, the current directory is set /local/path # ローカルのディレクトリを指定します。からの場合はカレントディレクトリを登録します。
全てを入力すると、.autosftpにSSHの接続情報が書き込まれます。
--- default: :user: username :host: sample.com :port: 22 :password: password :remote_path: /remote/path :local_path: /local/path
コレで準備が完了します。
Auto SFTPの使い方
自動監視をスタートします。
$ autosftp start [remote name]
設定したsftpを削除します。
$ autosftp delete [remote name]