Using Tar to Prepare for an Emergency LIFELIFE BOATBOAT
Total Page:16
File Type:pdf, Size:1020Kb
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 Linux distribution. 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- Knoppix [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].