backup & restore hard disk in linux

How to Backup & Restore Hard Disk in Linux

Occasionally, system administrators may need to take backup of hard disk and restore it in Linux. In this article, we will learn how to backup & restore hard disk in Linux. It is important to remember that the target disk where you will copy your hard disk needs to be of same size or larger than the source hard disk. We will look at how to copy disk to another disk, as well as how to copy it to a disk image.

How to Backup & Restore Hard Disk in Linux

Here are the steps to backup & restore hard disk. We will be using dd command for this purpose. It is useful for taking disk backups, especially the ones where the OS is installed on.

1. Backup Hard disk to Another disk

Let us say you have a disk /dev/sda and you want to take its backup on /dev/sdb, then here is the command to do so.

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

You will see the following kind of output indicating disk copy in progress.

23165424+0 records in
23165424+0 records out
12784901588 bytes (13 GB) copied, 413.846 s, 27.4 MB/s

In the above command,

  • if: source disk drive (/dev/sda)
  • of: destination disk drive (/dev/sdb)
  • bs: read and write BYTES at a time (default is 512 Bytes, You can use bs=64k for bigger disks)
  • noerror: continue after read errors.
  • sync: use synchronized I/O for data, also for metadata

If you want to restore hard disk, reverse the source and destination and run the above command.

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

2. Backup Disk to Disk Image

Sometimes you may need to create a disk image and store it on other devices. This makes it easy to restore backup in case of issues. You can backup disk to disk image using dd command. Here is an example command to backup /dev/sda to image /var/tmp/sda.img

# dd if=/dev/sda of=/var/tmp/sda.img

if: source disk drive (/dev/sda)
of: destination disk drive (/var/tmp/sda.img)

It is similar to taking backup of disk on another disk. The main difference is that we specify the location of disk image file, instead of mentioning another partition.

Now, to restore the disk image, reverse the source and destination in above command.

# dd if=/var/tmp/sda_disk.img of=/dev/sda

Of course, you can always restore the above image to another disk partition (e.g. /dev/sdb).

You can also use the above commands to backup your disk to disk image on NFS server.

Check if there is enough server space on your NFS Server.

# showmount -e nfs_server_IP

Next, backup your disk to NFS server as image file.

# dd if=/dev/sda of=/nfs_test/sda_disk.img

In this article, we have learnt how to backup disk to another disk or as disk image, as well as how to restore these backups.

You can also add these commands to your shell scripts to automate the process, or run it as a cronjob to take regular backups. Here is an example.

Open crontab with the following command.

$ crontab -e

Add the following line to it to run disk backup everyday at 10.a.m.

0 10 * * * dd if=/dev/sda of=/dev/sdb conv=noerror,sync >/dev/null 2>&1

You can use dd command on almost every Linux distribution since it is pre-installed on all systems.

Also read:

How to Lock & Unlock Users in Linux
How to Change FTP Port in Linux
How to Check CVE Vulnerability in Linux
How to Capture Top Command Output to File
How to Check Supported TLS/SSL Version in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *