Wednesday, August 6, 2014

Backup And Restore A Hard Drive/Disk Partition Table

This is a small demonstration on how to backup and restore partition table data in a Linux system. For this demo, I've taken '/dev/sde'  hard drive as an example. So, in the first part, I'd backup the drive partition table and later part we'd destroy the data, hence, we'd restore it finally using the backup copy.

1) The device '/dev/sde' which got one partition '/dev/sde1' with a label '/label_test' mounted on '/test2' as seen below:

{Checking the partition details of the device}

[root@RHEL4 ~]# fdisk -l /dev/sde

Disk /dev/sde: 213 MB, 213909504 bytes
64 heads, 32 sectors/track, 204 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sde1               1         204      208880   83  Linux



{Checking the mount status of the device}

[root@RHEL4 ~]# mount | grep sde
/dev/sde1 on /test2 type ext3 (rw)


{Checking the label associated with the device}

[root@RHEL4 ~]# e2label /dev/sde1
/label_test


{Checking to see if this device is added in fstab for automatic mount on bootup}

[root@RHEL4 ~]# grep test2 /etc/fstab
LABEL=/label_test   /test2     ext3    defaults   1 2


2) Backup '/dev/sde' partition table using 'sfdisk' command:

[root@RHEL4 ~]# sfdisk -d /dev/sde > /root/partition_table_backup/sde.ptable


{Checking to see the file contents}

[root@RHEL4 ~]# cat /root/partition_table_backup/sde.ptable
# partition table of /dev/sde
unit: sectors

/dev/sde1 : start=       32, size=   417760, Id=83
/dev/sde2 : start=        0, size=        0, Id= 0
/dev/sde3 : start=        0, size=        0, Id= 0
/dev/sde4 : start=        0, size=        0, Id= 0


3) Now, let's erase the partition table of '/dev/sde'.

[root@RHEL4 ~]# dd if=/dev/zero of=/dev/sde bs=1 count=512
512+0 records in
512+0 records out

4) Check the partition of /dev/sde1 now, it should not be available:

[root@RHEL4 ~]# fdisk -l /dev/sde

Disk /dev/sde: 213 MB, 213909504 bytes
64 heads, 32 sectors/track, 204 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes

Disk /dev/sde doesn't contain a valid partition table


- System could be rebooted here, however, it would throw up Control D error since could not find the device /dev/sde1. So, need to comment this entry in fstab and reboot. After which we could restore this.

5) Let's restore the partition table of the device '/dev/sde'.

[root@RHEL4 ~]# sfdisk /dev/sde < /root/partition_table_backup/sde.ptable
Checking that no-one is using this disk right now ...
OK

Disk /dev/sde: 204 cylinders, 64 heads, 32 sectors/track

sfdisk: ERROR: sector 0 does not have an msdos signature
 /dev/sde: unrecognized partition
Old situation:
No partitions found
New situation:
Units = sectors of 512 bytes, counting from 0

   Device Boot    Start       End   #sectors  Id  System
/dev/sde1            32    417791     417760  83  Linux
/dev/sde2             0         -          0   0  Empty
/dev/sde3             0         -          0   0  Empty
/dev/sde4             0         -          0   0  Empty
Warning: no primary partition is marked bootable (active)
This does not matter for LILO, but the DOS MBR will not boot this disk.
Successfully wrote the new partition table

Re-reading the partition table ...

If you created or changed a DOS partition, /dev/foo7, say, then use dd(1)
to zero the first 512 bytes:  dd if=/dev/zero of=/dev/foo7 bs=512 count=1
(See fdisk(8).)

6)  Verify if partition table is restored.

[root@RHEL4 ~]# fdisk -l /dev/sde

Disk /dev/sde: 213 MB, 213909504 bytes
64 heads, 32 sectors/track, 204 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sde1               1         204      208880   83  Linux


Partition table has been restored successfully!

Reference :

How can I backup and restore partition table?

How to create backup of partition table and Master boot record for Disaster recovery purpose?

No comments: