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.
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.Sign up to join this community
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.
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 http://www.my.host.net.nz/blog/2012/09/on-demand-raid-for-laptop-with-ssd-and-usb-disk/
Some hints related to the problem:
blockdev --getsize64 /dev/sda
dd if=/dev/zero of=/mnt/external_usb_drive/sda.mirror bs=1 count=0 seek=500107862016
mknod -m640 /dev/loop_sda_mirror b 7 8(
MINORnumbers, 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))
losetup /dev/loop_sda_mirror /mnt/external_usb_drive/sda.mirror
Then see another answer.