Although the GUI-based tools allow the uninitiated to make quick backups, all of these tools output tar files. The tar files, and the tar program that makes them, are one of the original carryovers from Unix. tar stands for Tape ARchive and refers to backing up data to a magnetic tape backup device. Although tar files are designed for backup, they've also become a standard method of transferring files across the Internet, particularly with regard to source files or other installation programs.
A tar file is simply a collection of files bundled into one. By default, the tar file isn't compressed, although additional software can be used to compress it. tar files aren't very sophisticated compared to modern archive file formats. They're not encrypted, for example, but this can also be one of their advantages.
Linux comes with a couple more backup commands, which you might choose to use. They are cpio and pax. Both aim to improve on tar in various ways, but neither is broadly supported at the moment. Examine their man pages for more details.
Perhaps unsurprisingly, tar files are created at the console using the tar command. Usually, all that's needed is to specify a source directory and a filename, which can be done like so:
tar -cf mybackup.tar /home/knthomas/
This will create a backup called mybackup.tar based on the contents of /home/knthomas/. tar is automatically recursive so, in this example, it will delve into all subdirectories beneath /home/knthomas. The -c command option tells tar you're going to create an archive, and the -f option indicates that the filename for the archive will immediately follow. If you don't use the -f option, tar will send its output to standard output, which means that it will display the contents of the archive on the screen.
If you typed in a command like the preceding example, you would see the message "Removing leading '/' from member names." This means that the folders and files added to the archive will all have the initial forward slash removed from their paths. So, rather than store a file in the archive as:
the file will be stored as:
The difference between the two forms concerns when the files are later extracted from the archive. If the files have the initial slash, tar will write the file to /home/knthomas/Mail/file1. If there's already a file of that name in that location, it will be overwritten. On the other hand, with the leading slash removed, tar will create a new directory wherever you choose to restore the archive. In this example, it will create a new directory called home, and then a directory called knthomas within that, and so on.
Because of the potential of accidentally overwriting data by specifying absolute paths in this way, a better way of backing up a directory is simply to change into its parent and specify it without a full path:
cd /home/ tar -cf mybackup.tar knthomas
When this particular archive is restored, it will simply create a new folder called knthomas wher-ever it's restored.
You can also compress the archive from within tar, although it actually calls in outside help from either bzip2 or gzip, depending on which you specify.
To create a tar archive compressed using bzip2, the following should do the trick:
tar -cjf mybackup.tar.bz2 knthomas
This will create a compressed backup from the directory knthomas. The -j command option passes the output from tar to the bzip2 program, although this is done in the background. Notice the change in the backup filename extension to indicate that this is a bzip2 compressed archive.
The following command will create an archive compressed with the older gzip compression:
tar -czf mybackup.tar.gz knthomas
This uses the -z command option to pass the output to gzip. This time, the filename shows it's a gzip compressed archive, so you can correctly identify it in the future.
Extracting files using tar is as easy as creating them:
tar -xf mybackup.tar
The -x option tells tar to extract the files from the maybackup.tar archive.
Extracting compressed archives is simply a matter of adding the -j or -z option to the -x option:
tar -xjf mybackup.tar.bz2
Technically speaking, tar doesn't require the preceding hyphen before its command options. However, it's a good idea to use it anyway, so you won't forget to use it with other commands in the future.
To view the contents of a tar archive without actually restoring the files, use the -t option:
tar -tf mybackup.tar |less
This example adds a pipe into less at the end, because the listing of files probably will be large and will scroll off the screen. Just add the -j or -z option if the tar archive is also compressed.
In addition, you can add the -v option to all stages of making, extracting, and viewing an archive to see more information (chiefly the files that are being archived or extracted).
Typing -vv provides even more information:
tar -cvvf mybackup.tar knthomas
This will create an archive and also show a complete directory listing as the files and folders are added, including permissions.
Once the tar file has been created, the problem of where to store it arises. As I mentioned earlier, storing backup data on the same hard disk as the data it was created to back up is foolish, since any problem that might affect the hard disk might also affect the archive. You could end up losing both sets of data!
If the archive is less than 700MB, it might be possible to store it on a CD-R or CD-RW. To do this from the command line, first the file must be turned into an ISO image, and then it must be burned.
To turn it into an ISO image, use the mkiso command:
mkiso -i backup.iso mybackup.tar.bz2
You can then burn the ISO image to a CD by using the cdrecord command. Before using this, you must determine which SCSI device number your CD-R/RW drive uses (all CD-R/RW or DVD-R/RW drives are seen as SCSI devices, even if they're not). Issue the following command (as root, since cdrecord must be run as root, no matter what it's doing):
You should find the device numbers listed as three numbers separated by commas. To burn the backup image, all you need to do is enter a command in this format:
cdrecord dev=<dev number> speed=<speed of your drive> mybackup.iso
On a typical system, this will take the form of:
cdrecord dev=0,0,0 speed=24 mybackup.iso
The tar command was designed to back up to a tape drive. If you have one of these installed on your computer, writing data to it is very easy. However, you'll first need to know if your tape drive is supported by SUSE Linux. Most modern tape drives are made available as SCSI devices, even if they aren't actually SCSI. Therefore, you can scan the hardware bus for SCSI devices like so (once again running this command as root):
In the results, look for a line relating to your tape drive and, in particular, a line that begins Device file:. This will tell you where in the /dev/ folder the tape drive has been made available.
Once you have this information, it's simply a matter of adapting the tar commands discussed previously in order to direct the output of the tar command to the drive (the following assumes you've identified your tape drive as /dev/st0):
tar -cf /dev/st0 /home/knthomas
Extracting files is just as easy:
tar -xf /dev/st0
The tape itself can be rewound and erased using the mt command. See its man page for the various command options. Generally, the -f command option is used to specify the device to use (such as /dev/st0 in the previous example), and then a plain English word is used to tell the device what to do. These are listed in the man page.
The most useful commands are those that can rewind a tape and also erase it. To rewind a tape, type the following:
mt -f /dev/st0 rewind
To erase a tape, type this:
mt -f /dev/st0 erase
One particularly useful command that you can use if the tape becomes unreliable is the retension command, which winds the tape to the end and then rewinds it to the beginning:
mt -f /dev/st0 retension