To create an image of a hard drive, it is not necessary to use utilities like Acronis True Image or Norton Ghost; a simple dd utility, which is included in most Unix-like computers, is sufficient. operating systems(Linux, FreeBSD, Solaris, etc.) The article describes a simple way to create backup copy image hard drive using dd. The first step is to prepare for backup. In this article we introduce the following notation:

  • /dev/sda - the disk whose image needs to be created;
  • /dev/sdb - the disk on which the image will be written.

If necessary, you need to substitute your own values.

Preparing to create a hard drive image

The first step is to boot from any available Live-CD disk that has the dd utility and enter command line as a superuser. Create a mount point to carry out backup.

mkdir /mnt/backup

We mount hard drive to which you want to save the image.

Creating a hard drive image

dd if=/dev/sda of=/mnt/backup/sda.img bs=8M conv=sync,noerror

  • if=/dev/sda - copy the entire hard drive sda;
  • of=/mnt/backup/sda.img - copy to /mnt/backup/sda.img;
  • bs=8M - set the size of the hard drive cache to speed up the copying procedure (otherwise the data will be reset in small portions of 512 bytes);
  • conv=sync,noerror - we indicate to dd the need for bit-for-bit copying and ignoring read errors.

To reduce the size of a hard disk image, you can compress it with any archiver.

dd if=/dev/sda bs=8M conv=sync,noerror | gzip -c > /mnt/backup/sda.img

Recovering a hard drive image

To restore a hard disk image, you must follow the reverse procedure to the procedure for creating this image.

dd if=/mnt/backup/sda.img of=/dev/sda bs=8M conv=sync,noerror

When using compression, you must unzip the image in parallel.

gunzip -c /mnt/backup/sda.img | dd of=/dev/sda conv=sync,noerror bs=8M

Migrating the system to another hard drive

To migrate the entire system to another hard drive, you must set the location of the new drive as the destination.

dd if=/dev/sda of=/dev/sdb bs=8M conv=sync,noerror

Then, if necessary, install the boot from this tough disk. Provided that new hard the disk is larger than the old one, there will be an unallocated area on it. It should be marked up and formatted according to existing requirements.

Copy statistics in dd

The main disadvantage of dd is the lack of a visual representation of the statistics of the copying procedure. However, this disadvantage can be easily circumvented. All you need to do is connect to another terminal.

Determine the process number under which dd is running.

Periodically send the command kill -USR1 process_number_dd to this process.

watch -n 5 kill -USR1 process_number_dd

  • watch -n 5 - execute the command every 5 seconds;
  • kill -USR1 process_number_dd - show copy statistics.

Quite often, system administrators need to copy various binary data. For example, sometimes you may need to make a backup copy of hard disk, create an empty file filled with zeros to organize swap space or another virtual file system.

To solve all these problems, the dd linux utility is used, which simply copies data from one place to another at the binary level. It can copy a CD/DVD disc, a section on a disc, or even an entire hard drive. In this article we will look at what the linux dd command is, its main options and parameters, and how to use it.

First you need to understand how the dd command works and what it does. In fact, this is an analogue of the utility only for block data. The utility simply transfers one block of data of the specified size from one place to another. Since everything, including devices, is considered a file in Linux, you can transfer devices to files and vice versa.

Using various utility options, you can influence the block size, and this, in turn, already affects the speed of the program. Next we will look at the main options of the utility and its capabilities.

dd command

The syntax of the utility is quite unusual, but at the same time very simple, once you remember and get used to it:

$dd if= copy_source of= destination parameters

Using the if parameter, you need to specify the source from which the blocks will be copied, this can be a device, for example, /dev/sda or a file - disk.img. Next, using the of parameter, you need to specify the destination device or file. Other parameters have the same syntax as if and of.

Now let's look at the additional options:

  • bs- indicates how many bytes to read and write at a time;
  • CBS- how many bytes need to be written at a time;
  • count- copy the specified number of blocks, the size of one block is indicated in the bs parameter;
  • conv- apply filters to the data stream;
  • ibs- read the specified number of bytes at a time;
  • obs- write the specified number of bytes at a time;
  • seek- skip the specified number of bytes at the beginning of the reading device;
  • skip- skip the specified number of bytes at the beginning of the output device;
  • status- indicates how detailed the conclusion should be;
  • iflag, oflag- allows you to set additional operation flags for the input and output device, the main ones: nocache, nofollow.

These were all the basic options you might need. Now let's move closer to practice and look at several examples of how to use the dd linux utility.

How to use dd?

Regular users use the dd command most often to create images DVDs or CD. For example, to save a disk image to a file, you can use the following command:

sudo dd if=/dev/sr0 of=~/CD.iso bs=2048 conv=noerror

The noerror filter allows you to disable response to errors. Next, you can create an image of the hard drive or partition on it and save this image to disk. Just be careful not to save to the same hard drive or partition, so as not to cause recursion:

dd if=/dev/sda of=~/disk.img

A file named disk1.img will be created in your home folder, which in the future can be deployed and restored to the damaged system. To write an image to a hard drive or partition, just swap the device addresses:

dd if=~/disk.img of=/dev/sda

A very important and useful option is bs. It allows you to greatly influence the speed of the utility. This parameter allows you to set the size of one block when transferring data. Here you need to specify a digital value with one of these format modifiers:

  • With- one character;
  • b- 512 bytes;
  • kB- 1000 bytes;
  • K- 1024 bytes;
  • M.B.- 1000 kilobytes;
  • M- 1024 kilobytes;
  • G.B.- 1000 megabytes;
  • G- 1024 megabytes.

The dd linux command uses just such a system, it is complex, but there is no escape from it. It will have to be understood and remembered. For example, 2b is 1 kilobyte, and 1k is also 1 kilobyte, 1M is 1 megabyte. By default, the utility uses a block size of 512 bytes. For example, to speed up disk copying, you can take blocks of 5 megabytes in size. To do this, use the following command:

dd if=/dev/sda of=~/disk.img bs=5M

The next parameter is count. Using it you can specify how many blocks need to be copied. For example, we can create a 512 megabyte file by filling it with zeros from /dev/zero or random numbers from /dev/random:

sudo dd if=/dev/zero of=file.img bs=1M count=512

Please note that this parameter does not indicate the size in megabytes, but only the number of blocks. Therefore, if you specify a block size of 1b, then you only need to take two blocks to create a 1KB file. This option can also be used to backup the MBR partition table. To do this, copy the first 512 bytes of the hard drive to a file:

sudo dd if=/dev/sda of=mbr.img bs=1b count=1

To restore, use the usual command to deploy the image to disk.

If the disk image is too large, you can redirect all output to the non-standard output stream of the gzip utility:

dd if =/dev/sda2 | bzip2 disk.img.bz2

You can also use the dd linux utility to copy files, although this is not its intended purpose:

dd if=/home/sergiy/test.txt of=/home/sergiy/test1.txt

As you know, the linux dd command writes data to disk directly in binary form, which means that zeros and ones are written. They override what was previously placed on the recording device. Therefore, to erase a disk, you can simply fill it with zeros from /dev/zero.

sudo dd if=/dev/zero of=/dev/sdb

Using dd this way results in the entire disk being completely erased.

Conclusions

In this article we looked at how to use dd linux, what this utility can be used for and how useful it can be. It's an almost indispensable tool. system administrator, because it can be used to make backup copies of an entire system. And now you know how. If you have any questions, ask in the comments!

Using the dd utility, we will create an image of a flash drive with archiving of free space. The backup image will come in handy if the original suddenly stops working. important information. For example, a flash drive with private keys of electronic signatures of the organization’s management. So, we have a 4GB flash drive /dev/sdd, the information on which occupies about 90MB.
du - sh / run / media / aleksey / Transcend

89M /run/media/aleksey/Transcend

All commands are executed on behalf of the user root. Or in the corresponding distributions add before the commands sudo.
fdisk - l /dev/sdd

Disk /dev/sdd: 3.7 GiB, 3904897024 bytes, 7626752 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0xc653eaa4 Device Boot Start End Sectors Size Id Type /dev/sdd1 2048 7628543 7626496 3.7G b W95 FAT32

Disk /dev/sdd: 3.7 GiB, 3904897024 bytes, 7626752 sectors

Units: sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disklabel type: dos

Disk identifier: 0xc653eaa4

Device Boot Start End Sectors Size Id Type

/dev/sdd1 2048 7628543 7626496 3.7G b W95 FAT32

By creating a simple image with the command
dd if = /dev/sdd of = sdd . iso bs = 4M conv = noerror,
we condemn ourselves to storing a 4GB file. What if the flash drive had a capacity of 64GB? And not alone? A regular archiver will help us solve this problem, let’s take a standard one gzip.
dd if=/dev/sdd bs=4M conv=noerror | gzip - c > sdd . iso. zip
where is the key -c allows you to work with standard output.
After completing the work, let's look at the resulting file. ls - al sdd*

The resulting file is approximately 25MB in size. Real savings on disk space even compared to a 4GB file!
To restore a flash drive from an image, use the reverse order of commands.
gunzip - c sdd . iso. zip | dd of = /dev/sdd conv = noerror bs = 4M

You can also archive images hard drives, where the volumes are an order of magnitude larger.

By the way! To make the process clearer, due to dd not having its own progress bar, I suggest using a small utility progress- Coreutils Progress Viewer. Installing it on Fedora is not difficult.
dnf install progress
For other distributions, the required repository can be found at https://pkgs.org/download/progress.
By installing and running the utility with the command watch progress in the second terminal (in the first we have an archiver and dd running) on ​​behalf of the same user, we will see something like this.

man progress will show you various useful keys of this utility.

As is known, “computer users are divided into those who make backups and those who will do them”. In this article we will look at various ways to backup the entire system and, accordingly, restore from a backup.

It’s worth noting right away that all operations should not be performed “live”, i.e. not on a running system, but from a liveCD or installed on a neighboring partition/flash drive/usb-hdd of the system. In cases where downtime of a few minutes is critical for the system, it is possible to copy the system from under itself, but you need to take into account some additional conditions, which are not yet discussed in this article

Further in the text, for actions performed as a superuser, the sudo command will be used, which is the standard for Ubuntu. On other systems it is possible to gain superuser privileges via su , some liveCD systems run in superuser mode by default

tar

One of the most popular ways to create a simple backup is to archive data using tar. The advantages of this method are the possibility of incremental backup (adding files to an existing archive, deleting or changing them), the ability to extract from the archive separate files, as well as the presence of tar in almost any Linux system.

Creating an archive

First, create mount points for the root partition and for the partition on which you are going to create a backup, for example like this

Mount both partitions. For greater reliability, you can mount the root partition in read-only mode to eliminate the possibility of accidental data changes

Sudo mount /dev/sdXY /mnt/root -o ro sudo mount /dev/sdXY /mnt/backup

(Instead of "sdXY" use your values ​​for the required partitions. You can determine them using sudo fdisk -l or sudo blkid)

If you use separate partitions for /boot, /usr, /home, etc. and want to include their contents in the backup, mount them in the appropriate folders

Sudo mount /dev/sdXY /mnt/root/usr -o ro sudo mount /dev/sdXY /mnt/root/home -o ro

If necessary, create a folder on the backup partition in which you want to place the archive, for example

Sudo mkdir -p /mnt/backup/ubuntu/root

Now you can start creating the archive. To create a gzip-compressed archive, run

Sudo tar -cvzpf -C /mnt/root /mnt/backup/ubuntu-sda1.tar.gz .

(The -p switch enables saving owners and permissions for files)

For bzip2 compression use

Sudo tar -cvjpf /mnt/backup/ubuntu-sda1.tar.bz2 /mnt/root

For lzma compression

Sudo tar --lzma -cvpf /mnt/backup/ubuntu-sda1.tar.lzma /mnt/root

Similarly for lzo compression - switch --lzop instead of --lzma

Different compression algorithms produce different archive sizes and also differ in performance

Once the process is complete, unmount all mounted partitions

Sudo umount /mnt/root(/boot,/var,/home,) /mnt/backup

Restoring from an archive

Create mount points for the root partition and the partition where your archive is stored

Sudo mkdir /mnt/(root,backup)

Mount the partition with the backup archive

Sudo mount /dev/sdXY /mnt/backup -o ro

Format the root partition to the same (or another) file system. If you use separate partitions for /usr, /boot, etc. and archived them, format them too

(if you are restoring the system to a new hard drive, partition it using fdisk/gparted and format the partitions)

Some file systems support setting the UUID when formatting. This makes it possible to create a file system with the same UUID as the old one, which will avoid the need to edit fstab.

For ext2/3/4, the UUID is set using the -U switch, and you can simplify the task even further with a command like

Sudo mkfs.ext4 -L "label" -U "$(sudo blkid -o value -s UUID /dev/sda1)" /dev/sda1

If you used archiving when creating the image file, first unpack it using the same archiver, for example

Bzip2 -dv /media/backup/sda5.dd.bz

Now you can mount the image

Sudo mount /media/backup/sda5.dd -o loop /mnt

(With the loop option, the mount program will automatically “pick up” the image file to a free loop device, and then mount the file system)

Now you can work with the contents of the image as with a regular file system, all your changes will be written to the image. When finished, mount the image as a regular file system

Sudo umount /mnt

dd - copy the entire hard drive

In this case, we will use dd again, only this time we will save everything contents of hard disk - with a partition table, the partitions themselves and all the data. Advantage this method the fact that you can save all systems installed on this hard drive in one step without having to backup each partition separately. In addition, with such a backup, all data related to the bootloader will be saved - thus, after restoring from the backup, you will not need additional manipulations, you can immediately boot from this hard drive.

Creating an image

In general, the procedure is similar to that described above for backing up individual partitions. In this case, the advice about clearing free space with “zeros” also applies - if you have free time, do this with all partitions.

Before starting the operation, make sure that none of the partitions on this hard drive are mounted. This can be done by running the mount command without parameters.

Select the partition on which you are going to place the clip file. Of course, this must be a partition from another hard drive. Also make sure that there is enough free space on this partition (for example, using the df utility) - the amount of free space should correspond to the volume of the copied hard drive (when compressed, the image will be smaller, but this depends on the type of data stored).

Mount a backup partition

Sudo mount /dev/sdXY /mnt

Now you can start

Sudo dd if=/dev/sdX bs=1M conv=noerror,sync | lzma -cv > /mnt/hdd.dd.lzma

(here “sdX” is a disk, not a partition! for copying without compression, the command is similar to the one above for backing up a partition)

Depending on the size of the hard drive and the performance of the computer, the procedure may take for a long time(up to several hours). When finished, mount the backup partition

Sudo umount /mnt

Recovery from image

Attention! This method involves a complete rollback to the state at the time the archive was created with the replacement of all data!

Before starting work, make sure the power supply is reliable. Connect network adapter, if you have a laptop, and if possible use a UPS or stabilizer. High write rates increase the risk of disk damage in the event of a power failure

Make sure that no partition of the disk being restored is in use. Mount a backup partition

Sudo mount /dev/sdXY /mnt

You can start the procedure

Bzip2 -dc /mnt/hdd.dd.bz | sudo dd of=/dev/sdX bs=1M conv=sync,noerror

Or for an uncompressed image

Sudo dd if=/mnt/hdd.dd.bz of=/dev/sdX bs=1M conv=sync,noerror

When finished, mount the backup partition

Sudo umount /mnt

If you want to extract the image to another hard drive, it must be at least as large as the original one. In case new disk larger volume, you can expand the partitions or create a new partition on free space using parted/fdisk/gparted/etc

Don't use both hard drives(“duplicate” and “original”) at the same time! If both drives are connected, the system will have two partitions for each UUID, which will lead to operational problems or inability to boot

Mounting the image

By analogy with the partition image, you can work with the hard disk image as with a regular hard drive. In this case, the procedure becomes somewhat more complicated, since the image contains several sections.

If the image is compressed, unpack it. Now “pick up” the image to the loop device

Sudo losetup -fv /media/backup/sda.dd

(With the -f switch, the program will automatically find a free loop device, otherwise you must explicitly specify it)

losetup will display the name of the device used - if you are not working with other image files (iso, encrypted containers, etc.), it will most likely be /dev/loop0

Now we have a device that is a hard drive for the system, but we do not have access to its partitions. The kpartx program will help you get to the partitions (you may need to install the package of the same name)

Sudo kpartx -av /dev/loop0

(Key -a - add partitions for a given device; -v - informative output)

The program will display the names of the created devices for the disk partitions: loop0p1 for the first partition, loop0p2 for the second, similar to the partitions of a regular disk. The device files will be located in the /dev/mapper folder

Now you can work with partitions and FS on them. For example, mount the former sda5 and write files to it

Sudo mount /dev/mapper/loop0p5 /mnt

When finished, unmount the partition

Sudo umount /mnt

Remove partition devices using kpartx

Sudo kpartx -dv /dev/loop0

and release the loop device

Sudo losetup -v -d /dev/loop0

All! The changes are recorded, and your image becomes a regular file again

cp

Here we will look at backup using the cp utility, i.e. using simple copying. Actually, this is not the most optimal method, and it is more suitable for copying the system to another hard drive / partition / computer, rather than for creating a backup copy.

On the other hand, this method has a number of advantages:

    Universality - you will find cp in any Linux system

    Low resource requirements (due to lack of compression and simplicity of the mechanism)

    Ease of further work with the backup copy (adding/changing/deleting files, extracting the necessary data, etc.)

Making a copy

Create mount points for the root and backup partitions

Sudo mkdir /mnt/(root,backup)

Mount both partitions

Sudo mount /dev/sdXY -o ro /mnt/root sudo mount /dev/sdXY /mnt/backup

Mount partitions for /usr, /boot, etc., if any

Sudo mount /dev/sdXY -o ro /mnt/root/home

Create a folder for your backup on the backup partition

Sudo mkdir /mnt/backup/ubuntu

We can start

Sudo cp -av /mnt/root/* /mnt/backup/ubuntu

(the -a switch enables copying links “as is”, saving all possible file attributes and recursive mode. -v switch - displaying information about what is happening)

Once the process is complete, unmount all partitions

In the future, you can archive your data in any convenient way.

Restoring from a copy

Attention! This method involves a complete rollback to the state at the time the archive was created, replacing all data!

Create mount points for partitions

Sudo mkdir /mnt/(root,backup)

Mount a backup partition

Sudo mount /dev/sdXY -o ro /mnt/backup

Format the root partition and /usr, /boot, etc. partitions, if any. (For formatting partitions while preserving the UUID, see the section about)

Sudo mkfs.reiserfs -l "root" /dev/sdXY sudo mkfs.ext2 -L "boot" /dev/sdXY sudo mkfs.ext4 -L "home" /dev/sdXY

Mount the newly created file systems

The copying process is similar, only in the opposite direction.

Sudo cp /mnt/backup/ubuntu/* -av /mnt/root

Once the copy is complete, edit fstab to correct the partition UUIDs

Unmount the partitions

Sudo umount /mnt/backup /mnt/root/(usr,home,)

squashfs

sudo mkfs.reiserfs -l "root" /dev/sdXY sudo mkfs.ext2 -L "boot" /dev/sdXY sudo mkfs.ext4 -L "home" /dev/sdXY

Mount the newly created file systems

Sudo mount /dev/sdXY /mnt/root sudo mount /dev/sdXY /mnt/root/usr sudo mount /dev/sdXY /mnt/root/var

We're ready to start! To unpack the image, use the unsquashfs utility

Sudo unsquashfs -d /mnt/root -f /mnt/backup/ubuntu-root.sqfs

(The -d switch specifies the path for unpacking, with the -f switch the program will use existing folders instead of trying to create new ones)

Just like when creating an image, you will see a progress bar and lots of other useful information.

When finished, edit fstab, replacing the partitions' UUIDs with new ones (if you formatted the partitions with the same UUIDs, skip this step)

Sudo nano /mnt/root/etc/fstab

Save the file and unmount all partitions

Sudo umount /mnt/backup /mnt/root(/usr,/var,)

Mounting the image

squashfs is mounted like any other image - via a loop device. Kernel support for squashfs is included in many distributions, including Ubuntu, so you just need to use the mount command with the loop option

Sudo mount /media/backup/ubuntu-root.sqfs -o ro,loop /mnt

(The ro option is not required, since writing nothing there will not work anyway)

Now you can copy any necessary files. Adding something this way will not work; to do this you will need to use mksquashfs again

When finished, mount the image as a regular file system

Sudo umount /mnt

rsync

Like cp, rsync works on files rather than block devices. The thing about rsync is that it doesn't copy files that are already at the destination. By default, it checks the size and modification time of files, but you can also check the hash (usually this is done when increased security is needed).

Easy to use

The rsync syntax is similar to cp:

Rsync -a /mnt/root /mnt/backup

The -a parameter is often sufficient; it provides what is most needed: recursive copying of directories, saving information about the owner and group, etc. To display detailed information about copying, use the -v switch; be careful with it; you may miss an error message in the data stream. The -x switch ensures that rsync does not go beyond the specified filesystem.

The rsync documentation describes a lot of options. For example, there are those that allow you to copy over SSH, or delete a file from the destination if it was deleted in the source directory.

Smart copying reduces system downtime. We run rsync directly on a running system, the data in which is constantly changing, rsync copies the data, say, within a few hours. Then we switch the system to read-only, run rsync again, now it copies only those files that have changed over these few hours. In a few minutes we have a complete copy of the original file system. Downtime was reduced by an order of magnitude compared to offline copying. And in some cases, one online copy will be enough without converting the system to read-only.

Saving previous copies

Strictly speaking, rsync is not a backup tool - it is a synchronization tool. This is important when creating regular copies, because if any important file was deleted in the source working directory - rsync will delete it in the backup copy as well. To improve data security, it is advisable to save old backup copies. However, simply saving multiple copies will require a lot of hard drive space. If copies have many identical files, then this leads to unnecessary redundancy. This problem can be solved by using hard links.

The point is that in modern file systems(including Ext4) file addressing is carried out in two stages: the file name indicates a unique file number (index descriptor or i-node), and the data itself is associated with this number. Any file name is, in fact, a hard link to this number. Consequently, a file (data set) can have several names and be in different directories, and this eliminates redundancy in case of need to duplicate files (after all, a hard link takes up little memory). The data itself is not deleted until the last hard link is requested to be deleted.

A significant limitation is that hard links are only possible within the same file system.

Synchronizing directory contents for the current backup with the source directory:

Rsync \ --archive \ --delete --delete-excluded \ # deleting files that do not exist in the source and excluded files from the backup--progress\ # display information about the progress of the transfer"/home/user/Files/" \ # directory source"/backup/latest/" \ # directory for current backup--exclude = "/Public/" # exclude unnecessary directories

In the “/backup/latest/” directory, a copy of all necessary files and directories from the source will be created and everything unnecessary will be removed.

Creating another current backup without redundancy:

cp\--archive\ # save all additional information about files--link\ # use hard links for files - eliminate redundancy"/backup/latest/" \ # source is the current backup obtained above "/backup/$(date +%Y-%m-%d_%H-%M-%S) /" # destination - directory with date in name for convenience (see man date)

The next time you create a backup, rsync will delete files in the “ /backup/latest/ ” directory that were deleted/excluded/changed in the source directory (changed files are first deleted and then written new version). However, only the file names (the same hard links) will be deleted; the files themselves (data) will be saved, since hard links were created to them in a neighboring directory with the “cp” command.

Other tools

There are many applications for creating backups in Linux. You can search for the word “backup” in the Center Ubuntu applications to find backup programs available in Ubuntu.

For a corporate environment and simply for fairly large-scale and critical backup tasks, we can recommend understanding one of the most popular and powerful backup systems for Linux, called Bacula

By the way, you can also find Russian-language manuals on the Internet.

Parted Magic

Parted Magic is another great one, but paid a distribution kit containing a whole collection of tools for backing up and restoring information, working with disks and partitions, as well as recovering lost data. It supports many file systems, LVM2 and RAID (both hardware and software) and contains tools such as fsarchiver, GParted, the aforementioned Clonezilla, and everything that is required for the methods described in this article. In addition, the distribution includes a web browser and some other additional software. The distribution is translated into several languages, including Russian, and has a full-fledged graphical interface.

LParted

LParted is a full-featured LiveCD designed primarily for working with hard drive partitions (HDDs), permanently deleting or restoring data, and testing hardware. LiveCD based on Lubuntu Linux. LParted is a functional analogue of Parted Magic.

I would like to add here about SystemRescueCD and others

A little more about saving data

    For important data, you can make a mirror partition on two disks. To do this, it is not at all necessary to have a RAID controller and disks of the same size - you can, for example, assemble a mirror from an 80 GB old drive and an 80 GB partition on a new one. Mirroring can be implemented using LVM or software RAID. However, this method is useless if, for example, a voltage of ~220V hits the +5V bus or a meteorite falls on system unit computer.

    IT geeks who have their own server at home can expand the idea of ​​mirroring and use DRBD. The same RAID-1, but hard drives are in different computers, which increases reliability.

    A modern, convenient solution is to back up data to the cloud, for example, using Ubuntu One, Dropbox, http://www.adrive.com/ and others.

    Neither mirroring nor replication on Ubuntu One will save you from accidental pressing Delete, so in any case, make “classic” backups. And one day, all your hard work and efforts will be rewarded.

The dd command does just one simple thing: it copies data from a file to another file. But since in Linux many entities are represented precisely as files, dd has many uses. Let's look at the most useful of them.

What does dd mean?

dd is short for data duplicator. But because in the wrong hands the dd command can lead to complete loss of all data, the program is often jokingly called disk destroyer. Let's try to figure out how not only not to destroy your data, but even to benefit from using dd.

General use case for dd

The command syntax is as follows:

Dd if=$input_data of=$output_data

The command will copy the data from the $input_data file to the $output_data file, taking into account the options. It would seem that everything is simple. Now let’s look at what opportunities this simple copying opens up.

Examples of using dd

1. Destruction of all data on the disk without the possibility of recovery:

Dd if=/dev/urandom of=/dev/sda bs=4k

2. Complete byte-by-byte copying of one disk to another (cloning):

Dd if=/dev/sda of=/dev/sdb bs=4096

3. Copying one partition to another:

Dd if=/dev/sda3 of=/dev/sdb3 bs=4096 conv=notrunc,noerror

4. Display a list of available file systems:

Dd if=/proc/filesystems | hexdump -C | less

5. Copying data on devices with different block sizes (1 KB at the source and 2 KB at the destination):

Dd if=/dev/st0 ibs=1024 obs=2048 of=/dev/st1

6. Create a bootable USB flash drive:

Dd if=/home/$user/bootimage.img of=/dev/sdc

7. Check the disk for bad sectors:

Dd if=/dev/sda of=/dev/null bs=1m

8. Create a backup copy of the MBR disk and save to a floppy disk

Dd if=/dev/sda of=/dev/fd0 bs=512 count=1

9. Removal ISO image from CD:

Dd if=/dev/sr0 of=/home/$user/mycdimage.iso bs=2048 conv=nosync

10. Checking the file for viruses (of course, ClamAV is required):

Dd if=/home/$user/suspicious.doc | clamscan -

11. Saving the contents of RAM to a file:

Dd if=/dev/mem of=/home/$user/mem.bin bs=1024

12. Converting an image from Nero NRG format to a standard ISO image:

Dd bs=1k if=imagefile.nrg of=imagefile.iso skip=300k

13. View MBR contents:

Dd if=/dev/sda count=1 | hexdump -C

Where are the promised million applications?

An observant reader will probably notice that the article lists more than a million useful applications, and somewhat less. But the power of the dd program lies in the fact that the user can find other applications independently, combining different files as parameters if, of and selecting the necessary options. Just remember that working with dd requires extra attention. If you do not know exactly what actions will be performed, then it is better to refrain from experiments. Try not to give dd superuser rights when you can do without these rights.

Your own examples of using this wonderful program are welcome in the comments.


Close