Skip to content

Instantly share code, notes, and snippets.

@AlexandroPerez
Last active July 14, 2020 03:55
Show Gist options
  • Save AlexandroPerez/b749ed7c1255961794fb1f0d804fbe32 to your computer and use it in GitHub Desktop.
Save AlexandroPerez/b749ed7c1255961794fb1f0d804fbe32 to your computer and use it in GitHub Desktop.
Auto Sync shell script for Cygwin. For requirements see: https://gist.github.com/AlexandroPerez/c6122b1f8648273f83b894e5c4436e6a
#!/bin/bash
# This script is meant to run in cygwin as a way to sync a Windows host to a Guest Vagrant Machine
# So you can edit your code in the host, sync changes to your vagrant machine, and let node/npm
# run in a local folder (no more symlink problems!)
#
# For requirements see: https://gist.github.com/AlexandroPerez/c6122b1f8648273f83b894e5c4436e6a
#
# Usage: autosync.sh user@host:/path/to/remote/folder
#
# Optional but recommended: add a .syncignore file to your folder, and add
# a path in each line you don't want to sync to to remote.
# This is IMPORTANT, because rsync will delelete from remote any files/folders
# not found in the source. If you plan on using node/npm or other tools
# on remote that will create files there, you need to add them to the
# .syncignore so that they aren't deleted by rsync.
#
# EXAMPLE .syncignore file for npm/node on remote and git on source:
# node_modules
# build
# npm-debug.log
# .git
# package-lock.json
# .syncignore
#
# This will make sure .git folder isn't tracked in source, files created in
# remote by node/npm won't be deleted, and the file .syncignore itself
# won't be sent to remote.
# address to the remote host and destination folder must be provided as argument
if [ -z "$1" ];
then
echo "Must provide remote address: i.e. user@host:/path/to/destination"
exit 1
fi
remote=$1
#get current working directory from cygwin
cygpath=$(pwd)
#convert cygpath to windows absolute path
# inotifywait needs a windows path to work
dir=$(cygpath -da $cygpath)
#Do not sync paths found in .syncignore
#Save as a cygpath for rsync
syncignore="$cygpath/.syncignore"
# if a .syncignore file doesn't exist create one that ignores .git folder
# Also copy paths found in .gitignore if file exists
if [ ! -f "$syncignore" ]; then
echo ".syncignore doesn't exist."
if [ -f "$cygpath/.gitignore" ]; then
echo "... creating from .gitignore..."
cp $cygpath/.gitignore $syncignore
elif [ -d "$cygpath/.git" ]; then
echo "... .git folder found, excluding from syncing"
else
echo "... no .git folder found. You should be using it :p"
echo "... .git folder will be excluded, so you can start using git now ;)\n"
fi
echo "... A file named .syncignore has been created that excludes the .git folder"
echo "... edit this file to add more paths you want excluded from syncing"
echo ".git" >> .syncignore
echo ".syncignore" >> .syncignore
fi
echo "watching $dir"
echo "ignoring paths found in $syncignore"
echo "remote path is $remote"
# rsync command needs a cygpath
command="rsync -rtvu --delete --exclude-from \"$syncignore\" $cygpath/ $remote/"
echo 'Syncing for the first time...'
eval $command
while inotifywait -e create,delete,modify -rq $dir; do
echo 'Syncing...'
eval $command
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment