Skip to content

Instantly share code, notes, and snippets.

@gbluma
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbluma/bb5caca203ac8b4b11f4 to your computer and use it in GitHub Desktop.
Save gbluma/bb5caca203ac8b4b11f4 to your computer and use it in GitHub Desktop.
A script to listen for local file changes and push them to a remote server
# The contents of this file go in ~/.ssh/config
ControlMaster auto
ControlPath ~/.ssh/sockets/%h_%p_%r
ControlPersist yes
TCPKeepAlive yes
# try to keep connections open for four hours (adjust as you see fit)
ServerAliveInterval 4
ServerAliveCountMax 1
#!/bin/bash
REMOTE_USER=garrett
REMOTE_HOST=remotehostname
REMOTE_FOLDER=/var/www/projectX
LOCAL_FOLDER=/code/_release/projectX
cd $LOCAL_FOLDER
inotifywait \
-mqre close_write \
-e delete \
--format "%:e %w %f" ./ \
| while read -r event path newfile
echo "listening for changes..."
do
if [[ $newfile =~ .*~|.*git.*|\.swp.* ]]; then
continue
fi
if [[ $event == *DELETE* ]]; then
echo "rm $path$newfile" | sftp -b - $REMOTE_USER@$REMOTE_HOST:$REMOTE_FOLDER
fi
if [[ $event == *CLOSE_WRITE* ]]; then
echo "cd $path
put $path$newfile" | sftp -b - $REMOTE_USER@$REMOTE_HOST:$REMOTE_FOLDER
fi
done
mkdir -p ~/.ssh/sockets/
mkdir -p ~/.bin/
cd ~/.bin/
wget https://gist.githubusercontent.com/gbluma/bb5caca203ac8b4b11f4/raw/2032750c1eccd097dbd48d13cb134b202dfc81b9/sync.sh
chmod +x ~/.bin/sync.sh
# edit sync.sh as appropriate
# initialize connection (auth, etc.) and drop out of shell
ssh myremotehost
> exit
# session remains active
# activate monitor/syncing
~/.bin/sync.sh
# work in other window.
# on save => change will be detected & uploaded via SFTP to remote destination.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment