≑ Menu

6 Examples to Backup Linux Using dd Command (Including Disk to Disk)

Data loss will be costly. At the very least, critical data loss will have a financial impact on companies of all sizes. In some cases, it can cost your job. I’ve seen cases where sysadmins learned this in the hard way.

There are several ways to backup a Linux system, including rsync and rsnapshot that we discussed a while back.

This article provides 6 practical examples on using dd command to backup the Linux system. dd is a powerful UNIX utility, which is used by the Linux kernel makefiles to make boot images. It can also be used to copy data. Only superuser can execute dd command.

Warning: While using dd command, if you are not careful, and if you don’t know what you are doing, you will lose your data!

Example 1. Backup Entire Harddisk

To backup an entire copy of a hard disk to another hard disk connected to the same system, execute the dd command as shown below. In this dd command example, the UNIX device name of the source hard disk is /dev/hda, and device name of the target hard disk is /dev/hdb.

# dd if=/dev/sda of=/dev/sdb
  • “if” represents inputfile, and “of” represents output file. So the exact copy of /dev/sda will be available in /dev/sdb.
  • If there are any errors, the above command will fail. If you give the parameter “conv=noerror” then it will continue to copy if there are read errors.
  • Input file and output file should be mentioned very carefully, if you mention source device in the target and vice versa, you might loss all your data.

In the copy of hard drive to hard drive using dd command given below, sync option allows you to copy everything using synchronized I/O.

# dd if=/dev/sda of=/dev/sdb conv=noerror,sync

Example 2. Create an Image of a Hard Disk

Instead of taking a backup of the hard disk, you can create an image file of the hard disk and save it in other storage devices.There are many advantages to backing up your data to a disk image, one being the ease of use. This method is typically faster than other types of backups, enabling you to quickly restore data following an unexpected catastrophe.

# dd if=/dev/hda of=~/hdadisk.img

The above creates the image of a harddisk /dev/hda. Refer our earlier article How to view initrd.image for more details.

Example 3. Restore using Hard Disk Image

To restore a hard disk with the image file of an another hard disk, use the following dd command example.

# dd if=hdadisk.img of=/dev/hdb

The image file hdadisk.img file, is the image of a /dev/hda, so the above command will restore the image of /dev/hda to /dev/hdb.

Example 4. Creating a Floppy Image

Using dd command, you can create a copy of the floppy image very quickly. In input file, give the floppy device location, and in the output file, give the name of your floppy image file as shown below.

# dd if=/dev/fd0 of=myfloppy.img

Example 5. Backup a Partition

You can use the device name of a partition in the input file, and in the output either you can specify your target path or image file as shown in the dd command example below.

# dd if=/dev/hda1 of=~/partition1.img

Example 6. CDROM Backup

dd command allows you to create an iso file from a source file. So we can insert the CD and enter dd command to create an iso file of a CD content.

# dd if=/dev/cdrom of=tgsservice.iso bs=2048

dd command reads one block of input and process it and writes it into an output file. You can specify the block size for input and output file. In the above dd command example, the parameter “bs” specifies the block size for the both the input and output file. So dd uses 2048bytes as a block size in the above command.

Note: If CD is auto mounted, before creating an iso image using dd command, its always good if you unmount the CD device to avoid any unnecessary access to the CD ROM.

Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • pupu October 11, 2010, 4:31 am

    I recommend using ‘dd’ on unmounted/readonly devices only. You never know what changes in the middle of this kind of backup. Dd doesn’t lock your filesystem. It will read and write blindly and if you don’t know what you are doing, well…

  • rene October 11, 2010, 6:49 am

    Will this work with a hard drive with both Windows and
    Linux on it?

  • Jorge Cedi October 11, 2010, 11:53 am

    You can backup a usb key with dd

  • ciastek October 12, 2010, 5:46 pm

    Thanks for nice examples.
    When and why should i use synchronized I/O?
    How should i find optimal block size?

  • Bernardo October 13, 2010, 12:18 pm

    Nice examples,
    I would like to share one of many hacks to know the “status” of dd copy progress.
    Send the dd command to background
    i.e.
    # dd=if=/dev/sda of=backup.img &
    [1] 26431 <——– Process ID
    Check the process ID and run the next command:
    # kill -SIGUSR1 26431
    This will shows the copied bytes and the copy rate.

  • Matias October 14, 2010, 7:05 am

    Hello!
    It’s a really useful post, but I think it should be noted that dd is used just to copy files, and not to make backups, because a backup tool has a lot more functionality, like different backup levels, automation of backups and some kind of database storing related data (files saved, operator who run the backup, etc).
    Bye!

  • Paul A. October 20, 2010, 2:54 pm

    This is not very much related to dd, but I thought you’s like to know that your example 5 has a bash-ism that I’m not entirely sure is a bug or a feature. Most other shells won’t expand the ~ to $HOME like bash does for that particular command. Zsh has an option MAGIC_EQUAL_SUBST to let you choose either way.

  • iri October 26, 2010, 12:34 am

    Regarding example 5.
    what if ~ is on /dev/hda1 πŸ™‚

  • Cosmos November 17, 2010, 10:35 am

    Another useful tip:

    In this case for copying the Master Boot Sector:

    dd if=/dev/hda of=disk.mbr count=1 bs=512

    It copies 1 chunk of 512 bytes which is the amount of info that the MBR of the disk takes.

    and for restoring the MBR:
    dd if=disk.mbr of=/dev/hda

    It overwrites the first 512 bytes of your /dev/hda drive.

    Just another command that I use to do afterwards for saving the partitions:

    sfdisk -d /dev/hda > disk.sf
    It dumps the partitions of the /dev/hda disk to a text file, which you’ll be able to recover easily.
    sfdisk /dev/hda < disk.sf

    Bye

  • Sathish Kumar November 28, 2010, 1:58 am

    Thanks a lot…..

  • Anonymous April 9, 2011, 4:15 pm

    mistake with the device names here:

    In this dd command example, the UNIX device name of the source hard disk is /dev/hda, and device name of the target hard disk is /dev/hdb.

    # dd if=/dev/sda of=/dev/sdb

  • Anonymous June 23, 2011, 12:55 am

    Be VERY VERY careful about the source and destinations, if= and of=

    If you make a mistake and put the wrong source/destination as if/of, you WILL lose your data and recovery becomes very annoying. Possible, but annoying, such as losing all your filenames and filesizes.

  • fhefh November 19, 2011, 9:35 am

    delete MBR

    dd if=/dev/zero of=/dev/sda bs=512 count=1

  • Antonio Nogueira February 27, 2012, 9:21 am

    Despite that is a comment about to use dd command to backup (and restore) dual boot partitions with Windows and Linux inside (I didn’t see any repply about it), may I use dd command to backup a HD with one Partition only containning a windows system 64bit and after to create two partitions, restore the windows operational system on the first partition of the HD?

  • Edna August 29, 2012, 3:41 am

    Hello,

    I have a problem and I hope you can help me. I have to add two hard disks on a linux server. The first is the primary disk and the second is the backup disk. How to make the second hard drive to work when the first hard drive stop to work or break down? i fond a solution for windows but I need one for linux.
    Thank you!!!

  • Wil Barath September 29, 2012, 10:55 pm

    You have an error in your ‘dd’ example:

    For syncronized IO and no halt on errors:

    $ dd if=/dev/sda of=/dev/sdb iflag=noerror oflag=sync

    conv=sync merely pads the output to ibs size

  • Ananth October 1, 2012, 7:37 am

    Hello,

    Nice examples. I just want to know if i can take the complete backup (Disk image). when the system is online. eg: i have 1 hdd running centos 5.8 and will insert an external HDD of same size and take the backup when the system is in production.

    2. if my HDD is 250 GB and used is 150 GB will the backup size is 250 GB or 150 GB.? is it same for partition backup and HDD backup…?

  • Techie November 9, 2012, 2:21 am

    Hi,
    I tried dd if=/dev/sda of=/dev/sdb and its keep on running from last one hour and my used space of hdd is only 8 gb. Do i need to stop it manually.

    Thx

  • baliram singh December 14, 2012, 6:22 am

    Hello, this is very important information about back

  • sivaprakash April 9, 2013, 3:18 am

    wow….now only i get more hope to study unix…. in high level thank you so much…

  • lemon April 27, 2013, 3:35 pm

    What about data integrity when dd on running machine ?

  • Colin Weaver June 23, 2013, 11:01 pm

    To backup my Linux partitions, I combine dd and gzip, e.g. to back up my Ubuntu root partition which is on /dev/sda5:

    dd if=/dev/sda5 bs=4096 | gzip -c > sda5-root.img.gz

    Performance of the compression can be improved by creating & deleting a file from /dev/zero before doing the backup, e.g.

    dd if=/dev/zero of=zero.bin bs=4096

  • b.gao October 16, 2013, 2:31 am

    In the example (1), the command :
    # dd if=/dev/sda of=/dev/sdb conv=noerror,sync
    does not do synchronized I/O, to do this the option iflag=sync or oflag=sync should be used.

  • geek123 December 7, 2013, 5:40 am

    while making a general ISO of a 3-4 GB dvd disk. how much ‘bs’ to use. Please describe the use of it.

    Thanks

  • rashid January 4, 2014, 12:48 am

    thanks nice article which help great to backup disk and then restore that data. again thanks

  • pam--virdi March 10, 2014, 3:02 pm

    I want to copy segy from tape to disk using DD commands
    I have multiple files on input and I want same names on output
    can some one advise me please
    thanks

  • Pugazhendhi_r April 6, 2015, 3:51 am

    How to copy multiple img files to single img file. For example, I have 4 virtual hard disks (a.img, b.img, c.img, d.img), if I need to copy the contents of a.img, b.img, c.img to single disk d.img. is it possible using dd, if so , please let me know

  • niy August 20, 2015, 1:08 am

    hi Ramesh
    i have 5 files iso (from oracle linux 6.5 ) that i have to put or ( burn ) into one usb flash drive .
    which way is correct.
    #dd if = file1.iso,file2.iso,file3.iso,file4.iso,file5.iso of = /dev/sdb
    or one by one ..
    or ..? not possible
    thanks a lots

  • Amit January 1, 2016, 6:16 am

    Great Sir, it is really help information.
    Thank You

  • Rahul January 4, 2016, 7:31 am

    can i store the Image of HDD in NAS? Can i restore the same image on the other server which was having different specifications ?

  • joe January 7, 2016, 6:45 pm

    To copy multiple img file into single file, i’d rather use tar .

    tar -cf outfile.tar a.img b.img c.img d.img

    or, to include compression as well:

    tar -cjf outfile.tar.gz a.img b.img c.img d.img

  • Antihero Guy January 23, 2016, 1:35 am

    Joe I think the “j” flag in tar cjf means compress as bzip2, just a heads up, anyone wanting to use gzip should use the “z” flag instead tar czf

  • choudhry March 1, 2016, 11:34 am

    you are the best

  • Ravi March 12, 2016, 9:04 pm

    excellent site to learn unix commands

  • Rahul May 9, 2016, 7:50 am

    I wanted to save the IMG file directly on the NAS is that possible?