Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What I need to have continious mirroring of the whole notebook hard drive into a file on the exernal usb hard drive. Sometimes the external drive could be unplugged.

I'm running Ubuntu Linux.

share|improve this question
up vote 2 down vote accepted

You can set up a mirror, that will keep a copy off all data on a second disk.

If you disks are of equal size use:
mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sda /dev/sdb
(Assumign sda is your first disk and sdb your second (the USB) disk)

If they are not equal size you can do the same with a partition:
mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sda1 /dev/sdb1
(Note the extra letter).

If you boot with the USB drive missing then mdadm will complain about the mirror being in a degraded state, but you can still use it. When you plug in the drive it can be resynchronised.

[Edit] As pointed out in a comment I indeed missed the "backup to a file on the external drive"

You can create a file on the external drive using dd. E.g. for a 30GB file use dd if=/dev/zero of=/mnt/my_usb_drive/my_backupfile bs=1M count=30*1024. Then use loopback to mount that file as a block device and run mdadm over the newly created block device.

The downside to this is that you will have to add some hotplug rules which detect when an USB drive is mounted, check if it is the right disk and, if it is the right disk and not another USB drive, run a script to start the loopback mount and resync.

All of this is technically possible, but a simple filesystem dump or an rsync to a directory on the USB disk is probably a much better solution.

share|improve this answer
Not actually what Errandir asked for (they want to use an image file for the mirror, or at least, that's how I read it), but likely to be a better solution. – Darael Feb 5 '13 at 8:51
Ah, missed that part. :( The same is possible with any block device and a file can be mounted via loopback, resulting in a block device. That would technically work but at the cost of needless complexity. Still, will edit the answer and add that. – Hennes Feb 5 '13 at 12:26
There is one major problem. There is the only disk in notebook and system runs on it, thus mdadm: device /dev/sda not suitable for any style of array. So i need somehow to umount the whole drive that has several logical partitions… – Timofey Gorshkov Feb 6 '13 at 14:14
There are several posts on this site which describe how to turn a regular disk into a part of a mirror, requiring only two drives. However given the extra loopback stuff that might get tricky. I guess it is possible with a liveCD. (Note if/before you attempt that, make a backup.) – Hennes Feb 6 '13 at 15:12
For future reference: This post has information on running scripts on plugin.… – Hennes Mar 4 '13 at 1:55

I actually did this a totally different way, using DRBD (protocol A). This allowed me to plug and unplug the USB drive without having to resync the entire thing, and gave me a speed increase on writing to my internal SSD. My solution is

share|improve this answer

Some hints related to the problem:

  1. To get size of device in bytes run blockdev --getsize64 /dev/sda
  2. To create large file quickly run dd if=/dev/zero of=/mnt/external_usb_drive/sda.mirror bs=1 count=0 seek=500107862016
  3. To create extra loopback device run mknod -m640 /dev/loop_sda_mirror b 7 8 (MAJOR and MINOR numbers, that are 7 and 8 in the example, you shoud get accordingly to other loopdack devices (run ls -al /dev/loop* to see values for default ones))
  4. To attach file to loopback device run losetup /dev/loop_sda_mirror /mnt/external_usb_drive/sda.mirror

Then see another answer.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .