LoginSignup
41

More than 5 years have passed since last update.

fswatch&rsync&Automatorで、Macとサーバーのファイルをリアルタイム同期する。

Last updated at Posted at 2013-12-25

開発サーバーがリモートにあったりしちゃって、ローカル感覚で開発できないよーということが何度かありました。
それをfswatchとAutometorを使ってファイルをリアルタイム同期して、ローカルで開発するような感覚で開発できるようになったのでメモ残しておきます。

ちなみにGitでブランチをチェックアウトしてもリモートに反映してくれます。
Sublime Text 3を使っていますが、SFTPよりも安心感が有りますね。

fswatchの導入

brew でインストールします( otolabさん、情報ありがとうございました!)

$ brew install fswatch
==> Downloading https://homebrew.bintray.com/bottles/fswatch-1.5.1.yosemite.bottle.tar.gz
######################################################################## 100.0%
==> Pouring fswatch-1.5.1.yosemite.bottle.tar.gz
🍺  /usr/local/Cellar/fswatch/1.5.1: 48 files, 1.0M

Automatorの作成 (※Automator使うの辞めました -> 追記)

Automatorを開くと新規作成ダイアログがでてくるので、

新規作成
ー> アプリケーションを作成
ー> 保存の時、名前の拡張子を .app としておく。

アクションから、シェルスクリプトを実行 を選択して
内容にこう書きます

/usr/local/bin/fswatch /path/to/watch/folder/ "rsync -avru --delete --exclude='.git' /path/to/sync/from/ servername:/path/to/sync/to/"

rsync のオプションなどはググってもらえれば。
※ちなみにこれはservernameに鍵認証でアクセスできる時の書き方です。

Rsyncの開始

保存するとAutometorの実行ファイルが出来上がるので、クリックすればRsync開始。

ファイルを作成、変更、削除しても、リモートにリアルタイム同期しちゃいます。あー楽。

追記

・・・と運用していたのですがAutomator辞めました。何やら同期してくれない時がありまして。
最近はコマンドで動かして、窓を放置プレーしています。

.bash_profile
from="/path/to/watch/folder"
to="servername:/path/to/sync/to"
excludes="--exclude='.git'"
sync="rsync -avru --delete $excludes $from/ $to/"
alias myrsync=$sync
alias myrsyncwatch='/usr/local/bin/fswatch $from/ "$sync"'

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
41