やりたいこと
- 試行錯誤中だからgitにpushしたくない!
-
出先、仕事場、自宅で編集したソースを簡単に共有したい!
っていう事がありまして、普通にDropboxを始めとしたオンラインストレージを使えば解決だと思っていた時期もありました。
しかし、自分が使っているオンラインストレージ(Dropbox、GoogleDrive)には納得できる除外機能が見当たりません。
簡単な例でいうと、 .ideaディレクトリを同期対象から除外したい だけだったのです。もし方法をご存知の方がいたら教えてください(特にGoogleDriveだと嬉しいです)!!
今回は直接オンラインストレージを使用するのではなく、間にローカル作業用のディレクトリを挟むことで解決してみました。
改善前の構成
PC(work) => OnlineStorage <= LapTop(home)
改善後の構成
PC(work) => PC LocalDir <=> OnlineStorage(GoogleDrive) <=> LapTop LocalDir <= LapTop(home)
つまり?
この記事はオンラインストレージに除外機能をつけるためのWorkaroundと読み替えることもできます。
やりかた(OSX)
LocalDirとのOnlineStorageとの同期にはUnisonを使います。
# install
$ brew install unison
設定してみる
欲しかった除外機能を含めた設定をしていきます。
ここではLocalDirを/Users/name/local-work/tmp、OnlineStorageを/Users/name/GoogleDrive/tmpとした場合で記述しています。
# 設定ディレクトリを作成
$ mkdir ~/.unison/
# 設定ファイルの作成
# http://www.cis.upenn.edu/~bcpierce/unison/download/releases/stable/unison-manual.html
$ cat > ~/.unison/sample.prf
# It also wants to copy together the timestamp of the source file
times = true
# The priority of the new file
prefer = newer
# New file does not matter how synchronized to the user
auto = true
# Update existence by file update date and time
fastcheck = true
# Only error log
#silent = true
# follow =
# Set the synchronization directory that becomes the most significant at the time of
root = /Users/name/local-work
root = /Users/name/GoogleDrive
# Specify you want to synchronize the directory and file name
path = tmp
# Specify the directory and file name that you want to ignore
# .gitginoreと同じ内容にしておけば間違いない!!
ignore = Name */target
ignore = Name */project/project
ignore = Name */.DS_Store
ignore = Name */.sbtserver
ignore = Name */.idea*
ignore = Name */*.iml
ignore = Name */.classpath
ignore = Name */.project
ignore = Name */.settings
ignore = Name */RUNNING_PID
動かしてみる
手動
それではまず手動で動かしてみます。
# LocalDirにファイルを作成
$ touch /Users/name/local-work/tmp/test1.txt
# OnlineStorageにファイルを作成
$ touch /Users/name/GoogleDrive/tmp/test2.txt
# 手動で同期
$ unison sample
Contacting server...
Looking for changes
Reconciling changes
local-work GoogleDrive
new file ----> tmp/test1.txt
<---- new file tmp/test2.txt
Proceed with propagating updates? [] y
Propagating updates
UNISON 2.48.3 started propagating changes at 11:30:47.93 on 06 Aug 2015
[BGN] Copying tmp/test1.txt from /Users/name/local-work to /Users/name/GoogleDrive
[END] Copying tmp/test1.txt
[BGN] Copying tmp/test2.txt from /Users/name/GoogleDrive to /Users/name/local-work
[END] Copying tmp/test2.txt
UNISON 2.48.3 finished propagating changes at 11:30:47.93 on 06 Aug 2015
Saving synchronizer state
Synchronization complete at 11:30:47 (2 items transferred, 0 skipped, 0 failed)
自動
Unisonをデーモン化する方法もありますが、ここでは手軽に定期実行しちゃいます。
# LaunchAgentsの設定ファイルを作成
$ cat > ~/Library/LaunchAgents/my-unison.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<!-- label -->
<key>Label</key>
<string>my-unison</string>
<!-- script -->
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/unison</string>
<!-- ここに引数となる設定名を指定します(.prfを除外した名前) -->
<string>sample</string>
<!-- Yes/Noを聞かれないバッチ用のオプション -->
<string>-batch</string>
</array>
<!-- launchctl loadしたタイミングで実行するか -->
<key>RunAtLoad</key>
<true/>
<!-- インターバル指定 例) 5分毎 -->
<key>StartInterval</key>
<integer>300</integer>
<!-- エラー内容を捨ててますが、ちゃんと運用したい場合はこの設定もしっかり -->
<key>StandardErrorPath</key>
<string>/dev/null</string>
<key>StandardOutPath</key>
<string>/dev/null</string>
</dict>
</plist>
# LaunchAgentsの読み込み
$ launchctl load ~/Library/LaunchAgents/my-unison.plist
最後に
これでソース関連ファイルだけを同期することができるようになりました。
これで思いついたアイディアをパパっと複数の環境で共有できますね!