Wednesday, September 3, 2008

Rsync Ssh Expect Backup Script

#!/bin/bash
#
# A script to backup various items to via sudo with a user when you can't use passkeys
#
#
USER="remoteuser"
PASS="remotepassword"

for x in \
server1:/home/user \
server1:/etc \
server1:/export \
server2:/home/user \
server2:/etc \
server2:/export \
server3:/home/user \
server3:/etc \
server3:/export \
; do

HOST=`echo $x|cut -f1 -d":"`
DIR=`echo $x|cut -f2 -d":"`

if [ ! -d /export/backups/$HOST$DIR ]; then
mkdir -p /export/backups/$HOST$DIR
fi

/usr/bin/expect -c "

spawn /bin/bash
expect \"]#\"
send \"/usr/bin/rsync --rsh='/usr/bin/ssh -l $USER' --rsync-path='/usr/bin/sudo /usr/bin/rsync' --delete -avz $HOST:$DIR/ /export/backups/$HOST$DIR\r\"
expect \"word: \"
send \"$PASS\r\"
expect \"]#\"
send \"exit\r\"

"

done