You are on page 1of 4

Software&security

Computergames

Lifetopics

Hillbillyphysics

Greatestsites

3Dart

Modelplanes

Howtobackupandrestorethepartitiontable
Updated:August26,2015
Advertise!

ForthoseofyounewtoDedoimedoarticles,youhaveaseriousbacklogofreadingaheadof
you,beforeyoucanconfidentlynavigatetheperilouswatersoftheNerdyBay,wherewords
likepartitionandtableareusedwithwildabandon.
Butifyouareinthegroove,thenyoushalllikethisarticle.Today,wewilltalkaboutpartition
tablebackupandrestore,whyandwhenyoushouldundertakethisseeminglydubiousstep,
andwhyit'suseful.Afterme.

Wouldyouliketo
advertiseyour
product/siteon
Dedoimedo?
Readmore

Donateto
Dedoimedo!
Doyouwantto
helpmetakeearly
retirement?How
aboutdonating
somedineroto
Dedoimedo?
Readmore

Note:CourtesyofFreeImages/MarcinBarlowski.

Foreword
Sortof.So,ifyouhappentohaveaspareharddisk,withmultiplepartitionsand/orfrequent
usethatinvolvesformatting,partitioning,movingandresizingofexistingpartitions,data
copyandsuch,youmaywanttoknowthattheintegrityofthosebitsyoudon'twantdeleted
ispreservedwhenyougoaboutchangingthosethingsthatyoudo.
Indeed,wheneveryoufiddlewithdisksandpartitions,there'sachancesomethingmightgo
wrong.Youcouldgetmomentarilydistractedorconfusedanddeletethewrongdevice,or
worse,formatit,withallitspreciousdata.Wetalkedaboutdatarecoveryinthepast,now
wewilltalkaboutsomethingelse.
Whatdoyoudoifyousuddenlydestroythepartitiontableofadevice?Ordeleteapartition?
Now,atfirstglance,thesemayappeartobehighlydestructiveoperations,buttheyarequite
innocent.Infact,mostofthetime,deletionofdata,evenformatting,isrelativelyinnocent.
It'snotaboutwhatisstoredondisk,butwhatyouthinkisstored.
Deletingafilemerelymeansdeindexingitfromthedisklog,makingthespacepreviously
usedbythatfileavailableforsomeother,futureuse.Deletingapartitionisthesamething.
Youdeallocatethemarkersthattellthedisk,andsubsequently,theoperatingsystemusing
thediskwhichpiecesofthediskspaceareavailableforuse,andwhichareconsideredfree.
Andattheverytop,there'sthepartitiontable.Ifyougetthisentrydeleted,thenthedisk
willappearasaraw,unuseddevice,eventhoughitmaycontainaperfectlysaneandusable
structureofpartitions,filesystems,inodes,andactualdata.
Goingbacktoourinitialstatement,wewanttobeabletoquicklyandeasilyrecoverour
partitions,shouldtheysomehowgetlost.WhichiswhyI'mgoingtoshowyouhowyoucan

backupthepartitiontable,bothonMBRandGPTdisks.Wewillthenrecoveraseemingly
deaddisk.Andfinally,discusstheaddedvalueofthislittlegeekygame.

Backup
Wewillstartwithregulardisks,i.e.MBRedones,withthestandardmsdostypepartition
table.Inotherwords,youcanhaveuptofourprimarypartitions,oneofwhichcanbean
extendedpartitioncontaininglogicalpartitions.Inthiscase:
sfdiskd/dev/<disk>><disk>.layout
Here,weusethesdfiskcommand,withthedump(d)option.Theoutputisahuman
readablefile,listingthepartitions,includingthestartsector,sizeandfilesystemtype.Quite
usefulifyoueverneedtorecoversomethingonyourdevice.Ofcourse,youcanalwaysgo
forautomatedrecoverytools,butwhynotbesensible,andhaveabackup?
#partitiontableof/dev/sdc
unit:sectors
/dev/sdc1:start=4094,size=1248886786,Id=f
/dev/sdc2:start=0,size=0,Id=0
/dev/sdc3:start=0,size=0,Id=0
/dev/sdc4:start=0,size=0,Id=0
/dev/sdc5:start=4096,size=122880860,Id=83
/dev/sdc6:start=122886144,size=122880000,Id=83
/dev/sdc7:start=245768192,size=122880000,Id=7
/dev/sdc8:start=368650240,size=122880000,Id=83
/dev/sdc9:start=491540868,size=122865057,Id=7
/dev/sdc10:start=614414336,size=634476544,Id=83
Ifyourpartitionsarenotaligned,youmayseethiswarning:
sfdiskd/dev/sdc>/root/sdc.layout
sfdisk:Warning:extendedpartitiondoesnotstartatacylinder
boundary.DOSandLinuxwillinterpretthecontentsdifferently.
ForGPT,youshouldusesgdisk,whichisreadilyavailableinallLinuxdistributions.Fdiskand
sfdiskarenotsuitableforusewithGPTdevices,sorememberthat.Indeed,beforedoingany
backups,youshouldexecutethefdiskorsfdisktoolsinaconsolewindowstoseewhether
theywilldisplaymeaningfulinformationintherunningshell.Ifyouencounteranysortof
errorswithsfdisk,yououghttotrysgdisknext.

Usingtheddcommand
Anotherwaytoaccomplishthebackupismanually,usingtheddcommand.WithMBR,you
simplyneedtocopythefirst512bytesofthedisktoafile,andlateruseitforrestore.
Indeed,withour/dev/sdcdiskasaguineapig:
#fdiskl
Disk/dev/sda:320.1GB,320072933376bytes,625142448sectors
Units=sectorsof1*512=512bytes
Sectorsize(logical/physical):512bytes/512bytes
I/Osize(minimum/optimal):512bytes/512bytes
Disklabeltype:dos
Diskidentifier:0x000086a1
DeviceBootStartEndBlocksIdSystem
/dev/sda140946251410353125684715Extended
/dev/sda540968392703419430482Linuxswap...
/dev/sda6839475241799475120480000083Linux
/dev/sda74179968005203967995120000083Linux
/dev/sda8*5203988486251410355237109483Linux
Disk/dev/sdc:639.4GB,639432130560bytes,1248890880sectors
Units=sectorsof1*512=512bytes
Sectorsize(logical/physical):512bytes/512bytes
I/Osize(minimum/optimal):512bytes/512bytes
Disklabeltype:dos
Diskidentifier:0x00071ad3
DeviceBootStartEndBlocksIdSystem
/dev/sdc140941248890879624443393fW95Ext'd...
/dev/sdc540961228849556144043083Linux
/dev/sdc61228861442457661436144000083Linux

/dev/sdc7245768192368648191614400007HPFS...

/dev/sdc83686502404915302396144000083Linux

/dev/sdc949154086861440592461432528+7HPFS...

/dev/sdc10614414336124889087931723827283Linux
Andthen,somewickedddmagic:
ddif=/dev/sdcof=/root/sdc.headerbs=512count=1
1+0recordsin
1+0recordsout
512bytescopied,0.00104001s,3.9MB/s
Ifyouarenotsureaboutsizes,thenyoushouldconsulttheheaderformatstofigureoutthe
exactvalues.Forinstance,WikipediaentriesonMBRandGPTtellusallweneedtoknow.In
general,youshoulduse512and16,384bytes,respectively.

Restore
Let'smessupourdevice.Insteadofusingitasasource,wewilluseitasadestination,and
writeabunchofzerostoit.Thiswillsimulateascenariowherethepartitiontablegot
accidentallydeletedordamaged,preventingtheuseoftheharddisk.Inreality,theactual
damagemightbebiggerormorecomplexorcompletelyirreversible,butinourcase,let's
focusonthepartitiontable.
Again,wewillusetheddcommand.BTW,shouldIremindyouNOTtoplaywithproduction
devices,withoutfirstmakingsureyouhaveample,provenbackupselsewhere?Thisisavery
riskyactivity,anddon'ttryitunlessyouknowwhatyou'redoing.
ddif=/dev/zeroof=/dev/sdcbs=512count=1
1+0recordsin
1+0recordsout
512bytescopied,0.00175504s,2.3MB/s
Ifyouremovethediskandreconnectit,andthenrunfdiskagain,itwillappearasifhasno
partitions.Theoutputshownbelowisn'tveryuseful,butthat'swhatyou'llsee,withthe
exceptionofdisknotationandsize.
Disk/dev/sdc:639.4GB,639432130560bytes,1248890880sectors
Units=sectorsof1*512=512bytes
Sectorsize(logical/physical):512bytes/512bytes
I/Osize(minimum/optimal):512bytes/512bytes
Disklabeltype:dos
Diskidentifier:0x00071ad3
Torestorethedisktoafunctionalstate,useddtocopythesavedheaderbacktothedevice.
Makesureyoucorrectlyspecifytheblocksizevalueandthecount,becauseotherwise,you
maycausemoredamage.
ddif=/locationofsavedheaderof=/dev/sdcbs=512count=1

Whywouldyouwanttodothis?
Allright,sothere,agoodquestion.Whyshouldanysaneperson,i.e.nonLinuxgeekever
botherwiththis?Truthtobetold,ifyouhaveastaticsetup,thenyoudefinitelyneednotto
worryaboutyourpartitiontable.Thechanceofdamagethatisn'tacompletefailureofthe
diskisremote.Andinthatcase,youwillhavebiggerissuesthanjustamesseduppartition
table.Thisisagoodopportunitytoremindyouofproperimaginganddatabackupsasa
philosophyandstrategyinyourdailycomputing.
Now,ifyouhappentobeasomewhatmoretechsavvypersonandyoudoalotofsoftware
testing,includingLinuxdistroinstallations,partitioningandsuch,itmightbeprudenttokeep
acopyofthediskheader,justincasesomethinggoes(lightly)wrong.

Disclaimer
Now,thiskindofrudimentarybackup&restoreismeaninglessifyouoverwriteyourreal
data.Forinstance,ifyouzerothedisk,thennothingwillhelpreally.Furthermore,youmust
makesurenottoperformanydestructiveactionsbeforeandduringtherestoreoperation,as
youcouldaccidentallydeleteimportantstuff.

Conclusion
Thereyougo.Thislittletutorialwon'tmakeyoursalaryanyhigher,butitmayjustboostyou
withtheextradoseofconfidencewhenitcomestoworkingwithharddisksandpartitions
andmakingchanges.Ifyourememberthatallandanydeviceisjustabunchofzerosand
ones,thenthere'snothingsinisteraboutthem.Youcanbackupportionsofthoselongstrings
ofbinarycode,youcanoverwritethem,youcanrestorethem.
Andso,theabilitytobackupandrestorethepartitiontableisarathervaluableadditionin
one'stestingandtweakingarsenal,especiallyforpeoplewithaneverchangingsetupof
operatingsystems.Ifyoudoalotofformattingandpartitioning,soonerorlater,youmay
endwiththeneedtorestoreoneofyourdevices.Whenthathappens,it'salwaysusefulto
haveaproperbackup.Attheveryleast,youwillsavetime.Ihopeyoulikedthis.
Cheers.

del.icio.us stumble

Top

digg

Home

reddit

slashdot

Termsofuse

Contactme

About

Copyright@Dedoimedo.com20062016allrightsreserved