Wednesday, August 6, 2014

Backup And Restore A Hard Disk Partition Table

- Device : /dev/sde is taken as an example here.

(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, we'd erase the partition table of /dev/sde as below:

[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) Restoring the partition table of /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)  Now, run fdisk -l on /dev/sde to find out the partition

[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: