LoginSignup
2
2

More than 5 years have passed since last update.

ローカル開発環境を自動でrsyncする話

Posted at

PHPを始めて、ローカル環境からリモートサーバへのファイル転送(デプロイ)が億劫がだったため作成

デプロイ先:dev-server

ローカルPC:mac


まずはデプロイ先に繋ぐssh_configの設定から。。。

~/.ssh/configを設定する

Host dev-server
  HostName ${ホスト]
  User ${ユーザ}
  IdentityFile ~/.ssh/id_rsa

コマンドラインから繋がればOK

$ ssh dev-server

手動実行編

.bashrcを書き換える

alias rsync_app="rsync -auvz ¥
--exclude='.git/' --exclude='.settings/' --exclude='.buildpath' ¥
--exclude='.gitignore' --exclude='.project' --exclude='vendor/' ¥
${デプロイ元dir} ${ユーザ}@dev-server:${デプロイ先dir}"

.bashrcを読み込む

$ source .bashrc

.bash_profileに記載が必要かも

if [ -f "${HOME}/.bashrc" ]; then source "${HOME}/.bashrc"; fi

これで手動実行は可能

$ rsync_app

ここからは勝手にやってくれるように編

rsync用のシェルを用意(rsync.sh)

本来ならばシェルの中で「rsync_app」を呼んでコマンド記述を省略したかったのだが、うまくいかず時間との兼ね合いで断念。。。

#!/bin/bash
rsync -auvz ¥
--exclude='.git/' --exclude='.settings/' --exclude='.buildpath' ¥
--exclude='.gitignore' --exclude='.project' --exclude='vendor/' ¥
${デプロイ元dir} ${ユーザ}@dev-server:${デプロイ先dir}

キック用のfswatchコマンドを記載したシェルを用意(fswatch.sh)

#!/bin/bash
 fswatch -o ${デプロイ元dir} | xargs -n1 rsync.sh &

コマンドラインからfswatch.shを実行

$ ./fswatch

  • これでfswatchコマンドがバックグラウンドで実行される
  • ${デプロイ元dir}の変更を検知
  • rsync.shが実行され、デプロイ完了
2
2
0

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
2
2