Contents
Tutorial: Clone a Disk Over SSH with Compression
This guide explains how to clone a disk (/dev/sda
) from a source machine to a destination machine over SSH using dd
and gzip
for compression.
Prerequisites
-
Source Machine:
- Ensure
dd
,gzip
, andssh
are installed.
- Ensure
-
Destination Machine:
- Ensure
gzip
andssh
are installed.
- Ensure
-
Network Setup:
- Both machines should be accessible over the network.
- You need SSH access to the destination machine.
Command Overview
The command performs the following:
-
Source Machine:
- Reads data from the disk (
/dev/sda
) usingdd
. - Compresses the data using
gzip
. - Sends the compressed data over SSH to the destination machine.
- Reads data from the disk (
-
Destination Machine:
- Decompresses the received data using
gzip
. - Writes the decompressed data to its disk (
/dev/sda
) usingdd
.
- Decompresses the received data using
Command Breakdown
On the Source Machine:
dd if=/dev/sda bs=5M conv=fsync status=progress | gzip -c -9 | ssh user@DestinationIP 'gzip -d | dd of=/dev/sda bs=5M'
dd if=/dev/sda
:- Reads the disk (
/dev/sda
) on the source machine.
- Reads the disk (
bs=5M
:- Sets the block size to 5 MB for efficient data transfer.
conv=fsync
:- Ensures data integrity by forcing a filesystem sync.
status=progress
:- Displays progress while copying.
gzip -c -9
:- Compresses the output of
dd
with maximum compression (-9
).
- Compresses the output of
ssh user@DestinationIP
:- Sends the compressed data to the destination machine over SSH.
'gzip -d | dd of=/dev/sda bs=5M'
:- Decompresses the received data and writes it to the disk (
/dev/sda
) on the destination machine.
- Decompresses the received data and writes it to the disk (
Steps to Execute
-
Identify the Disks:
- Use
lsblk
orfdisk -l
to identify the source and destination disks. - Example:
/dev/sda
.
- Use
-
Run the Command:
- Replace
user
with the SSH username. - Replace
DestinationIP
with the IP address or hostname of the destination machine. - Execute the command on the source machine.
- Replace
-
Monitor Progress:
- The
status=progress
option will display the data transfer progress.
- The
-
Verify:
- Once completed, verify the disk on the destination machine by checking its content or filesystem.
Example
Source Machine Command:
dd if=/dev/sda bs=5M conv=fsync status=progress | gzip -c -9 | ssh root@192.168.1.100 'gzip -d | dd of=/dev/sda bs=5M'
Explanation:
- Cloning
/dev/sda
from the source machine. - Sending data to the destination machine with IP
192.168.1.100
. - Using SSH with the
root
user.
Notes
- Data Loss Warning:
- Be careful with
dd
as it can overwrite data. Double-check the destination disk path.
- Be careful with
- SSH Key Authentication:
- Use SSH key authentication to avoid being prompted for a password during the process.
- Network Speed:
- The command's speed depends on network bandwidth and disk I/O.
Troubleshooting
- Permission Denied:
- Ensure proper disk access permissions on both machines.
- Slow Transfer:
- Use a larger block size (e.g.,
bs=10M
) to potentially speed up the transfer.
- Use a larger block size (e.g.,
This command and tutorial provide an efficient way to clone disks across machines securely and with compression.