This guide uses rsync and SSH Agent Forwarding to securely and efficiently copy folders between two servers.
Use this when you can SSH into the source server.
-
On Your Local PC (Prep Agent): Load the key for the destination server.
eval $(ssh-agent -s) ssh-add ~/.ssh/key_for_dest_server
-
On Your Local PC (Connect to Source): Use
-Ato forward your agent.ssh -A user@source-server
-
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/
- To copy the folder itself:
Use this when you can only SSH into the destination server.
-
On Your Local PC (Prep Agent): Load the key for the source server.
eval $(ssh-agent -s) ssh-add ~/.ssh/key_for_source_server
-
On Your Local PC (Connect to Destination): Use
-Ato forward your agent.ssh -A user@dest-server
-
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/
- To copy the folder itself:
- SSH Agent Forwarding (
ssh -A): This is the magic that lets a remote server securely use your local SSH keys. If you get aPermission deniederror, the first thing to check on your local PC isssh-add -lto see if the correct key is loaded. - The
rsyncTrailing Slash: This is critical./folder➡️ copies the folder itself./folder/➡️ copies only the contents inside the folder.
rsync -avzflags:- a: archive mode (preserves everything).
- v: verbose (shows progress).
- z: compress (speeds up transfer).