How to Preserve Full Remote Path Structure When Using rsync for Backups


3 views

A common frustration when using rsync is that it doesn't automatically preserve the full remote path structure by default. When transferring files from a remote server to a local backup directory, you often end up with just the final directory rather than the complete path hierarchy.

The key to solving this is the --relative (or -R) flag. This tells rsync to keep the relative path structure intact:

rsync -avzR saturn:/home/udi/files/pictures/ ./backup/

This will create the directory structure backup/home/udi/files/pictures/ locally rather than just backup/pictures/.

If you need more control over the path structure, consider these alternatives:

1. Using the --include and --exclude pattern:

rsync -avz --include='/home/udi/files/pictures/***' --exclude='*' saturn:/ ./backup/

2. Changing to the root directory first:

cd backup && rsync -avz saturn:/home/udi/files/pictures .

Be extremely careful with trailing slashes in your paths as they affect rsync's behavior:

  • saturn:/home/udi/files/pictures/ - Copies the contents of pictures
  • saturn:/home/udi/files/pictures - Copies the pictures directory itself

Here's how to sync multiple paths while maintaining their structure:

rsync -avzR \
    saturn:/home/udi/files/pictures/ \
    saturn:/var/log/apache2/ \
    ./backup/

This will create both backup/home/udi/files/pictures/ and backup/var/log/apache2/ with their complete directory trees.


When performing remote backups with rsync, many administrators encounter situations where they need to maintain the complete source server path structure in the destination directory. This becomes particularly important when backing up files from multiple servers to a central backup location.

By default, rsync copies only the specified directory's contents to the destination. For our example:

rsync -avz saturn:/home/udi/files/pictures/ /backup/

This would create /backup/pictures/ rather than preserving the full path structure.

The most effective solution is using the --relative or -R option:

rsync -avzR saturn:/home/udi/files/pictures/ /backup/

This preserves the complete path structure, creating /backup/home/udi/files/pictures/ as desired.

For more control over path construction, consider these methods:

# Method 1: Using chdir
(cd / && rsync -avz saturn:/home/udi/files/pictures/ /backup/)

# Method 2: Explicit path construction
rsync -avz saturn:/home/udi/files/pictures/ /backup/home/udi/files/pictures/

When dealing with complex backup scenarios, combine options for better control:

rsync -avzR --no-implied-dirs saturn:/home/udi/./files/pictures/ /backup/

The ./ tells rsync where to start the relative path construction.

Here's a complete backup script that preserves paths while handling multiple directories:

#!/bin/bash
REMOTE="saturn"
DEST="/backup"
DIRS=(
    "/home/udi/files/pictures"
    "/var/log"
    "/etc"
)

for dir in "${DIRS[@]}"; do
    rsync -avzR --delete "${REMOTE}:${dir}" "${DEST}"
done

If you encounter problems:

  • Ensure trailing slashes are used correctly
  • Verify permissions on destination directories
  • Check for sufficient disk space
  • Test with --dry-run first