clonando uma particao

Upload: salvianoleao

Post on 01-Jun-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 Clonando Uma Particao

    1/8

    Comandos Linux

    Salviano A. Leão

    21/12/2013

    Sumário

    1 Introdução   1

    2 Usando o partclone   2

    3 Using dd   2

    3.1 Cloning a partition   . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33.2 Cloning an entire hard disk   . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33.3 Backing up the MBR   . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43.4 Create disk image  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43.5 Restore system   . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53.6 Examples with compression   . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

    4 Using cp   7

    5 Disk cloning software   7

    1 Introdução

    Neste tutorial pretende-se mostrar como fazer o backup de uma partição usando a linha decomando. Primeiro, não se deve esquecer de verificar a partição para depois fazer o backup.Tanto para verificar como para fazer um backup é necessário que a partição não esteja montada.Neste caso usa-se um “live CD” que contenha os programas necessário, ou faz-se um pen-drivebootável para tal fim. O que será dito a seguir, supõe-se que o sistema foi inicializado por

    um live CD ou pen-drive. Note que em um pen-drive, co espaço suficiente é possível instalaros softwares que você não tem, enquanto no live CD isso não é possível. Se estiver usandoum pen-drive com uma distribuição baseada no debian, então, por exemplo, para instalar opartclone digite:

    > s ud o a pt - g e t i n st a ll p a rt c lo n e

    em seguida você terá o  partclone disponível.Antes de iniciar o processo de backup é necessário identificar a partição e para isso, pode-se

    usar um software gráfico com o  gparted ou via linha de comando o software  fdisk, da seguinteforma

    > s ud o f di sk - l

    1

  • 8/8/2019 Clonando Uma Particao

    2/8

    ou usando o comando com recursos mais avançados  cfdisk

    > s ud o c fd i sk

    Então, identifica-se a partição a ser clonada, aqui será considerado nos exemplos que será a

    partição  /dev/sda1.Para clonar a partição  /dev/sda1 formatada com ext4, é aconselhável, primeiro verificar o

    estado da partição com

    > s ud o e 2f sc k - f - y - v / d e v / sd a1

    Não se esqueça de mudar a partição  /dev/sda1  para a partição que se deseja fazer uma cópiade segurança, um backup.

    2 Usando o partclone

    Usando o  partclone  para clonar a partição  /dev/sda1  formatada com  ext4  sem compressão.Para fazer uma cópia de segurança da partição sem comprimir a imagem criada, e salvar noarquivo /path/image_sda1.pcl  faça:

    > p a rt c lo n e . e xt 4 - c - s / d ev / s d a1 - o / p a th / i m a ge _ sd a 1 . pc l

    Para restaurar digite:

    > s ud o p a rt c lo n e . e xt 4 - r - s / p a th / i m a ge _ sd a 1 . p cl - o / d ev / s d a1

    Para realizar o mesmo procedimento com compressão da imagem gerada faça:

    > p a rt c lo n e . e xt 4 - c - s / d ev / s d a1 | g zi p - c > / p a th / i m a ge _ sd a 1 . pc l . gz

    Note que para se obter uma compressão máxima use a opção: "gzip -c9".Para restaurar a partição digite:

    > z ca t / p a th / i m a ge _ sd a 1 . p cl . g z | s ud o p a rt c lo n e . e xt 4 - r - o / d ev / s d a1

    Note que o  partclone necessita se chamado com os privilégios do usuário  root, no ubuntue variantes use o sudo.

    3 Using dd

    The dd command is a simple, yet versatile and powerful tool. It can be used to copy fromsource to destination, block-by-block, regardless of their filesystem types or operating systems.A convenient method is to use dd from a live environment, as in a livecd. Warning:

    •  This paragraph used to contain several errors that misrepresented the inner workings of 

    the dd command. I tried to fix them, but YMMV.

    2

  • 8/8/2019 Clonando Uma Particao

    3/8

    •   As with any command of this type, you should be very cautious when using it; it candestroy data. Remember the order of input file (if=) and output file (of=) and do notreverse them! Always ensure that the destination drive or partition (of=) is of equal orgreater size than the source (if=).

    3.1 Cloning a partition

    From physical disk  /dev/sda, partition 1, to physical disk  /dev/sdb, partition 1.

    # d d i f = / d ev / s d a 1 o f = / d ev / s d b 1 b s = 5 12 c o nv = n o e r ro r , s y n c

    Warning: If output file  of= (sdb1 in the example) does not exist, dd will create a file withthis name and will start filling up your root file system.

    3.2 Cloning an entire hard disk

    From physical disk  /dev/sdX to physical disk  /dev/sdY

    # d d i f = / d ev / s d X o f = / d ev / s d Y b s = 5 12 c o nv = n o e rr o r , s y n c

    This will clone the entire drive, including MBR (and therefore bootloader), all partitions,UUIDs, and data.

    •  noerror instructs dd to continue operation, ignoring all read errors. Default behavior fordd is to halt at any error.

    •  sync fills input blocks with zeroes if there were any read errors, so data offsets stay insync.

    •   bs=512   sets the block size to 512 bytes, the "classic"block size for hard drives. If andonly if your hard drives have a 4K block size, you may use "4096"instead of "512". Also,please read the warning below, because there is more to this than just "block sizes- it alsoinfluences how read errors propagate.

    Warning: The block size you specify influences how read errors are handled. Read below.The dd utility technically has an "input block size"(IBS) and an "output block size"(OBS).

    When you set bs, you effectively set both IBS and OBS. Normally, if your block size is, say,1M, dd will read 1M bytes and write 1M bytes. But if a read error occurs, things will go

    wrong. Many people seem to think that dd will "fill up read errors with zeroes"if you use thenoerror,sync options, but this is not what happens. dd will, according to documentation, fillup the OBS to IBS size, which means adding zeroes at the end of the block. This means, for adisk, that effectively the whole 1M would become messed up because of a single 512 byte readerror in the beginning of the read: ERROR6789 would become 678900000 instead of 000006789.

    If you are positive that your disk does not contain any errors, you could proceed usinga larger block size, which will increase the speed of your copying manifold. For example,changing bs from 512 to 64K changed copying speed from 35MB/s to 120MB/s on a simpleCeleron 2.7GHz system. But keep in mind, that read errors on the source disk will end up asblock errors on the destination disk, i.e. a single 512 bytes read error will mess up the whole

    64k output block. Tip: If you would like to view dd progressing, you can send it a USR1 signal.pidof dd will tell you the PID of dd, then use kill -USR1. In one line: kill -USR1 $(pidof dd).

    Note:

    3

  • 8/8/2019 Clonando Uma Particao

    4/8

    •   To regain unique UUIDs of an Ext2/3/4 filesystem, use tune2fs   /dev/sdXY -U randomon every partitions.

    •   Partition table changes from dd are not registered by the kernel. To notify of changeswithout rebooting, use a utility like partprobe (part of GNU parted).

    3.3 Backing up the MBR

    The MBR is stored in the the first 512 bytes of the disk. It consist of 3 parts:

    1.) The first 446 bytes contain the boot loader.

    2.) The next 64 bytes contain the partition table (4 entries of 16 bytes each, one entry for eachprimary partition).

    3.) The last 2 bytes contain an identifier

    To save the MBR into the file "mbr.img":

    # d d i f =/ d e v / sd X o f =/ p a th / t o / m br _ fi l e . im g b s = 51 2 c o un t = 1

    To restore (be careful: this could destroy your existing partition table and with it access toall data on the disk):

    # d d i f = / p a th / t o / m b r _ f i le . i m g o f = / d ev / s d X

    If you only want to restore the boot loader, but not the primary partition table entries, justrestore the first 446 bytes of the MBR:

    # d d i f =/ p a th / t o / m br _ fi l e . im g o f =/ d e v / sd X b s = 44 6 c o un t = 1

    To restore only the partition table, one must use

    # d d i f =/ p a th / t o / m br _ fi l e . im g o f =/ d e v / sd X b s =1 s ki p = 44 6 c ou n t = 64

    You can also get the MBR from a full dd disk image.

    # d d i f = / p a th / t o / d i s k . i m g o f = / p a t h / t o / m b r _ fi l e . i m g b s = 5 12 c o u nt = 1

    3.4 Create disk image

    1.) Boot from a liveCD or liveUSB.

    2.) Make sure no partitions are mounted from the source hard drive.

    3.) Mount the external HD.

    4.) Backup the drive,

    # dd i f =/ d ev / s dX c on v =s yn c , n oe rr or b s =6 4 K | g zi p - c > / p at h / to / b ac ku p .←

    i m g . g z

    4

  • 8/8/2019 Clonando Uma Particao

    5/8

    If necessary (e.g. when the format of the external HD is FAT32) split the disk image involumes (see also split man pages).

    # dd i f =/ d ev / s dX c on v =s yn c , n oe rr or b s =6 4 K | g zi p - c | s pl it - a3 - b2 G -   ←

    / p a t h / t o / b a c k u p . i m g . g z

    5.) Save extra information about the drive geometry necessary in order to interpret the par-tition table stored within the image. The most important of which is the cylinder size.

    # f di s k - l / d ev / s dX > / p a th / t o / l is t _f d is k . i nf o

    Note: You may wish to use a block size (bs=) that is equal to the amount of cache onthe HD you are backing up. For example, bs=8192K works for an 8MB cache. The 64Kmentioned in this article is better than the default bs=512 bytes, but it will run faster

    with a larger bs=.

    3.5 Restore system

    To restore your system:

    # g u nz i p - c / p a th / t o / b ac k up . i m g . gz | d d o f =/ d e v / sd X

    When the image has been split, use the following instead:

    # c at / p a th / t o / b ac k up . i m g . gz * | g u nz i p - c | d d o f =/ d e v / sd X

    3.6 Examples with compression

    When you need to create the hard drive or a single partition compressed backup image file youmust use compression tools which can do backup from a stdout and the dd command. Thosecompressed files cannot be mounted by the mount command but are useful to know how tocreate and restore them.

    7zip  Install the [32]p7zip package from the [33]official repositories. This backup example will

    split the dd command output in the files by up to the 100 megabyte each:

    # d d i f =/ d ev / s dX Y | 7 z a - v 10 0m - t7 z - si i ma ge - f i le . 7 z

    Restore with 7zip:

    # 7 z x - so ima ge - f il e .7 z | dd of =/ de v/ sd XY

    Note: 7zip can split only the 7z compression type files

    Zip   Install the [34]zip package from the [35]official repositories, which contains zipsplit amongother utilities for the management of zip archives. It will create a file with -"name insidethe image-file.zip file which will contain data from the dd command output. To make a

    5

  • 8/8/2019 Clonando Uma Particao

    6/8

    raw output of the file you can use the -cp option with unzip in stdout for the dd command.Backup:

    # d d i f =/ d e v / sd XY | z ip - - c om p re s si o n - m e th o d b zi p 2 i ma ge - f i le . z ip -

    Restore:

    # u n zi p - c p i ma ge - f i le . z ip | d d o f =/ d e v / sd XY

    The zip tool cannot split files on the fly but you can use the zipsplit utility on an alreadycreated file.

    See also man zip for more information.

    Rar   Install the [36]rar package from the [37]AUR. Warning: The rar examples were madebased on the manuals, please confirm!

    This should do a backup and split the creating file on the fly in by up to 150 megabytefiles each.

    # d d i f =/ d e v / sd XY | r ar a - v 15 0 m - s ii ma ge - f i le . r ar

    This should restore

    # u nr ar x - p i ma ge - f i le . r ar | d d o f =/ d ev / s dX Y

    or you can use the rar instead of the [38]unrar utility. The unrar utility is available inthe official repositories and can be installed with pacman -S unrar.

    Bzip2   Creation by using the dd is more safe and use to be error free:

    # d d i f =/ d e v / sd XY | b z ip 2 - f 5 > c o mp r e ss e df i le . b z ip 2

    9 3 70 1 6+ 0 r e co r ds i n

    9 3 7 01 6 + 0 r e c or d s o u t

    4 79 75 21 92 b yt es ( 48 0 M B) c op ie d , 9 4. 70 02 s , 5 .1 M B/ s

    And a safe way of restoring with combination of the dd:

    # b u nz i p2 - d c c o mp r es s ed f il e . b z ip 2 | d d o f =/ d e v / sd XY

    or

    # b z ca t c o m pr e ss e df i le . b z ip 2 | d d o f =/ d e v / sd XY

    Warning: Never ever use the bzip2 -kdc imgage.bzip2 > /dev/sdXY and bzip2 -kc /dev/sdXYmethods for serious backup of partitions and disks. The errors might be due the end of 

    the device or partition and the restore process gives also errors due the truncated end.

    6

  • 8/8/2019 Clonando Uma Particao

    7/8

    4 Using cp

    The cp program can be used to clone a disk, one partition at a time. An advantage to usingcp is that the filesystem type of the destination partition(s) may be the same or different thanthe source. For safety, perform the process from a live environment. Note: This method should

    not be considered in the same category as disk cloning on the level at which dd operates. Also,it has been reported that even with the -a flag, some extended attributes may not be copied.For better results, rsync or tar should be used.

    The basic procedure from a live environment will be:

    •  Create the new destination partition(s) using fdisk, cfdisk or other tools available in thelive environment.

    •  Create a filesystem on each of the newly created partitions.

    Example:

    # m kf s - t e xt 3 / d ev / s dX Y

    •  Mount the source and destination partitions. Example:

    # m o un t - t e xt 3 / d ev / s d XY / m nt / s o u rc e

    # m o un t - t e xt 3 / d ev / s d ZY / m nt / d e s ti n at i on

    •  Copy the files from the source partition to the destination

    # c p - a / p a th / t o / s ou rc e / * / p a th / t o / d es t in a ti o n

    - a : p r es e rv e a ll a t tr ib u te s , n ev e r f o ll o w s y mb o li c l in ks a nd c op y

    r e c u r s i v e l y

    •  Change the mount points of the newly cloned partitions in  /etc/fstab accordingly

    •  Finally, install the GRUB bootloader if necessary. (See [39]GRUB)

    5 Disk cloning software•  Partclone provides utilities to save and restore used blocks on a partition and supports

    ext2, ext3, ext4, hfs+, reiserfs, reiser4, btrfs, vmfs3, vmfs5, xfs, jfs, ufs, ntfs, fat(12/16/32)and exfat. Optionally, a ncurses interface can be used. Partclone is available in thecommunity repository.

    •   Partimage, an ncurses program, is available in the community repos. Partimage does notcurrently support ext4 or btrfs filesystems. NTFS is experimental.

    •  PartedMagic has a very nice live cd/usb with PartImage and other recovery tools.

    •   Mindi is a linux distribution specifically for disk clone backup. It comes with its owncloning program, Mondo Rescue.

    7

  • 8/8/2019 Clonando Uma Particao

    8/8

    •   Acronis True Image is a commercial disk cloner for Windows. It allows you to create alive (from within Windows), so you do not need a working Windows install on the actualmachine to use it. After registration of the Acronis software on their website, you willbe able to download a Linux based Live cd and/or plugins for BartPE for creation of theWindows based live cd. It can also create a WinPE Live CD based on Windows. The

    created ISO Live CD image by Acronis doesn’t have the hybrid boot ability and cannotbe written to USB storage as a raw file.

    •  FSArchiver allows you to save the contents of a file system to a compressed archive file.Can be found on the System Rescue CD.

    •  Clonezilla is an enhanced partition imager which can also restore entire disks as well aspartitions. Clonezilla is included on the Arch Linux installation media.

    •  Redo Backup and Recovery is a Live CD featuring a graphical front-end to partclone.

    8