Sometimes you need to rsync something preserving permissions or rsync to a different remote user. But what if ssh access for root is closed (common policy) and all rights are managed via sudo on remote machine? No problem - this script will let you use rsync as you would use it normally but instead of logging in as different user via ssh it logs in with current user id and calls sudo on remote end. The call syntax of the script is the same as if you would call rsync directly, the only requirement is that destination should be specified as last argument (which is usualy what everyone is doing anyway). Here's the script code: #!/bin/bash # This is wrap around script ro use rsync with sudo # # usage rsync-sudo [rsync options] [[user@}host:]destloc # suntax is same as rsync but destination should be the last argument # # (c) Yevgeniy Aleynikov http://www.beingroot.com/ user=root dst=${!#} args=$# if [ $args -lt 2 ] ; then rsync -h exit 1 fi for a in `seq 1 ${args}` do if [ $a -lt ${args} ] ; then # skipping last argiment arg[a]=$1 fi shift; done echo "dst: ${dst}" user=root if [[ `expr index "${dst}" :` -eq 0 ]] ; then host=localhost ddir=${dst} else ddir=${dst##*:} htmp=${dst%%:*} host=${htmp##*@} if [[ ! `expr index "${dst}" @` -eq 0 ]] ; then user=${dst%%@*} fi fi echo "rsyncing to ${user}@${host}:${ddir}" stty -echo ssh ${host} sudo -v -S stty echo rsync -e ssh --rsync-path "sudo -H -u $user rsync" ${arg[@]} ${host}:${ddir} exit $? Enjoy! |
Articles > Useful scripts >