Skip to content

Instantly share code, notes, and snippets.

@ramithuh
Last active October 1, 2025 15:32
Show Gist options
  • Select an option

  • Save ramithuh/f4d0b83d921a988f62965d5fbfd84b21 to your computer and use it in GitHub Desktop.

Select an option

Save ramithuh/f4d0b83d921a988f62965d5fbfd84b21 to your computer and use it in GitHub Desktop.
copying

Cheat Sheet: Copying Folders Between Remote Servers

This guide uses rsync and SSH Agent Forwarding to securely and efficiently copy folders between two servers.

Method 1: Push from Source Server 📤

Use this when you can SSH into the source server.

  1. On Your Local PC (Prep Agent): Load the key for the destination server.

    eval $(ssh-agent -s)
    ssh-add ~/.ssh/key_for_dest_server
  2. On Your Local PC (Connect to Source): Use -A to forward your agent.

    ssh -A user@source-server
  3. On the Source Server (Push with rsync):

    • To copy the folder itself:
      rsync -avz /path/to/folder user@dest-server:/path/to/destination/
    • To copy only the folder's contents:
      rsync -avz /path/to/folder/ user@dest-server:/path/to/destination/

Method 2: Pull to Destination Server 📥

Use this when you can only SSH into the destination server.

  1. On Your Local PC (Prep Agent): Load the key for the source server.

    eval $(ssh-agent -s)
    ssh-add ~/.ssh/key_for_source_server
  2. On Your Local PC (Connect to Destination): Use -A to forward your agent.

    ssh -A user@dest-server
  3. On the Destination Server (Pull with rsync):

    • To copy the folder itself:
      rsync -avz user@source-server:/path/to/folder /path/on/this/server/
    • To copy only the folder's contents:
      rsync -avz user@source-server:/path/to/folder/ /path/on/this/server/

💡 Key Reminders

  • SSH Agent Forwarding (ssh -A): This is the magic that lets a remote server securely use your local SSH keys. If you get a Permission denied error, the first thing to check on your local PC is ssh-add -l to see if the correct key is loaded.
  • The rsync Trailing Slash: This is critical.
    • /folder ➡️ copies the folder itself.
    • /folder/ ➡️ copies only the contents inside the folder.
  • rsync -avz flags:
    • a: archive mode (preserves everything).
    • v: verbose (shows progress).
    • z: compress (speeds up transfer).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment