KNOW-HOW System Recovery with tar

Using tar to prepare for an emergency LIFELIFE BOATBOAT

Use a tarball to restore your system in next to no time – www.photocase.com without a complete re-install.

BY MARKUS KLIMKE

inux has no end of options for ferent filesystem, in case of emergency doing so, tar tells us that the image file backing up data, however, a one- or if you need to create a copy. has changed during the archiving pro- Lto-one copy will always use the The -c option tells tar to create an ar- cess. Although you might be able to live same amount of space as the hard disk chive; -p passes in the permissions, and with this, there is not much point in add- partition. You can save space by bun- -v provides verbose output on screen. -f ing all your files to the image. dling the backup data into a compressed assigns a name to the file you are creat- You will probably want to back up the archive. ing. The slash following the image name data in your home directory on an exter- indicates the starting point for the ar- nal storage medium, but volatile data Swiss Army Knife chive, and the -j flag creates a Bzip2 like this should not be part of your re- tar is a die-hard tool from the early days compressed archive. covery image. This data has nothing to of Unix, and a GNU variant of tar is in- Instead of -j, you could specify -z to do with restoring the system and only cluded with any . The use gzip to compress the archive. The inflates the tarball unnecessarily. program, which was originally devel- differences are the resulting size of, and To explicitly exclude files and directo- oped as a “tape archiver,” has been con- the time taken to create, the archive file: ries from the tarball, tar gives you the tinually extended over the years. Its orig- gzip is quicker, but it creates larger files; --exclude-from option. The flag reads the inal task of storing data on tape is just bzip2 takes longer, but it uses a more ef- file and directory entries to be excluded one of many tasks tar handles today. ficient compression method. How long a from the archive from a file: At first glance it might seem strange to backup will take, and the size of the re- bundle a Linux installation in a tarball sulting tarball, depend on the CPU and user # cat << EOF >> U and unpack it at a different location. the data you are handling. /tmp/out However, the following command: > /home/* Specifying Content > /mnt/* tar -cvjpf U So far, so good: but if you attempt a > /usr/portage/distfiles/* /tmp/image-date.tar.bz2 / backup like the one I just described, you > /tmp/backup-date.tar.bz2 will notice a number of error messages. > EOF writes a complete snapshot of your We told tar to create an image of the root user # tar -cvjp --U Linux installation to a file. You can un- filesystem, so it will try to recursively exclude-from=/tmp/out -f U pack the file on a different disk, or a dif- add itself to the backup. And while /tmp/backup-date.tar.bz2

52 ISSUE 70 SEPTEMBER 2006 WWW.LINUX - MAGAZINE.COM System Recovery with tar KNOW-HOW

The wildcards that follow the directory names remove all files and directories below the directory you want to exclude. If you leave out the wildcard, tar will ex- clude the whole directory from the ar- chive. The exclude file should also in- clude the image filename. Before you create an image, run the mount command to check what you have mounted. Any external media or network shares will become part of the image if you forget to unmount or ex- clude. Some distributions do not mount the boot partition for security reasons. In this case, you should make sure that root is added to the image. After creating an image with the con- tent you need, the next step is to use a burning tool of your choice – say K3b – to burn the image onto a DVD.

Rescue Line Figure 1: Partitioning a hard disk by redirecting the commands to fdisk. The results are During the recovery process – that is, the output at the end. process of restoring the image you cre- ated – a Linux live CD can be a big help, on. For partitions, a serial number is ap- by pressing w (for “write”) to store the as it lets you boot a minimal distribu- pended to the device name; for example, information in the Master Boot Record tion. The most popular live CD has to be /dev/sda1 is the first partition on the first (MBR). It is a good idea to verify the par- [1]. As the steps for restoring S-ATA or SCSI disk. titioning at this stage by giving the fdisk the image take place at the command Let’s launch into the recovery process -l /dev/hda command (Figure 1). line, a text-based live CD, such as the at the live CD prompt. Start by typing If you don’t want to enter the partitio- Gentoo Live CD [2], Grml [3], or the fdisk /dev/hda to enter fdisk’s command ning commands interactively, you can Suse or Red Hat installation in rescue mode. Typing m at the fdisk prompt lists store the commands in a control file and mode will be just fine. All of these distri- the available options. If the disk is not pass the file in to fdisk. A control file is butions give you all the tools you need. brand new, you may need to remove any shown in Listing 1. To free up the DVD drive for the disk partitions already on the disk. Type p The next item on the list is to format with the recovery image after booting, (for “print”) to output an overview of the partitions you just created with a you have to boot the live distribution in the current hard disk partitions; d (“de- filesystem to manage the files and direc- a way that places the operating system lete”) will delete the partitions. in your machine’s memory. You can then Table 1 shows the commands you Listing 1: Control File unmount the live CD after booting and need to set up a hard disk with four par- 01 livecd root # cat << EOF >> insert the medium with your image. titions. Your requirements may be differ- /tmp/fdisk.part Most live systems call this boot parame- ent. You can create an “extended” parti- ter toram. If you have Knoppix, you tion as partition four and then add a few 02 > n would enter knoppix toram; for Grml, logical partitions, but four partitions 03 > p this would be grml toram. And if you should be fine for a desktop system. 04 > 1 have the Gentoo Live CD, the gentoo The n (for “new”) command tells fdisk 05 > docache command will do the trick. to create a new partition. Fdisk will ask you if this is a primary or extended parti- 06 > +64M Preparing Your Hard Disk tion. The tool also expects the partition 07 > n Before restoring the image, partition and number and the start and end cylinder 08 > p format your hard disk with the text- numbers, or the size (in kilobytes, mega- 09 > 2 based fdisk partitioning tool. bytes or gigabytes). 10 > In Linux, hard disks are reference by The a (for “active”) command tags device names like /dev/hda (IDE), or / the first partition as your boot partition. 11 ... dev/sda (S-ATA/ SCSI). The last letter in For the swap partition, you will need to 12 > w the device name (this is, “a” in our ex- press t (for “type”) and stipulate type 82. 13 > EOF ample) refers to the drive “number.” For Now that you have set up the parti- 14 livecd root # fdisk example, the second IDE disk in a PC tioning to your liking, you will want to /dev/hda < /tmp/fdisk.part has a device name of /dev/hdb, and so write the partitioning information to disk

WWW.LINUX - MAGAZINE.COM ISSUE 70 SEPTEMBER 2006 53 KNOW-HOW System Recovery with tar

tories you will be restoring. The most Table 1: Fdisk Commands (Examples) popular filesystems for Linux desktop systems are Ext2, Ext3, and ReiserFS. Name Partition Command Type Number Start/ Code End Journaling filesystems like Ext3 and Rei- /boot /dev/ hda1 n p 1 [Enter] +64M serFS need more space on disk. With a swap /dev/ hda2 n p 2 [Enter] +1G filesystem such as Ext2, 32 MB would be / /dev/ hda3 n p 3 [Enter] +10G fine for /boot. The formatting commands /home /dev/ hda4 n p 4 [Enter] [Enter] - /dev/ hda1 a - 1 - - for the three filesystems are mkfs.ext2, - /dev/ hda2 t - 2 82 - mkfs.ext3, and mkfs.reiserfs, followed by the partition name. To format the root partition from our example with Rei- puters with S-ATA disks, the required change to the unpacked image on the serFS, enter mkfs.reiserfs /dev/hda3. mk- drive is typically /dev/hda; if you have new hard disk – as if the system had swap /dev/hda2 creates the swap parti- two IDE disks in your machine, the just booted from the image – using ch- tion. If you are interested in more details name could be /dev/hdc. root (“change root”). /proc has a variety on the topic of partitioning, check out Finally, to unpack the image in the of system information that grub needs, the Gentoo documentation at [4]. right partition (/mnt/gentoo in our ex- and this makes it imperative to mount ample), run the following commands the directory before changing root. Restoring the Image After calling grub from within the ch- This more or less completes the prepara- nonumber root jail, you should see a prompt: tory work, and we can move on to re- cd /mnt/gentoo storing the image on disk. The last step tar -xvjpf /mnt/cdromU livecd # grub is to mount the partitions you just cre- /image-date.tar.bz2 grub> root (hd0,0) ated on the live CD’s filesystem. We used grub> setup (hd0) Gentoo Live and did this: The -x (for “extract”) option is the only grub> quit difference between the commands for user # mount /dev/hda3 U unpacking and archiving; it tells tar to The root command points to the boot /mnt/gentoo unpack the archive. Unpacking will take partition, in our example using (hd0,0), user # mkdir /mnt/gentoo/U a while, but it is quicker than a new in- that is, to the first partition on the first {boot,home} stall. hard disk in the machine. This is the user # mount /dev/hda1 U right command if your installation does /mnt/gentoo/boot Tidying Up not have a separate boot partition, but user # mount /dev/hda4 U If your partitioning and drive hierarchy uses /boot below the root partition. If /mnt/gentoo/home is unchanged with respect to your origi- you are restoring the image to a second user # swapon /dev/hda2 nal installation, there should be no need hard disk, the command would be root to change the boot manager configura- (hd1,0). Use the setup command to The mountpoint /mnt/gentoo is a Gentoo tion or the partitioning table. However, write the boot manager to the MBR on convention; don’t forget to create a suit- you will need to enter any partitioning the disk; the boot manager typically able mountpoint for other live CDs. changes in the /etc/fstab file that you re- resides on (hd0), that is, the first hard Then start by mounting the hard disk store. If you move the image, by unpack- disk in a machine. root directory. The command in Line 2 ing on your second hard disk instead of creates mountpoints for the boot and your first, for example, you will need to Reboot home partitions. The last line enables modify the entries in /boot/grub/grub. Before you boot into your “old/ new” the swap partition. conf to reflect the move. Linux from the live CD, don’t forget to The hard disk is all set up for you to The decisive step in tidying up after umount any partitions you have restore the image. To do so, first un- restoring to a new disk is to install the mounted by running umount. Disable mount the live CD. The command for boot manager, which will be missing at the swap partition by entering swapoff this with most distributions is umount this stage, in the master boot record /dev/hda2. Now you can tell your ma- /mnt/cdrom. Then insert the DVD with (MBR) of the first hard disk. If you don’t chine to reboot, lean back, and enjoy the image file, and type mount /dev/hdb do this, the boot process will fail. More watching your restored installation /mnt/cdrom to mount the file. In our ex- or less all current distributions use the launch. ■ ample, /dev/hdb is the device name for Grub boot manager (Grand Unified Boot the DVD drive; the device name can vary Loader), which uses a small trick to rein- INFO depending on your hardware. For com- stall the data from the restored image. [1] Knoppix: http:// www. knoppix. org The commands [2] : http:// www. gentoo. org/ GLOSSARY main/ en/ mirrors. xml MBR: The first data block (512 bytes) of user # mount -t proc none U [3] Grml homepage: http:// www. grml. org a bootable, partitioned storage medium. /mnt/gentoo/proc [4] Gentoo Linux Manual: http:// www. It contains a partitioning table and the user # chroot /mnt/gentoo U gentoo. org/ doc/ en/ handbook/ boot loader. /bin/bash handbook-x86. xml?part=1&chap=4

54 ISSUE 70 SEPTEMBER 2006 WWW.LINUX - MAGAZINE.COM