AKZN Notes

Archives for My Lazy and Forgetful Mind

Migrate vps to a new vps

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

  1. Source Machine:

    • Ensure dd, gzip, and ssh are installed.
  2. Destination Machine:

    • Ensure gzip and ssh are installed.
  3. 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:

  1. Source Machine:

    • Reads data from the disk (/dev/sda) using dd.
    • Compresses the data using gzip.
    • Sends the compressed data over SSH to the destination machine.
  2. Destination Machine:

    • Decompresses the received data using gzip.
    • Writes the decompressed data to its disk (/dev/sda) using dd.

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.
  • 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).
  • 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.

Steps to Execute

  1. Identify the Disks:

    • Use lsblk or fdisk -l to identify the source and destination disks.
    • Example: /dev/sda.
  2. 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.
  3. Monitor Progress:

    • The status=progress option will display the data transfer progress.
  4. 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.
  • 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

  1. Permission Denied:
    • Ensure proper disk access permissions on both machines.
  2. Slow Transfer:
    • Use a larger block size (e.g., bs=10M) to potentially speed up the transfer.

This command and tutorial provide an efficient way to clone disks across machines securely and with compression.

Leave a Reply

Your email address will not be published.