Skip to content

Instantly share code, notes, and snippets.

@paveltimofeev
Created November 28, 2014 16:32
Show Gist options
  • Save paveltimofeev/457599e1b7664742db54 to your computer and use it in GitHub Desktop.
Save paveltimofeev/457599e1b7664742db54 to your computer and use it in GitHub Desktop.
SSH/SCP Sync
#! /bin/bash
# SSH/SCP Sync
# use in this way:
# bash sync.sh '/c/Git/Project/forder-for-sync/*' '10.0.0.10' '/home/username/forder-for-sync/' '/c/sshKeys/key.pem'
args=("$@")
SRCFOLDER=${args[0]} # '/c/Git/Project/forder-for-sync/*'
TRGHOST=${args[1]} # '10.0.0.10'
TRGFOLDER=${args[2]} # '/home/username/forder-for-sync/'
KEYSPATH=${args[3]} # '/c/sshKeys/key.pem'
BEFORECMDS=${args[4]} # 'sudo rm -rf /temp/' or ''
AFTERCMDS=${args[5]} # 'ls' or ''
# read before & after scripts
beforeSyncCommands=$(<beforeSync.sh)
afterSyncCommands=$(<afterSync.sh)
exit 0;
# execute remotely Before Sync commands
echo "execute Before Sync commands"
if [ $beforeSyncCommands ]; then
ssh -i $KEYSPATH -o StrictHostKeyChecking=no ubuntu@$TRGHOST "$beforeSyncCommands"
fi
if [ $BEFORECMDS ]; then
ssh -i $KEYSPATH -o StrictHostKeyChecking=no ubuntu@$TRGHOST $BEFORECMDS
fi
# copy files
echo "copy files"
for f in $SRCFOLDER
do
printf "."
if [[ -f $f ]]; then
scp -i $KEYSPATH -o StrictHostKeyChecking=no $f ubuntu@$TRGHOST:$TRGFOLDER$(basename $f)
fi
done
echo ""
# execute remotely After Sync commands
echo "execute After Sync commands"
if [ $AFTERCMDS ]; then
ssh -i $KEYSPATH -o StrictHostKeyChecking=no ubuntu@$TRGHOST $AFTERCMDS
fi
if [ $afterSyncCommands ]; then
ssh -i $KEYSPATH -o StrictHostKeyChecking=no ubuntu@$TRGHOST "$afterSyncCommands"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment