How to make a disk image and restore from it later?

  • I'm a new Linux user. I've reinstalled my Wubi from scratch at least ten times the last few weeks because while getting the system up and running (drivers, resolution, etc.) I've broken something (X, grub, unknowns) and I can't get it back to work. Especially for a newbie like me, it's easier (and much faster) to just reinstall the whole shebang than try to troubleshoot several layers of failed "fixing" attempts.

    Coming from Windows, I expect that there is some "disk image" utility that I can run to make a snapshot of my Linux install (and of the boot partition!!) before I meddle with stuff. Then, after I've foobar'ed my machine, I would somehow restore my machine back to that working snapshot.

    What's the Linux equivalent of Windows disk imagers like Acronis True Image or Norton Ghost?


    Note: I found a similar question: Easy backup/restore of installed system?

    Torben, under the Wubi folder (usually C:\UBUNTU, but may be some other drive & folder), you'll find not only the loopback disk images created for your Ubuntu install, but also some other important files (like the Wubi loader). Sorry if it's a guess, but I think it's an educated one. ;-) Maybe ('cause I really have never tried - sorry - but wouldn't hurt if you try it - I would) if you backup that folder while the system is in a good state, you can take Wubi back by replacing the good state over the non-functional one.

    Please post this as an answer, because it sounds like a good way, specifically for Wubi installations! After my last fubar I installed a "real" Ubuntu, not Wubi, so this little trick will go into my drawer for next time I find myself in that spot.

    You can just copy the files and the master boot record (http://www.freesoftwaremagazine.com/articles/backing_up_your_master_boot_record) and you'll be fine.

    What do you mean? Can you please explan. I need thoes packages and files also.

    Acronis True Image can capture an accurate backup while the system is running. All of the answers below except the one that mentions Ghost for linux fail because they don't actually replace the functionality of Acronis and Ghost. There is a common belief that backups can be made on the system on which they are running. Although this is true, the backups created stand the risk of not restoring properly due to files being changed in the file-system as the backup is being made, making the backup inconsistent with itself.

  • Correct answer

    10 years ago

    It's Clonezilla Live: http://clonezilla.org/

    The tutorial for Clonezilla can be found here.

    +1 Safer way of doing it. `dd` is little dangerous to use.

    Clonezilla will not image a running file-system like Acronis or Ghost, so Clonezilla does not do what they do. In order to use Clonezilla, the OS must be stopped, and a 2nd OS must mount it's file-system and take a snapshot. If you don't stop the OS the restore has the chance of not working because in a multi-tasking OS other tasks can put the backup in an unnatural state.

    Well, yes, Clonezilla should be used from a boot CD to have exclusive (like any full-drive-imaging tool. (Windows became a bit of an exeception though its Volume Shadow Copy Service "hack").

    "Boot CD" usually means "Boot USB Stick" nowadays. And a great way to create one is the Linux Live USB Creator. It even has "Clonezilla Live CD" is even under it's predefined images to choose from... Click, Build, ready-to-boot. Only caveat: Will only run on your (friends) Windows machine...

  • dd is the low level utility that you can use to accomplish this task, it essentially a low level byte-for-byte copy utility. If you want the "UNIX" way of accomplishing this, then read on.

    All references to the file system and hard disks are located locally on the virtual /dev/ filesystem. There are a multitude of "nodes" in /dev/ that are interfaces to almost all the devices on your computer. For example, /dev/hda or /dev/sda would refer to the first hard drive in your system (hda vs sda depends on the hard drive), and /dev/hda1 would refer to the first partition on your hardrive.

    The most straight forward way to make a raw image of your partitions is to use dd to dump the entire partition to a single file (remember the OS access the partitions /dev/sda1 through a file interface). Make sure you are on a larger partition or on a secondary drive and perform the following command
    dd if=/dev/hda1 of=./part1.image to backup(repeat for different partitions)
    dd if=./part1.image of=/dev/hda1 to restore

    You can use the exact same command to backup the entire hard disk (replace hda1 with hda). You can then use any compression program (gunzip, zip, bzip) to compress the file for storage. You can use this same technique to make rote copies of entire partitions to make clones of your computer.

    There is one limitation though, when restoring the backup, the partition needs to be the same size (or bigger) as the partition you took the image from, so this limits your options in case of a restore. However, you can always expand the partition after you've restored the backup using gparted or parted. The picture gets even muddier when you are trying to restore entire disk copies, however, if you are restoring the backup to the same exact hardrive you don't need to worry about this at all.

    However, if you want a "friendlier" utility ala norton ghost then this suggestion might not be for you.

    I almost expected there to be a built-in tool for this, but I also expected that tool to be ... not-so-noob-friendly :-) This is probably a good solution, once I get more Linux experience.

    Also check out ddrescue. Maybe with SystemRescueCd.

    Will `dd` work if I need to take a snapshot of a virtual machine and install it on another virtual machine? What if some files (like `/etc/hostname`) have to be different on both machines?

    @Thomas for virual machine images you better use corresponding VM Manager built-in functionality e.g. _Export image_ in Virtual Box.

    Does the restoring hdd has to be of same size? What if I make image of a 80gb disk and dump it on a 100gb one?

    Isn't "conv=fsync" required for the dd invocations?

    Why can't I pipe to tar? `dd if=/dev/sda2 | tar -zcvf sda2.image.tar.gz` `tar: Cowardly refusing to create an empty archive`

    Piping would require your dd command to be sending its output to standard out. It must not be doing that in the form you are using.

    @Bibhas: only 80GB of the 100GB disk will be used; but you can expand the partition afterwards in the ordinary way.

    Ohh Awesome **dd**! never thought would take so long to enjoy your pleasure! I bow in front of your power!! ;-)

    Yes, indeed. `dd` is safer than CloneZilla. Just add a second hard drive large enough to hold your boot drive, then boot a LiveCD (in "Try Ubuntu") mode. I use 16.04 - then issue a command like `sudo dd if=/dev/sda of=/dev/sdb bs=64k` command in terminal. It takes a while.

    I usually do a `fdisk -l` first to make sure that I have my device names right. Then a `dd if=/dev/urandom of=/dev/sdb count=24` to wipe the partition table. Then a second `fdisk -l` to verify it. Then I run the `dd` to do the image copy.

    how can we restore this `.image` file?

    can I do back up a system while it's running with this?

  • We have a GUI available in Ubuntu, called 'Disks' (gnome-disks) and comes default in later Ubuntu.

    Or

    sudo apt-get install gnome-disk-utility
    

    Launch/search as "Disks", or run as gnome-disks.

    Also needed are:

    • Gparted

    • A live Ubuntu (or derivatives) live-session usb-stick.


    Prepare the partition:

    Obviously, a partition cannot be copied while in use: it needs to be unmounted so it doesn't suffer changes during copying (that is self-evident). Therefore, in order to back up your system partition you have to boot in a usb live system - or, as seen in the above image, in a separate (multi-boot) system. The system partition is the one marked with a star. If you try to copy the system partition you get an error soon enough:

    enter image description here

    Also, one has to take notice that the back-up and the restoration of bootable partitions are both risky procedures and that a back-up of valuable/sensitive data should be first done in a different way (by copying the data instead of the entire partition, etc). - From my experience, this works with Ubuntu system partitions but not with Windows ones.

    Optionally, in order to minimize the space taken by the saved image, a partition can be first shrunk (from end, that is from right) so that it would not include the empty space. Here is a post on that: create partition backup image no larger than its files.

    Save the partition:

    enter image description here

    Restore the partition:

    Following this answer: How to correctly restore system partition (iso image) with 'Disks'. See the question there. The main idea is that the image (iso) has to be restored onto an existing empty partition that is:

    • formatted

    • un-mounted

    • larger than the saved image

    Prepare the restored partition for use:

    See the comments under the above linked answer on the fact that Gparted reports the difference of space between the destination partition and the restored one as un-allocated space. That can be fixed with Gparted: select partition and apply "Check" action.

    If the restored partition is a system partition that you want to use as the main one:

    If the live CD is x86 (because the CPU is x86), then creating image fails quickly at around 2GB, where 32-bit signed numbers underflow. I'm trying `dd` now to see if it has the same bug.

    Doesn't work if the disk is being used - you can't actually backup system partition :( and it doesn't even warn you until you choose the option, enter your password :(

    @NoBugs - That is expected behavior. Please see updated answer.

    All true - simply boot the system from an install CD (in "Try Ubuntu" mode) and run it while the partitions or drives are not in use.

    "Obviously, a partition cannot be copied while in use: it needs to be unmounted so it doesn't suffer changes during copying (that is self-evident)" Why Windows can do?

    @cdlvcdlv - The idea is that a system cannot save the partition *on which it is operating* . I am not familiar with the Windows tool for this purpose, but doesn't it ask to reboot during the procedure?

    @cipricus No. You can save several snapshots (begin and after a WU patch, for example) with the system working through all the process and sessions logged. I believe there are utilities for linux this kind too.

    @cdlvcdlv - Then you are answering your own question: you can do snapshots in Windows as well as in Linux (with Timeshift, also here). But as the concept of snapshots is different at least to some extent from that of the saved partition, you still cannot save the system *partition* from that very system.

    @cdlvcdlv - All this seems very interesting to me now, as I have never used Timeshift, so maybe I will try it and also post a question asking for details on main differences between a snapshot and a partition image.

    @cipricus Thanks for the clarification. Indeed, the question is about disk images, not snapshots. But IIRC Windows Backup utility can save the state of all partitions of a disk being online. Later, you can boot from a Windows RE and restore that saved state (which will erase all partitions, of course).

    Windows uses the *Volume Shadow Copy Service*, which creates an image of the disk at one instant in time, **while the disk is in use**. Any changes to the physical disk, after the shadow copy is made, are ignored. This is the one shortcoming of Linux that makes me create images using Windows tools. See https://en.wikipedia.org/wiki/Shadow_Copy

    Is it also possible to create an image of the whole drive instead of only a partition with this utility?

  • The "dump" and "restore" backup utilities in the Ubuntu repositories provide you with the ability to backup and recover an entire system's "system state".

    The "dump" and "restore" utilities can be installed from the repositories using:

    sudo apt-get install dump
    

    Below are links to the Man pages:
    Dump
    Restore

    In your case, you could backup the system to a portable drive:

    dump -0uan -f my_file /
    

    For recovery you would need to:

    1. Boot from your Ubuntu live CD/DVD/USB.

    2. Install the dump utility.

    3. Mount your hard drive.

    4. Restore (i.e. restore -r -f my_file /my_mount).

    Note: If you are restoring to a new disk you will need to install your bootloader (GRUB).

    More information can be found at Move linux to another hard drive (dump, restore, backup)

    How to restore from this if i fail to login to my ubuntu?

    here is the process for recovering, or moving, a system using the dump and restore commands as above. http://www.linuxscrew.com/2007/08/13/move-linux-to-another-hard-drive-dump-restore-backup/

    @virtual.stack - the answers would sound much better if you may provide a little how to on taking and restoring backups , using this method :)

    @tijybba - Sorry, I have updated the response. Please let me know if this is now clearer. Regards.

    While I am a fan of dump, it is not an image based backup.

    Shouldn't we exclude some folders while doing this, for example I have another HDD connected and its under /media/... if I just execute that command it will back it up as well while what I want to achieve is OS save restore?

  • The following are steps from the Ubuntu Wiki

    Backup with dd
    The following example will create a drive image of /dev/sda, the image will be backed up to an external drive, and compressed. For example, one may use bzip2 for maximum compression:

    sudo dd if=/dev/sda | bzip2 > /media/usb/image.bz2
    

    Restoring a drive image
    To restore a drive image, one will want to boot into a live environment. Restoration is quite simple, and really just involves reversing the if and of values. This will tell dd to overwrite the drive with the data that is stored in the file. Ensure the image file isn't stored on the drive you're restoring to. If you do this, eventually during the operation dd will overwrite the image file, corrupting it and your drive.

    To restore the drive above:

    bzcat /media/usb/image.bz2 | dd of=/dev/sda
    

    When restoring the whole drive, the system will not automatically create the devices (/dev/sda1, /dev/sda2, etc.). Reboot to ensure automatic detection.

    If you restored Ubuntu to a new drive, and the UUIDs (see UsingUUID for more) changed, then you must change the bootloader and the mount points. One will want to edit the following via a terminal:

    sudo nano /boot/grub/menu.lst
    sudo nano /etc/fstab 
    

    To know what the new UUIDs for your drives are, use the following command:

    sudo blkid 
    

    From this list, you can cross-reference the information with that of fdisk to know which drive is which. Then simply update the UUIDs in both GRUB and fstab files.

  • I would recommend RemasterSys for this Task.enter image description here

    It creates iso for your installtion, Backup user Date with Deja Dup.

  • There also Ghost for Linux, but Clonezilla is the better option - it's more up to date. The latest version of Ghost for Linux is listed May 2009, while Clonezilla's latest version was posted in November 2010. One minor limitation of Clonezilla compared to Ghost is that you can't resize the partition on restore, while Norton Ghost can.

    You probably won't need it, but Clonezilla also supports multicasting.

    Beware, Clonezilla won't take a correct snapshot of a running file-system like Ghost will.

  • You can do exactly what you need using CloneZilla

    Does it clone, my 320gb Hardrive on my 8gb pendrive?

    It should. Since you said that your total installation size is only 500mb.

    CloneZilla only works if the OS is stopped. Acronis and Ghost work while the OS is running.

  • Using "Dump" and "Restore" is one solution, as indicated by virtual.stack.

    However you might be interested in using Clonezilla if you have an external USB hard disk drive or a NAS. You just have to download an ISO image by clicking here (you can access the global download page here), burn it with "Brasero". Boot from Clonezilla Live CD and perform a backup (disk or partition to image) of your main hard disk drive (with your healthy Ubuntu). Please note that you can't backup the partition you have mounted as backup destination (quite logical). If your system is broken, you just have to boot again with Clonezilla Live CD and perform a restore of your system. Don't forget that Clonezilla makes snapshots, so if you have your data ("/home", "/etc", ...) on the same disk/partition as Ubuntu system, you'll get back the one from the backup and loose what has been done since that backup was performed...

    You can find a tutorial for Clonezilla Live here : How to Use Clonezilla - Tutorial

    You can also use "Back In Time (backintime-gnome)" (available from Ubuntu Software Center) or else (Déjà Dup, ...) alongside to get a backup of your data. You just have to include ("/home", "/etc", "/var", "/usr/local", ...) in the backup profile. Like that you can get back your healthy system with Clonezilla and then your latest data with "Back In Time" or else.

    Dump will not correctly image a running file-system like Acronis or Ghost, so Dump and restore do not do what Acronic or Ghost. In order to use Dump, the OS must be stopped, and a 2nd OS must mount it's file-system and a snapshot. If you don't stop the OS the restore has the chance of not working because in a multi-tasking OS other tasks can put the backup in an unnatural state.

  • If you're using Wubi, why don't you just boot into Windows and copy the root.disk file?

    Then restoring is as easy as renaming root.disk to something else, and renaming the copy to root.disk.

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM