Skip to content

Instantly share code, notes, and snippets.

@QinMing
Last active May 27, 2020 17:29
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 QinMing/26a19e6ac599c20667a39895c4fa7b91 to your computer and use it in GitHub Desktop.
Save QinMing/26a19e6ac599c20667a39895c4fa7b91 to your computer and use it in GitHub Desktop.
upload/download current folder to the same position at remote host
#!/bin/bash
#
# Copyright (c) 2016 Ming Qin (覃明) <https://github.com/QinMing>
# Open source under MIT LICENSE.
# Notice:
# dot files in remote will not be downloaded since we are using `zip -r -q ~/$ZIP_FILE_NAME ./*`
# This is to protect local dot files
# Change this to `zip blabala . ` if dot files are wanted. Also, `find . -delete` need to change
# Note: `find $path -delete` will delete the folder itself, so use `find . -delete`
SELF_NAME=`basename $0`
ZIP_FILE_NAME=${SELF_NAME}_sync.temp.zip
# Overwrite destination path for remote host in ./.ss_config file
if [[ -f "./.ss_config" ]]; then
# -m : max number of match returned
path=`grep -m 1 -e '^.*/git/.*' .ss_config`
ALLOWED_FOLDER=^.*/git/.+
else
path="~${PWD:${#HOME}}"
ALLOWED_FOLDER=^$HOME/git/.+
fi
if [[ -z $path ]]; then
echo "[ERROR] Specified path in .ss_config does not match pattern"
exit 1
else
echo "[INFO] Remote path: \"$path\""
fi
sync_arg_check() {
if [[ -z "$1" ]]; then
echo "Missing argument."
$SELF_NAME --help
exit 1
fi
if [[ ! $PWD =~ $ALLOWED_FOLDER ]]; then
echo "This folder not allowed. Current setting: $ALLOWED_FOLDER "
exit 1
fi
}
register_cleanup() {
# Clean up after process exits
trap "
echo '[INFO] Cleaning up ...'
rm ~/$ZIP_FILE_NAME || true
ssh $1 \"rm ~/$ZIP_FILE_NAME\" || true
" EXIT TERM
}
case "$1" in
""|-h|--help)
echo -e "Usage:\n"
echo -e "$SELF_NAME [user@]host"
echo -e " ssh to host and go to the same directory as local pwd\n"
echo -e "$SELF_NAME (up/down) [user@]host\n"
echo -e " This command uploads/downloads your current folder to/from the same position at the remote"
echo -e " Will erase dstination folder. Use at your own risk\n"
;;
up|u)
shift 1
sync_arg_check $1
register_cleanup $1
echo "[INFO] Compressing ..."
zip -r -q ~/$ZIP_FILE_NAME . || exit 1
# -q: --quiet
echo "[INFO] Uploading ..."
scp ~/$ZIP_FILE_NAME $1:~ || exit 1
# command -v unzip >/dev/null 2>&1 || echo '[ERROR] unzip is not installed'
ssh $1 "
echo \"[INFO] Checking installation of unzip command:\"
command -v unzip
if [[ \$? -ne 0 ]]; then
echo \"[ERROR] unzip is not installed\";
exit 1;
fi
if [[ ! -d $path ]]; then
echo \"[INFO] Creating folder $path\"
mkdir -p $path
if [[ \$? -ne 0 ]]; then
echo \"[ERROR] cannot create folder\";
exit 1;
fi
fi
cd $path
if [[ \$? -ne 0 ]]; then
echo \"[ERROR] cd $path\";
exit 1;
fi
find . -delete
if [[ \$? -ne 0 ]]; then
echo \"[ERROR] find\";
exit 1;
fi
echo \"[INFO] Remote folder $path erased. Uncompressing ...\"
unzip -q ~/$ZIP_FILE_NAME -d $path
if [[ \$? -ne 0 ]]; then
echo \"[ERROR] unzip -q ~/$ZIP_FILE_NAME -d $path\";
exit 1;
fi
" || exit 1
;;
down|d)
shift 1
sync_arg_check $1
register_cleanup $1
ssh $1 "
echo \"[INFO] Checking installation of zip command:\"
command -v zip
if [[ \$? -ne 0 ]]; then
echo \"[ERROR] zip is not installed\";
exit 1;
fi
echo \"[INFO] Compressing ...\"
cd $path
if [[ \$? -ne 0 ]]; then
echo \"[ERROR] cd $path\"
exit 1;
fi
zip -r -q ~/$ZIP_FILE_NAME .
if [[ \$? -ne 0 ]]; then
echo \"[ERROR] zip\"
exit 1;
fi
" || exit 1
echo "[INFO] Deleting content in current directory ..."
find . -delete
echo "[INFO] Downloading ..."
scp $1:~/$ZIP_FILE_NAME ~
echo "[INFO] Uncompressing ..."
unzip -q ~/$ZIP_FILE_NAME -d .
;;
test)
echo "========================== test_allowed_folder ========================== "
if [[ $HOME/git/a/c =~ $ALLOWED_FOLDER ]]; then echo Passed! ; else echo Failed! ; fi
if [[ $HOME/git/ab =~ $ALLOWED_FOLDER ]]; then echo Passed! ; else echo Failed! ; fi
if [[ $HOME/git/a =~ $ALLOWED_FOLDER ]]; then echo Passed! ; else echo Failed! ; fi
if [[ $HOME/git/ =~ $ALLOWED_FOLDER ]]; then echo Failed! ; else echo Passed! ; fi
if [[ $HOME/git =~ $ALLOWED_FOLDER ]]; then echo Failed! ; else echo Passed! ; fi
echo "========================== test_upload_download ========================== "
trap "
echo '[INFO] Cleaning up test temp files ...'
rm -r ~/git/ss_test_temp_folder || true
ssh aws \"rm -r ~/git/ss_test_temp_folder\" || true
" EXIT INT TERM
mkdir ~/git/ss_test_temp_folder || exit 1
cd ~/git/ss_test_temp_folder || exit 1
echo "test file content" > ss_test_file_1 || exit 1
ss up aws || exit 1
rm ss_test_file_1 || exit 1
scp aws:~/git/ss_test_temp_folder/ss_test_file_1 . || exit 1
[[ `cat ss_test_file_1` == "test file content" ]] || exit 1
rm ss_test_file_1 || exit 1
ss down aws || exit 1
[[ `cat ss_test_file_1` == "test file content" ]] || exit 1
echo "All tests passed!"
;;
*)
# -t: Force pseudo-terminal allocation.
ssh -t $* "
cd $path || cd ~
bash --login
"
;;
esac
# Note: comment at the end is not ok? Was code in here executed? `ls -ahlt`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment