Last active
April 3, 2020 08:32
-
-
Save jasdeepkhalsa/9072fb1156f8f87f8dbc04f0554ebd22 to your computer and use it in GitHub Desktop.
rsync A->B - Transfer a directory from a source server (A) into a directory on a target server (B) via SSH & rsync
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
#!/usr/bin/env bash | |
# This script will transfer a directory from a source server (A) | |
# into a directory on a target server (B) | |
# Pre-requisites: The script must be run by a user who has SSH / passwordless access to both server A & B | |
# From StackExchange.com (https://unix.stackexchange.com/a/317815) - Authored by David I. (https://unix.stackexchange.com/users/71948/david-i) adapated from an answer by roaima (https://unix.stackexchange.com/users/100397/roaima) - Under the license: cc by-sa 4.0 with attribution required (https://creativecommons.org/licenses/by-sa/4.0/) | |
# Slight modifications may have been made to the original script, which are in no way endorsed by the original authors of this script | |
# Specific alterations include: | |
# - the rsync parameters checking files by checksum instead of date/time/size | |
# - no strict key checking in SSH, so make sure you're connecting to trusted servers! | |
# - verbosity settings are turned on both for SSH and rsync to help in debugging issues | |
# - Making the input parameters configurable and required before the script begins | |
# This script is for my personal use only, by using it you do so at your own risk | |
if [[ "$1" ]]; then | |
SOURCE_USER="$1" | |
else | |
echo "Please enter a source user, usually before the @ within an ssh command" | |
exit 1 | |
fi | |
if [[ "$2" ]]; then | |
SOURCE_HOST="$2" | |
else | |
echo "Please enter a source host, usually after the @ in an ssh command" | |
exit 1 | |
fi | |
if [[ "$3" ]]; then | |
SOURCE_PATH="$3" | |
else | |
echo "Please enter a source path, usually starting with a forward slash /" | |
exit 1 | |
fi | |
if [[ "$4" ]]; then | |
TARGET_USER="$4" | |
else | |
echo "Please enter a target user, usually before the @ within an ssh command" | |
exit 1 | |
fi | |
if [[ "$5" ]]; then | |
TARGET_HOST="$5" | |
else | |
echo "Please enter a target host, usually after the @ in an ssh command" | |
exit 1 | |
fi | |
if [[ "$6" ]]; then | |
TARGET_PATH="$6" | |
else | |
echo "Please enter a target path, usually starting with a forward slash /" | |
exit 1 | |
fi | |
ssh -o StrictHostKeyChecking=no -v -l $TARGET_USER -A -R localhost:22000:$TARGET_HOST:22 \ | |
$SOURCE_USER@$SOURCE_HOST "rsync -e 'ssh -o StrictHostKeyChecking=no -v -p 22000' -arvz --checksum --delete --progress --stats $SOURCE_PATH \ | |
$TARGET_USER@localhost:$TARGET_PATH" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment