Skip to content

Instantly share code, notes, and snippets.

@mevansam
Last active May 24, 2017 07:27
Show Gist options
  • Save mevansam/9301eb02ac6e23e361e1ef841766a490 to your computer and use it in GitHub Desktop.
Save mevansam/9301eb02ac6e23e361e1ef841766a490 to your computer and use it in GitHub Desktop.
Synchronize a local path with a remote path
#!/bin/bash
if [[ $# -ne 5 ]]; then
echo -e "\nUsage: sync [SSH KEY PATH] [SSH_USER] [REMOTE HOST] [LOCAL DIR/FILE] [REMOTE DIR/FILE]\n"
exit 1
fi
if [[ ! -e $4 ]]; then
echo -e "\nError: '$4' does not exist."
exit 1
fi
ssh_key=$1
ssh_user=$2
ssh_host=$2@$3
src=$(cd $(dirname $4) && pwd)/$(basename $4)
if [[ -z $5 ]]; then
tgt='~'
else
tgt=$5
fi
function s() {
/usr/bin/ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i $ssh_key $@
}
function sync() {
rsync -avz -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i $ssh_key" --progress $@
}
s $ssh_host "mkdir -p $tgt"
while true; do
clear
sync $src $ssh_host:$tgt
sleep 1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment