Create disk image on linux

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 (freesoftwaremagazine.com/articles/…) and you’ll be fine.

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.

12 Answers 12

With dd

dd is the low level utility that you can use to accomplish this task. It’s essentially a low level byte-for-byte copy utility. If you want the «UNIX» way of accomplishing this, then read on.

Читайте также:  Linux local host file

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 hard drive.

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. When you backup /dev/hda1 this partition should be unmounted (or mounted read-only) to avoid potencial corruption.

You can use the exact same command to back up 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 hard drive, you don’t need to worry about this at all.

However, if you want a «friendlier» utility à la Norton Ghost then this suggestion might not be for you.

Источник

Introduction

This article is dedicated to documenting methods of performing drive imaging (also called bare metal backups, or disk cloning). Drive imaging is a complete copy of all information on a drive, necessary to restore all of the data or entire operating system on a drive to the same state it was when the image was created. This is different from imaging a partition, where one is making a copy of an individual partition that resides on a drive, or backing up individual files and folders.

Please ensure you are comfortable with the information discussed before proceeding. Improperly executing a command may result in partial or complete data loss. Please double- or even triple-check your target device to avoid such catastrophic loss.

Preparations

  • Exclusive access to the drive being imaged (i.e. the drive being imaged shouldn’t be mounted). Live operating system imaging methods, for example, physical-to-virtual (P2V), virtual-to-virtual (V2V), etc. are not covered here.
  • The location (remote file share, external USB drive, internal drive, etc.) where the drive image is being backed up to should have the same or more free space then that of the drive being imaging. For example, if you are imaging a 160GB drive, you should have 160GB or more free space to back up to.
  • The filesystem of the backup location needs to support the filesize necessary to backup the image as one file.
  • An environment to perform the drive imaging. This can be a live environment where one images the data of the computers internal drive.
Читайте также:  Cat linux команда создать файл

dd is a universal command line program used for low level copying of data. It will copy the entire drive, even if the used data is only consuming 10% of the beginning of the drive. For example, dd’ing a 100GB drive, where all the data is at the beginning, and is only 10GB is being consumed, the resulting file will be 100GB in size.

In order to find out which drive to clone, open a terminal and execute:

The output of the command will list each hard drive (ex. /dev/sda).

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

However, one may use this same concept to change the compression type (gzip, zip, etc.) to one that best suites your needs (higher compression speed, preferred compression format, etc.).

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:

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.

Clone Drive

Clone the contents of a whole hard drive onto another completely different drive

Clonezilla

https://launchpad.net/ubuntu/+source/clonezilla is another modern imaging solution. For more on it, please see the Clonezilla homepage.

Читайте также:  Linux unzip multiple files

DriveImaging (последним исправлял пользователь qiii 2015-09-28 17:55:23)

The material on this wiki is available under a free license, see Copyright / License for details
You can contribute to this wiki, see Wiki Guide for details

Источник

How to make disk image with dd on Linux

Recently I had to send my notebook to service (by the way — it’s fifth time when it’s in service). When I sent my notebook first time, I made a full backup because service after repair usually delete all data on disk and install clean Windows to check if everything is OK. But when I sent my notebook third time I said ENOUGH! Every few months I had to install Linux Mint, configure it, install my apps and so on, and so on. Then I started to create images. So how to make disk image with dd on Linux and what is the disk image?

What is disk image?

Disk image is a one big file which contains every byte of some disk. Notice that it’s not a copy if files. DIsk image is created by special applications from low level disk reading. It means these applications read disk sectors byte by byte and write them to another disk creating one big file. It’s very useful when you want to change disk to another for instance HDD to SSD.

How to create whole disk image?

dd if=/dev/ORIGIN_DISK of=/dev/PLACE-TO-WRITE bs=64K conv=noerror,sync 
dd if=/dev/sda of=/dev/sdb bs=64K conv=noerror,sync 

How to create a partition image?

dd if=/dev/sda1 of=/dev/sdb1 bs=64K conv=noerror,sync 

Let’s stop here for a moment and see what we have here: if — input device/file of — output device/file bs=64K — The block size. Usually peope use 64K or 128K, but you can use other values if know what you do conv=noerror — dd will continue operation and ignore all read errors sync — add input blocks with zeros if there were any read errors (it means data offsets stay in sync)

How to create disk image using live CD or USB flash drive?

fdisk

You should see similar to this:
Sometimes you can see different names of disk, eg. nvme for SSD M.2. To create disk image run this:

dd if=/dev/sda conv=sync,noerror bs=128K | gzip -c > SSD_image.gz 

DD will create compressed image of whole sda disk. The image will be in current directory. If you want to save it in another directory, tell dd where to save the image:

dd if=/dev/sda conv=sync,noerror bs=128K | gzip -c > /media/disk2/images/SSD_image.gz 

Above commands clone the entire hard disk, including the MBR, bootloader, all partitions, UUIDs, and data.

How to restore disk image?

gunzip -c SSD_image.gz | dd of=/dev/sda 

Bonus tip

dd if=/dev/sda conv=sync,noerror bs=128K status=progress | gzip -c > /media/disk2/images/SSD_image.gz 

Chris Texe Twitter

It would be great if you will comment or follow me on social media: Also you can visit my websites:

  • Linux blog
  • Web Agency website

Источник

Оцените статью
Adblock
detector