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.
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:
If they are not equal size you can do the same with a partition:
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
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:
Then see another answer.