Cara Menggunakan sshpass dengan Rsync
Apa itu sshpass?
sshpass adalah tool untuk menyediakan password SSH secara non-interactive (tanpa prompt). Sangat berguna untuk automation script.
INSTALASI SSHPass
Linux (Debian/Ubuntu)
sudo apt-get install sshpass
Linux (CentOS/RHEL)
sudo yum install sshpass
macOS
brew install hudochenkov/sshpass/sshpass # atau port install sshpass
METODE PENGGUNAAN SSHPass DENGAN Rsync
1. Method 1: Environment Variable
export SSHPASS="password123" sshpass -e rsync -avz /local/path/ user@103.115.31.218:/remote/path/
2. Method 2: Password dari File
# Simpan password di file echo "password123" > /tmp/mypass # Gunakan dengan rsync sshpass -f /tmp/mypass rsync -avz /local/path/ user@103.115.31.218:/remote/path/ # Hapus file password setelah digunakan rm /tmp/mypass
3. Method 3: Password Langsung (TIDAK DISARANKAN)
sshpass -p "password123" rsync -avz /local/path/ user@103.115.31.218:/remote/path/
CONTOH LENGKAP DENGAN RSYNC OPTIONS
Basic Sync dengan Password
sshpass -p "password123" rsync -avz \ /home/user/documents/ user@103.115.31.218:/backup/documents/
Dengan Progress dan Verbose
sshpass -p "password123" rsync -avz --progress \ /var/www/html/ user@103.115.31.218:/var/www/backup/
Dengan Custom SSH Port
sshpass -p "password123" rsync -avz -e "ssh -p 2222" \ /local/data/ user@103.115.31.218:/remote/data/
Dengan Delete Option (Sync Mirror)
sshpass -p "password123" rsync -avz --delete \ /source/folder/ user@103.115.31.218:/destination/folder/
Dengan Exclude Pattern
sshpass -p "password123" rsync -avz \ --exclude='*.tmp' --exclude='node_modules' \ /project/ user@103.115.31.218:/backup/project/
Dengan Bandwidth Limit
sshpass -p "password123" rsync -avz --bwlimit=1000 \ /large-files/ user@103.115.31.218:/storage/
KEAMANAN YANG LEBIH BAIK
Gunakan File dengan Permission Ketat
# Buat file password echo "password123" > ~/.sshpass chmod 600 ~/.sshpass # Hanya user yang bisa baca # Gunakan sshpass -f ~/.sshpass rsync -avz /local/ user@host:/remote/
Atau Lebih Baik Gunakan SSH Key
# Generate SSH key (jika belum punya) ssh-keygen -t rsa -b 4096 # Copy public key ke server ssh-copy-id user@103.115.31.218 # Sekarang rsync tanpa password rsync -avz /local/path/ user@103.115.31.218:/remote/path/
SCRIPT EXAMPLES
Backup Script dengan sshpass
#!/bin/bash # Variables SERVER="103.115.31.218" USER="username" PASSWORD="password123" LOCAL_DIR="/home/user/backup/" REMOTE_DIR="/remote/backup/" # Backup using rsync with sshpass sshpass -p "$PASSWORD" rsync -avz \ --progress \ --delete \ --exclude='*.tmp' \ "$LOCAL_DIR" "$USER@$SERVER:$REMOTE_DIR" # Check result if [ $? -eq 0 ]; then echo "Backup completed successfully!" else echo "Backup failed!" exit 1 fi
Script dengan Password File
#!/bin/bash PASS_FILE="/home/user/.backup_pass" REMOTE="user@103.115.31.218" # Check if password file exists if [ ! -f "$PASS_FILE" ]; then echo "Password file not found!" exit 1 fi # Sync with password file sshpass -f "$PASS_FILE" rsync -avz \ --progress \ --stats \ /important/data/ "$REMOTE:/backup/data/"
TROUBLESHOOTING
Debug Connection
# Test SSH connection first sshpass -p "password123" ssh user@103.115.31.218 "echo 'Connected!'" # Jika gagal, cek verbose sshpass -p "password123" ssh -vvv user@103.115.31.218
Handle Connection Timeout
sshpass -p "password123" rsync -avz \ -e "ssh -o ConnectTimeout=10 -o ServerAliveInterval=60" \ /local/ user@103.115.31.218:/remote/
Rsync dengan Retry
#!/bin/bash MAX_RETRIES=3 COUNT=0 while [ $COUNT -lt $MAX_RETRIES ]; do sshpass -p "password123" rsync -avz /local/ user@host:/remote/ if [ $? -eq 0 ]; then echo "Sync successful!" break else COUNT=$((COUNT+1)) echo "Attempt $COUNT failed. Retrying..." sleep 5 fi done
KELEMAHAN SSHPass
Risiko Keamanan:
❌ Password terlihat di command history
❌ Password terlihat di
ps aux❌ Less secure daripada SSH keys
Solusi Lebih Aman:
SSH Keys (recommended)
SSH Agent
Configuration Management Tools (Ansible, etc.)
ALTERNATIF TANKA SSHPass
Gunakan Expect Script
#!/usr/bin/expect spawn rsync -avz /local/path/ user@103.115.31.218:/remote/path/ expect "password:" send "password123\r" expect eof
Atau Setup SSH Key (RECOMMENDED)
# Generate key pair ssh-keygen -t rsa -b 4096 -C "your_email@example.com" # Copy to remote server ssh-copy-id user@103.115.31.218 # Now use rsync without password rsync -avz /local/path/ user@103.115.31.218:/remote/path/
KESIMPULAN
Gunakan sshpass hanya untuk:
Testing environment
Temporary automation
Systems where SSH keys aren't feasible
Untuk production, selalu prefer SSH keys untuk keamanan yang lebih baik!
Komentar
Posting Komentar