Ways to Try to Get Files Back
Now you’ve done it: You clobbered something important, and you really, really want it back.If you’re used to other systems, such as Windows, in which you can magically get deleted stuff back from the Recycle Bin, we’re sorry to tell you that UNIX doesn’t let you do that (unless you’re lucky enough to be using a UNIX desktop with a Trash tool; see the section in Chapter 4 about talkin’ trash). If it’s gone, it’s gone.
Copies, copies, everywhere
Maybe you have stashed away in a different directory other copies of the file you deleted. For our important files, we stick copies in a directory named save (or something similar). Also, sometimes you can reconstruct the information from a different form. If you clobber a word processor file, for example, you may have a backup version (the .bak files mentioned earlier in this chapter) that’s close to the current version. You may have printed the deleted file to a file (rather than directly to the printer) and can edit the print file back into document form.If you share your computer with other people or use a network, see whether someone else has a copy. It’s a rare file that exists in only one place.
Call in the backup squad
It’s really gone, huh? Now it’s time for the final line of defense: backups. You do have backups, don’t you?Tip We interrupt this chapter for a stern lecture: Always make backups. If your system administrator is on the ball, tape backups are made automatically every night. All you have to do is go to the administrator, with chocolate-chip cookies in hand, and ask politely for some help in getting your valuable file back from the previous night’s backups. If no backups exist, it’s fair to jump up and down and scream that someone had better get on the ball.Seriously, backups are a standard part of any administrator’s job, by either making the backups personally or overseeing operators who do the backing up. (After the procedures are set up, making backup tapes is so simple that you can practically train your dog to do it. One reason that it sometimes doesn’t get done is that backing up is boring.)
Remember Why you need backups
Making backups is a pain. The question isn’t whether you will lose data — it’s when. Here are some events that have sent us heading for the backup tapes:
The obvious one: We deleted a file by mistake.
Just as we were saving a file, the power failed and scrambled both the old and new versions of the file. Yikes!
One day, while working on the insides of the computer (one of us is a closet nerd), we accidentally dropped a screwdriver on the disk controller. Exciting sparks came out and fried the controller card that attaches the disk to the rest of the computer. We got a new controller card and found that, although the disk was physically fine, the new controller wasn’t quite compatible with the old one, and we had to reformat the disk and restore everything from tape.
We remembered hearing that we should absolutely, positively run a “disk-head parking” program before moving our hard drive, so we ran one. Unfortunately, it was a version that was incompatible with the hard drive, so it parked the disk head right off the edge of the disk, way out past what you may call the Long-Term Parking Area. We could hear the disk head banging on the side of the hard drive as it tried to get back on. Rats! (Modern disks park themselves, but we’ve had a few that became permanently unable to unpark.)
Back in the good old days, computers weighed about 15,000 pounds, and you needed a forklift to move one. Now they weigh about 25 pounds, which means that if the cleaning people bump into one with a vacuum cleaner, they can knock it over with a clunk that can put an unreadable ding into the disk. Oops — sorry, lady.
Although you probably have horror stories of your own, they all have the same moral: Make those backups.
If your system administrator doesn’t make backups, you had better learn how to do it yourself. If you have a tape unit on your machine, the procedure is usually as follows:
Put a tape in the tape unit and flip the latch to seat the tape firmly. Wait for the tape to load itself into the tape unit.
Give a command to copy files to the tape, usually the tar (tape archive) or pax (portable archive exchange) program.
On Sun workstations, the program is bar (backup archive). The exact things you have to say to the tar program vary from one system to another, mostly because of the peculiarities of different kinds of tape units. You can’t use the regular cp command because tapes aren’t logically organized the same way disks are. This process can take awhile — as long as overnight to dump a largish disk.
Take the tape out of the tape unit.
Write the current date on the label so that you know that it’s a current backup.
(The sensibly paranoid alternate several tapes, in case one of them goes bad. We describe this subject in more detail in the “Backup strategies” sidebar, later in this chapter.)
Put the tape back in its box, and put the box back on the shelf.
Once a week (or some other frequency), take the backup tape and store it off-site, such as in another office or at someone’s house or, if you’re really serious, in your safe-deposit box. (We do that.)
Linux The usual way to back up stuff on Linux is to use tar . To back up to tape, type this fabulously memorable command:
tar cvf /dev/rft0 *
The cvf means Create, Verbose Report, to File, and /dev/rft0 is the name of the tape unit. The * means to back up all the files in your directory; if you have a large number of them, you can list specific files and directories instead.If you have a large number of files, copying everything to tape can take awhile (perhaps an hour or so). You may want to do the backup over lunch. Or do what we do, and have your administrator arrange to run the tar program automatically every morning at about 3 a.m . We leave a tape in the tape unit every night. When we come back the next morning, the backup has been done, and we take the tape out and put it away. The tar command creates a report that is e-mailed to us so that we can check to see whether the tape was written correctly.
Disks, floppy and otherwise
Some systems don’t have tapes; they have removable disks, either diskettes or larger removable disks, such as ZIP disks or CD-R discs. Diskettes are a major pain for backup use for two reasons: You need a stack of them because each one holds only a little over a megabyte, and you must format them first.Before you can use a diskette, you must format it, which means that the computer writes some bookkeeping junk on it to mark where to put the data and in the process checks to be sure that no bad spots are on the disk. Fortunately, you have to do this only one time per disk. Formatting disks is easy but tedious and (sing along with us as we say this) varies, of course, from one system to another. You stick the blank disk in the computer, type the formatting command, something like fdformat , and UNIX formats it. If you’re stuck with diskette backups, ask your administrator for help in setting up the procedure, and while you’re feeding all the disks into the drive, consider getting a tape drive.
Linux If you back up to diskettes, type this line in Linux:
tar cvMf /dev/fd0 *
The M means multiple disks because you can’t put much stuff on one disk. Linux tells you when to swap disks. Be sure to write the backup date and the disk number on each disk so that they don’t get out of order.UNIX systems can also use ZIP disks, CD-R and CD-RW discs. ZIP disks are the easiest because (most likely with an expert’s help to set up) you can mount the disk as a part of the UNIX file system called something like /z , at which point you use cp to copy files to /z just like you do any other disk. When done, you tell UNIX to unmount the disk, and then you physically take it out and put it on the shelf. Writing CDs is harder because if you want to read your CD on other computers, it has to be in a format called ISO 9660, which UNIX systems can only write to with special programs. This is a good application for chocolate chip cookies, to bribe a nearby UNIX expert to set up a script to automate the process of collecting your files, making an ISO 9660 version of them, and writing it to a CD.
Thank goodness it’s backed up!
Getting stuff back from a tape or diskettes also involves the use of tar but is somewhat trickier because you want to restore just the files you clobbered. Ask for help — at least the first time. Generally speaking, you put the tape in the drive in the same way you did to make the backup, and then you type a tar or pax command similar to one of these:
tar xvf /dev/tape somedir/clobberedfile
pax -rv -f /dev/tape somedir/clobberedfile
In the tar command, xvf stands for extract verbosely from. In the pax command, -rv -f similarly means read verbosely from. Either way, the command is followed by the name of your tape drive (it’s often /dev/tape , except on Linux, where it’s usually /dev/rft0 ) and the name of the file to look for. The tape spins as tar or pax runs down the tape looking for the file you want. When it finds the file, it reads the file to the disk, reports that it did so, and stops. If you clobbered a bunch of files, you can use wildcards:
tar xvf /dev/tape "somedir/*"
pax -rv -f /dev/tape "somedir/*"
You have to use quotation marks around the wildcards because the files to match are on the tape, where tar or pax can find them, not on the disk where the shell (which normally handles wildcards) can find them.
Linux To restore files on Linux from a tape, type
tar xvf /dev/rft0 "somedir/*"
To restore from diskettes, type
tar xvMf /dev/fd0 "somedir/*"
If you want to see which files are on the tape and not restore any of them just now, use one of these commands:
tar tvf /dev/tape
pax -v -f /dev/tape
You should try to run the restore command from the same directory where the program that made the backup tape ran. If it’s a system backup, that’s probably the root directory ( / ).To restore from ZIP disks or CD, put the disk in the drive, tell UNIX to mount the disk (mounting CDs to read them works just fine), and then copy the backed up files from /z or /cdrom or wherever the disk is mounted. Then tell UNIX to unmount it and put the disk back on the shelf.
Tip Backup strategies
The obvious way to do tape backups is to copy the entire contents of the disk to a tape every night. Here are a couple reasons, however, that it may not be the best approach:
The disk may contain too much stuff to fit on a tape. Tape and disk manufacturers continually battle to see which one can outstrip the other. The battle has see-sawed back and forth — as we write, disks are pulling ahead because you can buy a 160GB disk drive for about $200, but a 160GB tape drive costs about $1,000, and the tapes are $90 apiece.
Because you may not notice for a day or two that you clobbered something important, a scheme that gives you a few days’ grace to get your data back would be nice.
Murphy’s Law says that the system will fail as it’s writing a backup tape, so you had better not depend on one tape.
The best scheme is a combination of rotating and incremental backups. With rotating backups, a set of tapes is used in rotation; five tapes, for example, one written every Monday, one every Tuesday, and so on. Because each tape is written only once a week, if you delete a file by mistake on Tuesday, for example, the file is still on the Monday tape until the following Monday. You then have nearly a week to realize that it’s gone and get it back. The number of tapes you use depends on your budget and your paranoia. Some people use as few as two, some as many as seven. (We use eight, one for each day of the week and an extra to rotate off-site.)
With incremental backups, you back up only what has changed. Generally, you do a full backup of everything on the disk at infrequent intervals — once a month, for example. This process may take five or six tapes, but because you do it only once a month, it’s not that bad of a job. For the daily incremental backup, you back up only what has changed since the last full backup (it should fit on a single tape). Any given file then is either on the full backup (if it hasn’t changed in a long time) or on the current backup, so you have only two places to look.If you have a large tape budget, you may want to have two sets of tapes you use alternately for full backups, in case your system fails while it is writing the full backups. Sometimes full backups are stored off-site in a bank vault or other safe place, but a trade-off exists: the security of the vault versus the inconvenience of going to the bank when you need to recover a file. If you’re really careful, you can have two full backups: one off-site and one on-site.Your system administrator should handle all this, of course. It’s useful to understand at least the rudiments of backup theory, however, so that when the administrator hands you several different tapes that may contain your file, you understand why.