Using dd to clone a disk while mounted – risks?

I want to clone a hard disk using dd. Because I want to keep a process on the machine alive continuously, I would like to do this while the filesystem is still mounted. I understand this is not the “ideal” way to do this, but it also seems from Googling that it is possible.

The clone is being used as a backup; in the event of a hard disk failure I would like to have an image to dd back onto a new hard disk. The OS that is running lives on the disk I want to clone.

The process I have running does do some disk I/O but not with the disk I wish to clone. As far as I know, only the OS/system processes would be reading or writing to the disk while I do this operation. What I want to know is if this light use is likely to ruin the whole cloned image? I imagine that there’s a danger of getting a few files corrupted if they’re being written as they are read by dd, but I have no idea how likely it is to ruin the backup. Can anyone share some insights?

Short of putting the it on a disk and trying to start it, is there any way I can verify the integrity of the image?

Thanks!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

If you’re lucky, the filesystem corruption will be detected as soon as you try to mount the copy. If you’re unlucky, it won’t be detected until later.

It’s also possible that you’ll manage to get a consistent copy of the filesystem except for the files that were modified during the copy. But I wouldn’t count on it. It might work with ext4 as long as you don’t create, delete or move any file, so that the directories aren’t modified.

If you copy a filesystem that’s mounted read-only, of course, everything will be fine. Except that you shouldn’t use dd, use cat instead.

There are several reliable ways to clone a disk. Pick one of these, rather than one that practically guarantees corruption.

  • Some filesystems offer a clone functionality, for example btrfs. I don’t think ext4 does.
  • If the filesystem is on Linux’s native partition scheme, i.e. an LVM volume, you can make an LVM snapshot. That requires that you use LVM, rather than putting the filesystem directly on some other partition scheme such as MBR or GPT. You’ll be left with a filesystem that wasn’t cleanly unmounted, but represents a consistent snapshot of the original at a point in time.
  • If you can get the filesystem onto a RAID-1 array, you can clone it by adding a member to the array, waiting for it to synch, and detaching the new member. Here too you’ll have a consistent but not clean snapshot. You can create a RAID-1 volume around an existing filesystem, but that requires an offline step to shrink the filesystem by 128kB.
  • You can make a file-level backup. That won’t get you a consistent view of the filesystem, since copying files takes time, but it does guarantee at least that every file that wasn’t modified during the backup will be backed up correctly.

Method 2

There is no harm in running a dd while running the server. The integrity of the backup depends mostly of what the server does, and what the partitions have there hosted.

Mostly static content has no problems at all, expect some problems with DB or log partitions. I would recommend to stop the DB servers on DB partitions.

If you are also backing up transaction-based filesystems, the backups should recover in no time.

dd could be used mostly to backup some server as it is, however you would need also file backups done at higher level.

dd will work quite well as a cloning alternative if the server is booted for an alternate medium and there is no activity at all.

As the previous post says, unless you have a specific need to used dd, there are better alternatives at there, free and commercial.

To name a few:

Clonezilla
Systemimager
dump / restore

Method 3

The utility dd is a really basic tool. It operates at the raw bytes level of whatever you point it at. The upshot of this is that if something is writing to a file, you might copy the file during the time it’s being written, likely resulting in a corrupted file, and certainly one that is simply wrong.

The things you would need to do to do this safely are already implemented in a variety of backup programs. It’s simplest to use one of those.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x