7

I'm trying to clone a hard drive on my raspberry pi which is on the same network to an external harddrive of the computer I'm currently on. However I'm having issues piping the command correctly. I've tried:

ssh user@hostip "dd if=/dev/sda" | sudo dd of=/dev/sdc

But the prompts for the sudo and the ssh connection stack together.

3 Answers3

8

Using netcat - no unneeded load on your CPU if you're not concerned about privacy (i.e. your home network is yours really)

On your host, run

cat /dev/sda | nc -n ipaddr 10000

where ipaddr is IP address of your PI

On your PI, run

nc -l 10000 >/dev/sdc

All commands are to be run as root. OpenBSD netcat is assumed to be your nc version there (can be found in LibreSSL), but others should work too.

5

Some simple workarounds without changing your command line:

  • Use key authentication (with ssh-agent if the key file is password-protected) instead of password authentication for SSH. It's usually a good idea anyway.
  • Use SSH's connection multiplexing to first open a connection (e.g. ssh -o ControlMaster=yes -M -f sleep 999999), then run the copy in a slave connection, which doesn't require authentication. See Get indication of ssh command for an example.
  • Run sudo true just before running this command. By default sudo caches the information that it's verified your password for 15 minutes (at least if you do it on the same terminal, that depends on the configuration).

If you really must type both passwords in the command due to an unusually restrictive configuration, you can use a named pipe to separate the commands:

mkfifo pipe
ssh user@hostip cat /dev/sda >pipe
# and in another terminal
sudo sh -c 'cat >/dev/sdc' <pipe

Also, use cat instead of dd, it's faster and more reliable.

  • I am indeed using a private key, but I wasn't aware of ssh-agent - cool stuff! I am interested in using cat though based on the links you posted. Do you have any recommended reading on backing up HDs with cat? – SHiLLySiT Nov 15 '15 at 00:04
  • @SHiLLySiT cat is simple to use: it copies its input to its output. That's what you were doing with dd, only slower. – Gilles 'SO- stop being evil' Nov 15 '15 at 00:19
2

Do you actually need a bit-for-bit copy of the entire remote disk? e.g. for digital forensics?

If not, if you just need a copy of the partitions and the data on them, you can save a lot of time and transfer bandwidth by:

  1. copying the partition table, e.g. with sfdisk.

For example:

ssh user@hostip sudo sfdisk -d /dev/sda | sudo sfdisk /dev/sdc

or in two steps with a tempfile:

ssh user@hostip sudo sfdisk -d /dev/sda > /tmp/sda.txt
sudo sfdisk /dev/sdc < /tmp/sda.txt
  1. using partclone to clone only the parts of the filesystem that are currently in use, ignoring all the empty space and deleted files etc.

For example:

ssh user@hostip sudo partclone.ext4 -c -s /dev/sda1 -o - | 
    sudo partclone.ext4 -r -s - -o /dev/sdc1

partclone supports all common and some uncommon fileystems used on Linux (ext2/3/4, btrfs, xfs, etc) and Windows (NTFS, FAT).

partclone is packaged for most distros, including debian. sfdisk is part of the util-linux package.

You still need to solve the ssh / sudo issue, but @Gilles has given a good answer for that.

The easiest way to eliminate the password prompts is to do all of this as root (sudo -i to get a root shell), so you don't need to preface every command with sudo, and to (as root) run ssh-copy-id hostip to install root@localhost's ssh key in ~/.ssh/authorized_keys on hostip. Then you can run:

ssh hostip sfdisk -d /dev/sda | sfdisk /dev/sdc
ssh hostip partclone.ext4 -c -s /dev/sda1 -o - | 
    partclone.ext4 -r -s - -o /dev/sdc1

If you're running ssh-agent, you'll only have to type in the passphrase for root's key once at the most (or not at all if you've used it previously).

cas
  • 78,579