0

I have a disk (A) (hard drive SATA 2 TB 3.5) and I have a NAS D-Link DNS-320L (with exact model of hard drive SATA 2 TB 3.5). Both connected to a router (local network), 192.168.1.3 and 192.168.1.2 respectively.

How can I clone disk A (/dev/sda) to my NAS (//192.168.1.2/disk) with dd?

Hypothetical example (it doesn't work):

sudo dd if=/dev/sda |pv|dd of=//192.168.1.2/disk bs=1M conv=notrunc,noerror

Note: I need to clone (replicate) entire HD "A" to HD "B". HD "A" contains SO Ubuntu 16.04 x64, swap, MBR and others partitions.

Objective: If there is a damage in "A", replace it with "B" quickly.

10
  • 2
    I wouldn't try. that kind of low level copy seems a bad choice when moving between systems. I would use a higher-layer tool like rsync to copy the content, while leaving of the destination disks filesystem type, geometry, etc to the NAS. that way you get resumability if the process needs restarted and file-level integrity checking. Mar 25, 2017 at 2:55
  • mmm. Please give me an example of your proposal
    – acgbox
    Mar 25, 2017 at 2:59
  • 1
    Couple of things - 1 - You should not "dd" the live disk (ie /dev/sda) - it could work, but it will create some corruption on the filesystem because things will change, undetected, on the underlying disk while dd is running. 2 - If you are trying to create an image, you should mount the mountpoint, then dd to a file on the mountpoint, otherwise use a different mechanism to mirror the files rather then the block device. 192.168.1.2/disk is not a block device, its a share - and this is why you can't do a bit copy to it.
    – davidgo
    Mar 25, 2017 at 3:41
  • 1
    @bdc: Your approach is perfectly doable; I've done something similar to that to create image-based backups. However, you can't just say "of=192.0.2.1". Instead, you leave off "of=" and pipe output to another program (pv is a great choice, but then you need to pipe to another program) that can handle networking, like ssh or nc. (Actually, the method I've used (and documented) uses both ssh and nc.)
    – TOOGAM
    Mar 25, 2017 at 3:54
  • I recommend you install the new disk in the NAS, format it, and configure a root share. on the pc mount the share somewhere, and issue a command like rsync -axHAWXS --numeric-ids --info=progress2 /path/to/src /path/to/share/mount/ superuser.com/questions/307541/… Mar 25, 2017 at 3:54

1 Answer 1

0

I need to clone (replica) entire HD "A" to HD "B".

Remove HD "B" from your NAS and attach it directly to the system where HD "A" is. Then you can clone with pv or dd, ddrescue, or even with cp or cat I think, and it can be as straightforward as

sudo dd if=/dev/sda of=/dev/sdb

Use the options you need. (BTW, in your example conv=notrunc,noerror makes no sense. It would make sense with the first dd.)

And it's true you shouldn't do this when any filesystem from /dev/sda is mounted with write access or /dev/sdb is mounted in any way. This is what happens when your source can change during sequential cloning.

Your target disk (its partitions, filesystems and files) may or may not be seen by the NAS when you reinstall the disk there. The NAS may write some changes and spoil the clone (e.g. it may or may not change bootloader data in MBR and following sectors).


I know you want a solution where the target disk sits inside the NAS all the time. This is hard because your NAS doesn't expose the disk (the device) for you nor its partition(s), nor filesystem(s). It exposes files (existing in EXT3 filesystem) using CIFS/SMB, NFS, AFP or HTTP/HTTPS. To clone to the disk you need to be able to write to it on a sector level – I think your NAS won't let you do it just like that.

I said "this is hard", not "impossible", because if you could gain SSH access (or telnet, or so) to your NAS then you might be able to write to the disk on a sector level. You might find a Linux(?) shell with standard tools and maybe you could do something similar to this (from live Linux distro on a PC where the HD "A" is):

sudo dd if=/dev/sda bs=1M conv=notrunc,noerror | pv | ssh root@192.168.1.2 "dd of=/dev/your_target_disk bs=1M"

Or use nc to stream your data.

Edit 1. Although I'm not convinced this will work with the NAS in question (or any other NAS), I'm adding the nc-based solution because the OP explicitly requested it (in comments). The following solution will work for sure for "NAS" that is a regular computer with Linux.

Before you tell your NAS to listen for incoming data and pass it to the device, you should make sure no other process writes to the target disk; umount it if you can or at least unshare the device in the NAS configuration. Then:

# log in to NAS
nc -l 3210 | dd of=/dev/sda

The number 3210 is a port number, you can choose it from the range 1024..65535. Replace /dev/sda string with the right device. From now on your NAS awaits incoming connection. If somebody else connects to it on the chosen port then their data will be written to the device, but in a home environment this is hardly a concern. However if you are concerned about this then you should know nc will accept one connection only (one at a time and one in total i.e. it will exit when the first connection terminates). So if you successfully connect in the next step then you will know nobody else did.

Next tell your OS with the source disk (HD "A") to read it and push the data to the listening nc on your NAS. As /dev/sda should not be mounted (strictly: it can be mounted, but read-only only), you'd better perform this step from within some live Linux distro.

# where HD "A" is
dd if=/dev/sda | nc 192.168.1.2 3210

Use the dd options you need (like conv=notrunc,noerror). Note the 3210 is the listening port number I chose in the previous step.

In practice you may want to monitor the progress, so the better command may be:

dd if=/dev/sda | pv | nc 192.168.1.2 3210

Or you can request dd to report its progress by passing the USR1 signal to it. Run the following in another terminal:

kill -s USR1 $(pidof dd)

and the information will appear in the terminal where dd runs.

After dd finishes and there's no error (on neither side), you should reboot the NAS gracefully. It's very important to let it sync the target disk so don't just cut the power. Use whatever way of proper reboot or shutdown your NAS offers you.

Edit 1 ends here.

If you succeed in doing this then normal NAS operations may or may not spoil something in the clone, just as if you reinstalled the disk directly cloned to.


Few comments suggested to mount the share and write to it on a file level. You have at least two options:

  • you copy files (e.g. with rsync);
  • or you write the entire image of your HD "A" as a file placed in a filesystem on HD "B".

Both methods may be useful but neither is cloning. You won't be able to replace HD "A" simply with HD "B" as you want. Moreover the latter method won't work at all in your particular case because it needs free space on HD "B" filesystem to be bigger than the whole HD "A" (this restriction may not stand if you use sparse file, but you cannot use one with NFS, SMB etc., can you?). You may write a compressed file and it may fit, yet this is even further from cloning.

3
  • thanks for your reply. It is well documented and makes sense. However I have to wait a few days to see if there is any proposal that has not been considered. Otherwise I will select your answer as the best. By the way. There is another way to do it with netcat, but I'm not sure how reliable it is. clona.pbworks.com/w/page/7807251/Por%20red%20con%20netcat
    – acgbox
    Mar 28, 2017 at 16:06
  • Please. Add netcat + partclone alternative solution to your answer, to select it as the best. Thank you
    – acgbox
    Mar 28, 2017 at 17:08
  • @bdc I have never used partclone so I won't include it in my answer. The answer is now expanded with the general nc-based solution. Mar 28, 2017 at 19:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.