Tuesday, June 23, 2015

Passing password to the rsync command with sshpass

In some cases, when writing a bash shell script like running the rsync command multiple times, and you want to avoid entering username and password over and over again, you may find sshpass useful.

$ sudo apt-get install sshpass

Example script:

#! /bin/bash

for oldblogid in `cat $1`
do
        media=/var/www/wp-content/blogs.dir/$oldblogid  
        echo "=== Checking the existance of $media directory..."                                                                                       
        if [ -d "$media" ] 
        then
                echo "    Existed! Copying $media..."                                                                                                  
                sshpass -p 'your-ssh-password-for-myuser' rsync -arv $media -e ssh myuser@new.multisite.com:/home/myuser/sites/               
        fi
done