Working with rsync on AWS Ubuntu

I’m migrating to a new AWS instance and so I need to bring a bunch of directories with me. I could do it the easy (and slow) way and copy them to my computer then to the new server. This would be drag and drop using SFTP (automatically brought up when I SSH into my server on MobaXterm). Currently this is how I upload a web page into an Apache web directory..

The more standard way using Linux command line is by rsync. I will use a command like:

sudo rsync --dry-run --delete -azvv -e ssh /home/path/folder1/ remoteuser@remotehost.remotedomain:/home/path/folder2

  1. In this command is an SSH connection, which means I need to be able to SSH to the remote machine from the machine running rsync. Just to brush up on SSH, I’ll SSH to my old AWS instance (where the web files currently are):
    • Create some new SSH credentials on the new host for rsync to use.
    • Copy the public key to the old host and add it to the authorized_keys file.
    • Create a new AWS security group rule for the old host allowing SSH from my new hosts private AWS IP (127.*.*.*/32).
    • Test that I can SSH from the new host to the old
      ssh <username>@<AWS private IP> -i "private_key_file"

      This brings up:

      "The authenticity of host <host> can't be established
      ECDSA key fingerprint is so-and-so"

      I can check so-and-so by going to the old host and entering:

      ssh-keygen -l -f /etc/ssh/ssh_host_ecdsa_key.pub
    • Logout of the SSH session within an SSH session with command ‘logout’.
    • Create a config file for this new connection..
      Now All I have to enter is

      ssh AWS1
  2. Now I want to test rsync through that SSH tunnel we just got working..
  3. One would think that rsync would use the config file for SSH, nope, so I still have to include the -i flag in quotes for it to work. No big deal. I could possibly get around this using this solution, but I can’t be bothered. So my rsync command looks like
     sudo rsync --dry-run --delete -azvv -e 'ssh -i /home/ubuntu/.ssh/id_rsa_O2' ubuntu@host-ip:/var/www/target/ "/home/ubuntu/dest/"
  4. I might just make a snapshot before I run these commands.
  5. And it all works fine..

Leave a Reply

Your email address will not be published. Required fields are marked *