Last active
August 14, 2025 10:09
-
-
Save hzbd/577055a99b58d272f6e0c3d88e6e3074 to your computer and use it in GitHub Desktop.
transfer.sh - Securely transfer files/directories using rsync over SSH. Supports resumable transfers and colored output.
This file contains hidden or 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/bash | |
| # transfer.sh - Securely transfer files/directories using rsync over SSH. | |
| # Supports resumable transfers and colored output. | |
| # --- Define Colors --- | |
| GREEN='\033[0;32m' | |
| RED='\033[0;31m' | |
| CYAN='\033[0;36m' | |
| NC='\033[0m' # No Color | |
| # --- Script Arguments --- | |
| MODE="$1" | |
| SOURCE="$2" | |
| DESTINATION="$3" | |
| PORT=${4:-22} # Default to SSH port 22 | |
| # --- Argument Validation --- | |
| if [[ "$MODE" != "send" && "$MODE" != "get" ]] || [ "$#" -lt 3 ]; then | |
| echo -e "${RED}β Error: Invalid arguments.${NC}" | |
| echo -e "\nUsage: $0 [send|get] [source] [destination] [port]" | |
| echo " ex: $0 send ./local-file user@host:/remote/path" | |
| echo " ex: $0 get user@host:/remote/file ./local-path 2222" | |
| exit 1 | |
| fi | |
| # rsync options: -a (archive), -v (verbose), -z (compress), -P (progress/partial) | |
| RSYNC_OPTS="-avzP" | |
| # --- Execute rsync command based on mode --- | |
| case "$MODE" in | |
| "send") | |
| if [ ! -e "$SOURCE" ]; then | |
| echo -e "${RED}β Error: Local source '$SOURCE' not found.${NC}" | |
| exit 1 | |
| fi | |
| echo -e "${CYAN}π Sending '$SOURCE' to '$DESTINATION' (port $PORT)...${NC}" | |
| rsync $RSYNC_OPTS -e "ssh -p $PORT" "$SOURCE" "$DESTINATION" | |
| ;; | |
| "get") | |
| echo -e "${CYAN}π Getting '$SOURCE' from '$DESTINATION' (port $PORT)...${NC}" | |
| rsync $RSYNC_OPTS -e "ssh -p $PORT" "$SOURCE" "$DESTINATION" | |
| ;; | |
| esac | |
| # --- Check the result --- | |
| if [ $? -eq 0 ]; then | |
| echo -e "${GREEN}β Success! Transfer complete.${NC}" | |
| else | |
| echo -e "${RED}β Failed. Please check the following:${NC}" | |
| echo " - Connectivity, credentials, paths, and permissions." | |
| echo " - SSH is running on port $PORT and 'rsync' is installed on the server." | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.