Last active
January 14, 2020 14:16
-
-
Save dsmiley/daff3c978fe234b48a69a01b54ea9914 to your computer and use it in GitHub Desktop.
Rsync and SSH to remote host
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# Utility script to rsync and then open a shell or execute commands on a remote host. | |
# Tailored a little bit for Lucene/Solr | |
# @author David Smiley | |
# | |
set -uex | |
REMOTEHOST=buildbox.local | |
REMOTEPATH="builds$PWD" | |
if [ ! -e ".git" ]; then #file or dir | |
echo "Expecting the current directory to be a git project." | |
exit 1 | |
fi | |
#https://stackoverflow.com/questions/13713101/rsync-exclude-according-to-gitignore-hgignore-svnignore-like-filter-c | |
git -C . ls-files --exclude-standard -oi --directory > /tmp/git-excludeme$$.txt | |
# note: This will fail if the target directory doesn't exist on the remote. This is deliberate to avoid accidents. | |
# Transfer everything in the current directory to the remote. | |
# Use times to detect new stuff. It'll sync times. | |
# Human-readable. Itemize. | |
# Exclude /.git and everything the .gitignore files say to ignore. | |
# Delete remote files except those excluded by our exclude patterns. | |
rsync -rthi --delete --exclude='/.git' --exclude-from=/tmp/git-excludeme$$.txt \ | |
. "$REMOTEHOST:$REMOTEPATH" | |
#FORMER FILTER: --filter="dir-merge,- .gitignore" | |
# However rsync has some slight differences, and some ignored files are in git anyway. | |
# SSH to the machine at the target directory | |
if [ -z "${1-}" ]; then #is $1 defined? | |
# switch to interactive shell (force TTY) | |
ssh -t $REMOTEHOST "cd $REMOTEPATH && exec \$SHELL -l" | |
else | |
# execute args and then quit | |
ssh -t $REMOTEHOST "cd $REMOTEPATH && $@" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment