1

I want to use dd to clone my running Linux OS which is installed on a PI4 MicroSD 64GB media, to an image. Because it is a Raspberry PI I cannot (easily) boot from a USB stick and run the job with the PI4 OS not running. If I do such a job, using a console login, can I restore later without issue? Essentially can dd create a proper, restorable image of the main drive (/dev/sda) from a terminal session from within a running system?

  • 2
    Theoretically, it depends on whether the disk is writing or not. Why not just plug that disk onto a computer and dd it off-line? – 炸鱼薯条德里克 Nov 20 '19 at 1:52
  • Thank you. I did not have that option initially because it is a MicroSD but I can borrow a Card Reader and do what you say. I wanted to see if 'dd' could replace 'dump' or 'cpio' on a live system, by some means. – robertdaleweir Nov 20 '19 at 17:29
  • I had thought that being at runlevel 1 might lower the Disk activity, particularly writing. Thanks... – robertdaleweir Nov 20 '19 at 18:19
  • dd is usually not a good option. Depending on what you want, you might want tar to make an file-based archive instead of block-based byte-by-byte copy. If you really need that, you can use bash as your init, so no other process would be writing to disk, but notice that bash itself also store .bash_history on disk, so this still cause data corruption – 炸鱼薯条德里克 Nov 21 '19 at 11:53
  • 1
    Does your question have to be solved with dd, or can we use pv, and possibly other utilities, please clarify this in your question. Cheers. – LinuxSecurityFreak Nov 22 '19 at 7:24
4

There are two general types of methods how you can clone GNU/Linux to another hard drive, an SD card or a floppy. The first is when you clone a whole file system with everything it contains and when you copy all required files of your system and configure it to work somewhere else.

The first type: clone the whole file system

dd is the most reliable and old-fashioned way to clone the whole file system. If you want to make an image of your entire disk with all partitions in it you can simply execute in your terminal

dd if=/dev/sdX of=/path/backup_sdX.img 

and then

dd if=/path/backup_sdX.img of=/dev/sdY

to deploy your image on sdY disk. It may require sudo. backup_sdX.img will have the same size as the whole sdX. Use lsblk to figure out sdX and sdY.

Be careful with dd command. It may irreversibly erase your operating system and all your data.

dd doesn't have a verbose option and you might want to see progress of dd with pipe data monitor pv. Make sure you've installed pv (for Debian or Ubuntu)

sudo apt-get install pv

pv /dev/sdX | dd of=/path/backup_sdX.img

It may require sudo before pv and dd command. If you have SSH access from your Raspberry PI to another machine, then you can do

dd if=/dev/sdX | ssh username@hostname "dd of=/path/backup_sdX.img"

or

pv /dev/sdX | ssh username@hostname "dd of=/path/backup_sdX.img"

to see progress. hostname could be a local IP address of another machine. Make sure you have enough space on your receiving size because file.img will be the same size as your whole sdX disk.

You can even mount your image to your system.

sudo mount /path/backup_sdX.img /mnt 

which could be helpful if you want to change something. If it won't work for some reason try this.

Why you might dislike this method:

  • It may take a long time to copy each block of your disk, especially if that disk is huge.
  • sdY should have a bigger size than sdX.
  • sdY will have the same partitions of sdX and will be simply resized to sdX (of course you can fix that later but with a risk to lose your data).
  • You could be very disappointed if you mix up with sdY.

Once you understand all the advantages and disadvantages of using dd command, you would probably want to try something more specific as clonezilla, partclone, or partimage.


The second type: copy and configure

Simply clone the required files of your operating system and configure it. This is a much more advanced way to clone a system.

  • make partitions on your target hard drive with gparted, parted or any other program
  • mount all required partitions
  • transfer files using rsync which could be done via SSH or just DHCP network locally
  • change /etc/fstab
  • install or update grub or configure uefi (if you use it) though chroot

A good guide on how to do this could be found here.

| improve this answer | |
  • 1
    Concerning your remark "dd doesn't have a verbose option and you might want to see progress of dd with pipe data monitor pv." -- there is an improved version of dd called dcfldd which is available (at least) on Debian-based distributions and has enhanced output options, including status updates. Otherwise: nice answer! – AdminBee Nov 20 '19 at 8:40
  • Thank you Ivan. – robertdaleweir Nov 20 '19 at 17:29
  • I have used 'dd' to clone 'offline' drives and partitions. I was wondering if there was a way to use it on a running system. I can see that it is primarily for 'offline' media. I do not use LVM and only ext[3,4] filesystems. Thank you very much for your post. I will keep it for my usage as it is so thorough. :-) – robertdaleweir Nov 20 '19 at 18:13
  • Don't bother with dd, especially as you're using 512 byte blocks, so generating 8 writes per SDD block. Just use cat. – roaima Nov 20 '19 at 23:58
  • 1
    @roaima Ok, well, you are correct there. I avoid dd for performance reasons too. But I personally prefer seeing a progress bar and time estimate, which is why I recommend pv whenever possible. Cheers! – LinuxSecurityFreak Nov 22 '19 at 8:43

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.