Simple clear advice in plain English

Backup and restore

Find out how to back up and restore your disk partitions in Linux

Disk errors
Sometimes a filesystem can become damaged or inconsistent, such as when there is a sudden power loss. These kinds of errors can usually be detected and fixed with standard filesystem tools, like e2fsck.

Most distributions perform routine scans to check for problems like these. But if there is a hardware error on the disk drive itself (a bad sector), this can go undetected for a long time, and means the filesystem on the affected partition can never be fixed in place.

If you want to recover this data, you might try to extract the raw partition data using dd, in the same way as you would clone the partition. This would be fine for a partition with filesystem errors alone, but it isn’t so simple with hardware errors. The dd command would read as far as it could, and then stop with an error when it could not read the bad sector on the drive. There is an alternative to dd for situations like this, called ddrescue. There are in fact two different tools, each with the same name, but here we are looking at the GNU version of ddrescue.

This tool is very similar to dd, and in the case of there being no hardware errors on the partition, is effectively the same. In the case of an error, ddrescue does not fall over and stop. Instead it tries its best to recover as much data from the partition as possible. It also creates a logfile of its progress, so if it is interrupted, it can be resumed from where it left off. This is especially useful for less predictable hardware errors than unreadable sectors, as ddrescue can be run multiple times on the same partition to try to recover the lost data; it will fill the gaps in the recovery image file as best as it can.

To install ddrescue in Ubuntu, you must select ‘gddrescue’ to specify this particular version:

# apt-get install gddrescue

The standard usage of ddrescue is mostly the same as dd, except there are no ‘if’ and ‘of’ options. To copy or recover all the data from /dev/sda1 and put it into the file/backup/img.bin:

# ddrescue -v /dev/sda1 /backup/img.bin /tmp/reclog

First comes the input file or device, then the output, and then the logfile. The logfile is optional, but it’s always a good idea to create it. We’re specifying the verbose (-v) option here to see additional information.

As above, ddrescue works as a drop-in substitute for dd, as without any errors on this partition a full copy is made. Ddrescue also has the advantage of potentially a small performance increase (due to the way it uses buffers).

Checking for errors
Although ddrescue is designed for copying and recovering data from drives, it will also draw your attention to any errors on the disk. Duplicating an entire disk to detect errors does not seem the most sensible way to go about this, and thankfully Linux’s standard /dev/null device means it is not necessary. Anything sent to /dev/null is ignored, and so just specify this as the output device. You can check an entire drive, such as /dev/sda, in this way:

# ddrescue -v /dev/sda /dev/null /tmp/reclog

In this example, ddrescue will read all of the data from the hard drive without copying any of it. If there are any bad sectors on the drive, they will show up clearly. In this case, as no data is being backed up or restored, you may run it on a hard drive which is in use, although wait until there is little or no disk activity.
I advise running a command like this on a brand new hard drive, as bad sectors often don’t show themselves until the drive begins to fill, especially with large drives. It is not necessary to run a check like this frequently; doing so would add unnecessary wear to the hard drive.

Article tags

Reader Comments

   

Add your comment

All fields must be completed. Your email address will not be displayed or used to send marketing messages.

All messages will be checked by moderators before appearing on the site.

See our Privacy Policy for more information.

Related articles

Fix-virtualbox-problems-in-ubuntu

Get VirtualBox working in Ubuntu Linux

Virtualbox is great if you need to get Windows apps working in Ubuntu but it may need help to get working

myunity-customiser-utility-for-ubuntu

Tame the Ubuntu 11.10 Unity interface with MyUnity

Customise how the Unity desktop looks and works

7zip-file-compression

Compress files with 7-zip

Extract files from and create your own compressed folders

Question & Answer

Q.Why are some of the keys on my keyboard doing strange...

> Read the answer

Q.Is my phone’s Bluetooth any use?

> Read the answer

Q.Can I switch boot drives so that I can work on older...

> Read the answer

Best deals on the web

img

Samsung RV520-A07

£359.98- Buy it now

img

Acer Aspire 5750G (LX.RXP02.019)

£399.99- Buy it now

img

Apple MacBook Pro (MD313B/A)

£904.37- Buy it now

Latest issue & subscription deals

Poll

Are you concerned about viruses that target mobile phones?

Jargon Buster

Computing terms explained in plain English

Restore point

A Windows backup of system files and settings.

Great shopping deals from Computeractive