Excellent question. You've correctly identified a critical security risk: entering credentials for a trusted host (B) on a potentially compromised host (A) could leak those credentials. The standard scp hostA:/dir user@hostB:/dir or sftp from Host A falls into this trap.
The solution is to reverse the direction of the connection. Instead of "pushing" from A to B, you will "pull" from B to A. You will initiate the session from the more trusted host (or the destination host, in any case) to the potentially compromised source host. This way, you only expose the credentials of the potentially compromised host (A) on the destination machine (B).
Here are the best methods that meet all your criteria (permissions, ACLs, symlinks, root, etc.).
Method 1: rsync in Pull Mode (Recommended)
This is the most common and flexible solution. rsync is a powerful tool designed for exactly this kind of high-fidelity, recursive copy.
Concept: You log into Host B and run a command that connects to Host A to "pull" the files.
Steps:
-
SSH into your destination machine, Host B.
ssh your_user@hostB
-
From Host B, run the rsync command. You will be prompted for the password for root@hostA.
sudo rsync -aAXv --numeric-ids --progress root@hostA:/path/to/source_directory/ /path/to/destination_directory/
Explanation of rsync Flags:
sudo: You need to run rsync as root on the destination (Host B) to be able to set the original ownership of the files.
-a (archive): This is a magic flag that's equivalent to -rlptgoD. It ensures:
-r: recursive copy
-l: symlinks are copied as symlinks
-p: permissions are preserved
-t: modification times are preserved
-g: group is preserved
-o: owner is preserved (requires root)
-D: device and special files are preserved (requires root)
-A: Preserves POSIX ACLs, which you specifically requested.
-X: Preserves extended attributes (e.g., SELinux contexts).
-v: Verbose output, so you can see what's happening.
--progress: Shows progress for large files.
--numeric-ids: Crucial. This tells rsync to not map user and group names. It uses the numeric UID and GID from Host A and applies them directly to Host B. This is essential if uid 1001 is user_a on Host A but user_b on Host B. You want the numeric ID preserved.
root@hostA:...: You connect to Host A as root to ensure you can read all files, regardless of their permissions.
How it Solves the Security Problem:
You are typing the password for Host A while on Host B. If Host A is compromised with a keylogger, it cannot steal the credentials for Host B. The connection is initiated from B to A.
Method 2: tar over ssh Pipe (The Classic Unix Way)
This method is excellent for a one-time full copy. It streams a tar archive from the source directly to a tar extractor on the destination without creating an intermediate archive file.
Concept: From Host B, you use ssh to run tar on Host A. The output of that tar command (the archive stream) is piped over the SSH connection to a local tar command on Host B, which extracts it.
Steps:
-
SSH into your destination machine, Host B.
ssh your_user@hostB
-
From Host B, run the tar pipe command.
ssh root@hostA 'tar --create --acls --xattrs --to-stdout -C /path/to/source_directory/ .' | sudo tar --extract --preserve-permissions --acls --xattrs -C /path/to/destination_directory/ --to-stdin
Explanation of tar Commands:
How it Solves the Security Problem:
Same as with rsync. The connection is initiated from B, and only credentials for A are used on B's command line.
Method 3: The Air-Gapped / Removable Media Method (Most Secure)
If you believe both hosts could be compromised or you want to eliminate network-based risks entirely, the only true way is to avoid a direct network connection.
Concept: Create a tar archive on an external drive, physically move the drive, and extract it on the destination.
Steps:
-
On Host A: Plug in an external drive (e.g., a USB HDD).
# Mount the drive first, e.g., to /mnt/external
mount /dev/sdc1 /mnt/external
# Create the high-fidelity tarball on the external drive
sudo tar --create --preserve-permissions --acls --xattrs --file=/mnt/external/backup.tar.gz --gzip -C /path/to/source_directory/ .
# Unmount when done
umount /mnt/external
-
Physically unplug the drive from Host A and plug it into Host B.
-
On Host B: Mount the drive and extract the archive.
# Mount the drive
mount /dev/sdb1 /mnt/external
# Extract the archive into the destination
sudo tar --extract --preserve-permissions --acls --xattrs --file=/mnt/external/backup.tar.gz -C /path/to/destination_directory/
# Unmount when done
umount /mnt/external
How it Solves the Security Problem:
There is no network connection between Host A and Host B, completely eliminating the risk of credential theft over the network. This is the most paranoid and secure method.
Summary and Comparison
| Method | How it Works | Security Posture | Best For |
|---|
rsync (Pull) | (Recommended) Initiates connection from B to A. | Protects B's credentials from A. Requires exposing A's credentials on B. | General use, especially for updating a copy, as it only transfers differences. |
tar over ssh | tar stream from A piped to tar extractor on B. | Same as rsync. Protects B's credentials from A. | Efficient one-time full transfers. Less efficient for subsequent updates. |
| Removable Media | Physical transfer of a tar archive file. | Most Secure. No direct network connection. No credentials exchanged. | Highest security requirements, offline environments, or when direct networking is not possible. |
For your use case, the rsync pull method is almost certainly the best balance of security, features, and convenience.