tar + ssh Filesystem Migration

Secure Archive and Transfer - 10 Practical Examples with Detailed Explanations

Example 1

Basic Directory Migration

$ cd /u02; tar cpf - * | ssh root@192.168.1.100 "(cd /u02; tar xpf -)"
# Streams tar archive through ssh and extracts on remote side # Preserves permissions, ownership, timestamps
Creates a tar archive on stdout, pipes through ssh to remote host, and extracts in target directory. The 'c' creates archive, 'p' preserves permissions, 'f -' writes to stdout. Remote side reads from stdin with 'x' extract and 'f -'. This is the gold standard for filesystem migration.
Why This Works: No intermediate files, preserves everything, encrypted transfer
Example 2

Migration with Progress Display

$ tar cpf - /data | pv | ssh user@remote "tar xpf - -C /"
1.2GiB 0:02:15 [9.2MiB/s] [=========> ] 45% ETA 0:02:48
Adds progress viewer (pv) between tar and ssh to show transfer progress, speed, and ETA. Install pv with your package manager. The '-C /' on remote side changes to root directory before extraction. Essential for monitoring large migrations.
Install pv: apt install pv (Debian/Ubuntu) or yum install pv (RHEL/CentOS)
Example 3

Compressed Transfer for Slow Networks

$ tar czpf - /var/www | ssh user@remote "tar xzpf - -C /"
# Data compressed before transfer, decompressed on arrival
Uses gzip compression with 'z' flag to reduce bandwidth usage. Compresses on source, decompresses on destination. Best for slow networks or large text-heavy directories. Note: adds CPU overhead on both ends.
Compression Options: 'z' for gzip, 'j' for bzip2, 'J' for xz
Example 4

Migration with Exclusions

$ tar cpf - --exclude='*.log' --exclude='tmp/*' /app | ssh user@remote "tar xpf - -C /"
# Excludes log files and tmp directory contents
Excludes specified patterns from the archive using --exclude flags. Can specify multiple exclusions. Perfect for skipping cache files, logs, or temporary data during migration. Reduces transfer time and destination space.
Pattern Matching: Use shell wildcards in exclusion patterns
Example 5

Preserve All Extended Attributes

$ tar cpf - --xattrs --acls --selinux /data | ssh root@remote "tar xpf - --xattrs --acls --selinux -C /"
# Preserves extended attributes, ACLs, and SELinux contexts
Explicitly preserves extended attributes, ACLs, and SELinux security contexts. Critical for complete system migrations where security contexts matter. Ensures exact replication of all file metadata.
Important: Both systems should support the same extended attributes
Example 6

Incremental Migration Using File Lists

$ find /data -newer /tmp/last_backup -print0 | tar cpf - --null -T - | ssh user@remote "tar xpf - -C /"
# Only transfers files modified since last backup
Uses find to locate changed files, creates tar from file list. The --null and -print0 handle filenames with spaces. -T - reads filenames from stdin. Efficient for incremental migrations or updates.
Incremental: Track changes for faster subsequent migrations
Example 7

Migration with Custom SSH Port

$ tar cpf - /opt | ssh -p 2222 user@remote "tar xpf - -C /"
# Connects to SSH on port 2222 instead of default 22
Specifies custom SSH port with -p flag. Common when SSH runs on non-standard ports for security. Ensure you know the correct port before attempting migration.
Security: Many admins change default SSH port to reduce automated attacks
Example 8

Verbose Migration for Troubleshooting

$ tar cvpf - /etc | ssh user@remote "tar xvpf - -C /"
etc/ etc/passwd etc/group etc/hosts ... # Lists each file as it's processed
Adds verbose flag 'v' to both tar commands to see each file being processed. Useful for troubleshooting, verifying what's being transferred, or monitoring progress on small directories.
Debugging: Verbose mode helps identify stuck or problematic files
Example 9

Migration with SSH Key Authentication

$ tar cpf - /home | ssh -i ~/.ssh/migration_key user@remote "tar xpf - -C /"
# Uses specific SSH key for authentication
Specifies a particular SSH private key with -i flag. Useful when you have multiple keys or need to use a dedicated migration key. Ensures proper authentication without password prompts during automated migrations.
Automation: Key-based auth essential for scripted migrations
Example 10

Bidirectional Verification Migration

$ tar cpf - /data | tee >(md5sum > /tmp/source.md5) | ssh user@remote "tee >(md5sum > /tmp/dest.md5) | tar xpf - -C /"
# Creates checksums on both ends for verification
Uses tee and process substitution to calculate checksums on both source and destination. Allows post-migration verification that data arrived intact. The md5sum files can be compared to ensure successful transfer.
Integrity Check: Compare checksums to verify successful migration