#!/bin/bash
# Rsync to Android (Termux) Script
# This script helps you sync files from Mac to Android via SSH/rsync
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
echo -e "${GREEN}=== Rsync to Android (Termux) ===${NC}\n"
# Check if rsync is installed
if ! command -v rsync &> /dev/null; then
echo -e "${RED}Error: rsync is not installed${NC}"
echo "Install it with: brew install rsync"
exit 1
fi
# Get Android username
read -p "Enter Android username (e.g., u0_a123): " ANDROID_USER
if [ -z "$ANDROID_USER" ]; then
echo -e "${RED}Username cannot be empty${NC}"
exit 1
fi
# Get Android IP address
read -p "Enter Android IP address (e.g., 192.168.1.100): " ANDROID_IP
if [ -z "$ANDROID_IP" ]; then
echo -e "${RED}IP address cannot be empty${NC}"
exit 1
fi
# Get SSH port (default 8022)
read -p "Enter SSH port [default: 8022]: " SSH_PORT
SSH_PORT=${SSH_PORT:-8022}
# Get source directory
read -p "Enter source directory/file on Mac: " SOURCE_PATH
if [ -z "$SOURCE_PATH" ]; then
echo -e "${RED}Source path cannot be empty${NC}"
exit 1
fi
# Expand tilde in source path
SOURCE_PATH="${SOURCE_PATH/#\~/$HOME}"
# Check if source exists
if [ ! -e "$SOURCE_PATH" ]; then
echo -e "${RED}Error: Source path does not exist: $SOURCE_PATH${NC}"
exit 1
fi
# Get destination directory
echo -e "\n${YELLOW}Common Android destinations:${NC}"
echo " ~/storage/downloads/ - Downloads folder"
echo " ~/storage/shared/ - Internal storage"
echo " ~/received/ - Home directory folder"
echo " ~/ - Termux home"
read -p "Enter destination path on Android: " DEST_PATH
if [ -z "$DEST_PATH" ]; then
echo -e "${RED}Destination path cannot be empty${NC}"
exit 1
fi
# Authentication method
echo -e "\n${YELLOW}Select authentication method:${NC}"
echo " 1) Password"
echo " 2) SSH key"
read -p "Enter choice [1-2]: " AUTH_METHOD
# Build rsync command
RSYNC_CMD="rsync -avzh --progress"
if [ "$AUTH_METHOD" = "2" ]; then
read -p "Enter path to SSH private key [default: ~/.ssh/id_rsa]: " SSH_KEY
SSH_KEY=${SSH_KEY:-~/.ssh/id_rsa}
SSH_KEY="${SSH_KEY/#\~/$HOME}"
if [ ! -f "$SSH_KEY" ]; then
echo -e "${RED}Error: SSH key not found: $SSH_KEY${NC}"
exit 1
fi
RSYNC_CMD="$RSYNC_CMD -e \"ssh -p $SSH_PORT -i $SSH_KEY\""
else
RSYNC_CMD="$RSYNC_CMD -e \"ssh -p $SSH_PORT\""
fi
# Add source and destination
RSYNC_CMD="$RSYNC_CMD \"$SOURCE_PATH\" ${ANDROID_USER}@${ANDROID_IP}:\"${DEST_PATH}\""
# Confirm before executing
echo -e "\n${YELLOW}=== Summary ===${NC}"
echo "Source: $SOURCE_PATH"
echo "Destination: ${ANDROID_USER}@${ANDROID_IP}:${DEST_PATH}"
echo "SSH Port: $SSH_PORT"
echo "Auth: $([ "$AUTH_METHOD" = "2" ] && echo "SSH Key ($SSH_KEY)" || echo "Password")"
echo -e "\n${YELLOW}Command to execute:${NC}"
echo "$RSYNC_CMD"
echo
read -p "Proceed with transfer? (y/n): " CONFIRM
if [ "$CONFIRM" != "y" ] && [ "$CONFIRM" != "Y" ]; then
echo "Transfer cancelled."
exit 0
fi
# Test SSH connection first
echo -e "\n${YELLOW}Testing SSH connection...${NC}"
if [ "$AUTH_METHOD" = "2" ]; then
ssh -p "$SSH_PORT" -i "$SSH_KEY" -o ConnectTimeout=5 "${ANDROID_USER}@${ANDROID_IP}" "echo 'Connection successful'" 2>/dev/null
else
ssh -p "$SSH_PORT" -o ConnectTimeout=5 "${ANDROID_USER}@${ANDROID_IP}" "echo 'Connection successful'" 2>/dev/null
fi
if [ $? -eq 0 ]; then
echo -e "${GREEN}✓ SSH connection successful${NC}\n"
else
echo -e "${RED}✗ SSH connection failed. Please check:${NC}"
echo " - Android device is on the same network"
echo " - SSH server is running on Android (sshd)"
echo " - IP address and username are correct"
echo " - Port $SSH_PORT is accessible"
exit 1
fi
# Execute rsync
echo -e "${YELLOW}Starting transfer...${NC}\n"
eval $RSYNC_CMD
if [ $? -eq 0 ]; then
echo -e "\n${GREEN}✓ Transfer completed successfully!${NC}"
else
echo -e "\n${RED}✗ Transfer failed${NC}"
exit 1
fi
# Ask if user wants to save configuration
echo -e "\n${YELLOW}Save this configuration for future use?${NC}"
read -p "(y/n): " SAVE_CONFIG
if [ "$SAVE_CONFIG" = "y" ] || [ "$SAVE_CONFIG" = "Y" ]; then
CONFIG_FILE="$HOME/.rsync_android_config"
echo "ANDROID_USER=\"$ANDROID_USER\"" > "$CONFIG_FILE"
echo "ANDROID_IP=\"$ANDROID_IP\"" >> "$CONFIG_FILE"
echo "SSH_PORT=\"$SSH_PORT\"" >> "$CONFIG_FILE"
echo "AUTH_METHOD=\"$AUTH_METHOD\"" >> "$CONFIG_FILE"
if [ "$AUTH_METHOD" = "2" ]; then
echo "SSH_KEY=\"$SSH_KEY\"" >> "$CONFIG_FILE"
fi
echo -e "${GREEN}✓ Configuration saved to $CONFIG_FILE${NC}"
echo "You can edit this file to quickly load settings next time"
fi