Rsync a directory with direct root login disabled and authentication with wheel user with key
1. Add the rsync user to sudoers file on the destination server to run rsync as root user.
>> On destination server
vi /etc/sudoers
rsyncuser ALL= NOPASSWD:/usr/bin/rsync
2. From source server, run rsync command on the source server including the below option
—rsync-path=”sudo rsync”
3. If the server is using key based authentication instead of password, we can include it in the rsync
‘ssh –i ssh.key’
where ssh.key contains the public key and the permission of the file should be 600
An eg. for rsync including the both options
rsync -avub -e ‘ssh -i ssh.key’ –rsync-path=”sudo rsync” /home/user/public_html/ ubuntu@ip:/home/user/public_html/
(rsync -avub -e ‘ssh -i ssh.key’ –rsynce-path=”sudo rsync” /source/location/ user@destinationip:/destination/location)
Here I include a sample script for doing rsync with the above options
#! /bin/bash
if [ `ps -ef | grep rsync | grep -v grep | wc -l` -eq 0 ]
then
echo “running rsync command”
rsync -avub –delete -e “ssh -i pem” –rsync-path=”sudo rsync” /home/user/public_html user@ip:/home/user/public_html/
rsync -avub –delete -e “ssh -i pem” –rsync-path=”sudo rsync” /home/user/public_html/.htaccess user@ip:/home/user/public_html/
fi