-
-
Save erikhansen/420080f13b83b63045e02f0fa79a350d to your computer and use it in GitHub Desktop.
rsync examples (two remotes)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# rsync with sudo | |
rsync -av \ | |
--rsync-path="sudo -u www-data rsync" \ | |
-e 'ssh -o StrictHostKeyChecking=no' | |
path_to_local_data/ [email protected]:/var/www/ | |
# rsync media between two remotes that can not talk to each other | |
# this creates a port (50000) on the origin server that establishes a tunnel between the origin and destination through | |
# your computer, as long as your computer can access both servers you can rsync the files between the two | |
# You will need to either: | |
# 1. Put public key from origin server (origin.net) into authorized_keys of destination server (destination.com) | |
# 2. Add a `-A` flag to the ssh command so that your computer's private key gets forwarded for authentication purposes | |
ssh -R 50000:destination.com:22 origin.net 'rsync -e "ssh -o StrictHostKeyChecking=no -p 50000" -av \ | |
~/origin.net/htdocs/media/ root@localhost:/var/www/htdocs/media/' | |
# push/send from origin to send to destination | |
# authorize origin server to login to destination server | |
ssh -R 50000:${DESTINATION_HOST}:22 ${ORIGIN_USER}@${ORIGIN_HOST} "rsync -e 'ssh -o StrictHostKeyChecking=no -p 50000' -av \ | |
${ORIGIN_SHARED_PATH}/pub/media/ ${DESTINATION_USER}@localhost:${DESTINATION_SHARED_PATH}/pub/media/" | |
# pull/get from destination to get from origin | |
# authorize destination server to login to origin server | |
ssh -R 50000:${ORIGIN_HOST}:22 ${DESTINATION_USER}@${DESTINATION_HOST} "rsync -e 'ssh -o StrictHostKeyChecking=no -p 50000' -av \ | |
${ORIGIN_USER}@localhost:${ORIGIN_SHARED_PATH}/pub/media/ ${DESTINATION_SHARED_PATH}/pub/media/" | |
# Three way file transfer of backup file adding file timestamp when sending to destination | |
# | |
# An SSH connection is established with the destination and key forwarding | |
# is used in order to establish the ssh connection from the destination to the origin | |
# and initiate the rsync from the destination. A progress indicator exists to monitor | |
# the file transfer progress. | |
# | |
# The rsync command escalates the permissions to root with sudo to pull a backup file | |
# that was generated and only accessible by the root user. The destination file will | |
# be owned by the destination user. | |
declare ORIGIN_HOST="my_origin_hostname" | |
declare ORIGIN_USER="my_origin_username" | |
declare DEST_HOST="my_destination_hostname" | |
declare DEST_USER="my_destination_username" | |
declare BACKUP_FILE_NAME="backup_filename.sql.gz" | |
declare BACKUP_FILE_PATH="/the/path/to/the/file/you/want/to/transfer/" | |
declare DEST_PATH="/the/path/you/want/the/destination/file/to/reside/at/" | |
declare BACKUP_DATE=$(\ | |
ssh -q ${ORIGIN_USER}@${ORIGIN_HOST} "\ | |
date -d @\$(sudo stat -c %Y ${BACKUP_FILE_PATH}${BACKUP_FILE_NAME}) +%Y%m%d \ | |
"\ | |
) | |
declare TUNNEL_HOST="localhost" | |
declare TUNNEL_PORT="50000" | |
ssh -A -R ${TUNNEL_PORT}:${ORIGIN_HOST}:22 ${DEST_USER}@${DEST_HOST} "rsync \ | |
-e 'ssh -l ${ORIGIN_USER} -o StrictHostKeyChecking=no -p ${TUNNEL_PORT}' -av --progress --rsync-path=\"sudo rsync\" \ | |
${TUNNEL_HOST}:${BACKUP_FILE_PATH}${BACKUP_FILE_NAME} ${DEST_PATH}${BACKUP_DATE}_${BACKUP_FILE_NAME}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: To get the "rsync media between two remotes" working, I had to add the
-A
flag to thessh
command as I was getting aPermission denied (publickey)
error.