Another service that root may be required to perform for users on some systems is the production and maintenance of archives and backups.
The most obvious media for storing backups and the one most readily available is floppy disks. If the amount of material to store is only small then it is a simple task to create a Linux second extended filesystem on a floppy disk, mount the disk into the root filesystem and then just copy the files across. Assuming that you have an unformatted 1.44 Mb floppy disk to start with, the procedure for saving the contents of a given directory (src_dir in the example) along with all its subdirectories and files is:
# fdformat /dev/fd0H1440 # mke2fs /dev/fd0 1440 # mount -t ext2 /dev/fd0 /mnt # cp -a src_dir /mnt # umount/mnt
The five commands in this sequence perform the following functions:
In situations where there are one or more large files included in the material to archive then you may want to try to compress them to save space on the floppy disk.The standard command available with Linux distributions for file compression is the GNU gzip command. The general form of this command is:
gzip [options] file
The specified file will be compressed to generate a new file with the same name as the original, but with the suffix gz. This new file will replace the original. There are lots of options available with gzip, the most useful ones are:
-1 | use fastest compression; output files not as small; |
-9 | use maximum compression; takes longer to run; |
-l | list some statistics about the files being compressed; |
-h | display a help screen of information and options. |
Typically, a large text file will be compressed by 60% to 70%, which can represent an enormous saving in disk space.
Given a file which was compressed by gzip, it can be restored to its original state with the command gunzip.
gunzip file
Once again, the output file will replace the input file. It will also have the gz file name extension removed. A compressed file can also be read while still leaving it compressed at the end by using the command zcat. This command is analogous to the ordinary cat command except that it operates on compressed files. The output from zcat is sent to the standard output device, which makes the command particularly useful for reading compressed text files, as the following example shows:
$ zcat textfile.gz I more
This command pipes an uncompressed version of the file textfile.gz into the more command to give a paginated display of the text the file contains.
Once you get up to a certain size, backing files up into a Linux filesystem starts to become unwieldy. What you really need then is to be able to write whole directories with all their subdirectories and files straight out to a tape drive. One of the commands that can perform this operation is called tar (i.e. Tape ARchiver).
What this command does effectively, is to pack all the files in the directories it is given into a single huge file, called a tarfile, which can then be written to tape.
So far, tar would seem to be of limited use as so few systems support a tape drive of any kind. However, tar is capable of writing its output file to anywhere including onto floppy disk, out to the screen or even into an ordinary file. In addition to this, tar also has a couple of other tricks it can perform. If the tar file is being written to floppy disk but it is too large to fit onto a single disk, then tar can be instructed to split the file over multiple disks (called a multi-volume archive). It is also possible to instruct tar to use the gzip algorithm to compress the tar file before writing the result to its output.
The general format of the tar command is:
tar [options] files
where files is the list of files and directories whose contents will be included in, or extracted from, the archive. The tar command has many options, the most useful are:
-c | create a new tarfile archive; |
-x | extract files from a tarfile archive; |
-t | display the contents of a tarfile archive; |
-f file | specify tarfile name as file; |
-v | get tar to be verbose about what it is doing; |
-l | stay in local filesystem, don't cross mount points; |
-M | create/extract/display a multi-volume archive; |
-N date | only store files newer than date; |
-z | use gzip to compress/uncompress archive. |
By convention, a tar archive filename should be given the suffix .tar and a cormpressed archive filename should have the suffix .tgz (as all the packages probably, had on your Linux distribution disks). A typical tar command to create a compressed archive containing the home directory contents for user rachel would be:
# tar -cvzf /tmp/rachel.tgz /home/rachel
A command to examine the contents of this archive would be:
# tar -tzf /tmp/rachel.tgz
And a command sequence to restore user rachel's home directory from the archive after some of the original files were accidentally deleted:
# cd / # tar -xvzf /tmp/rachel.tgz
The reason for the cd / command in this last example is that tar has stored all the filenames in the archive as names relative to the root directory. So you need to be in the root directory to restore the files to their proper place. If you were to unpack this archive from some other directory, then the files would still be unpacked but in places relative to your new position in the directory hierarchy. Using the tar -t option is a simple way to see relative to which directory the archive files were stored. If it is necessary to allow ordinary users to create archives to floppy disk then there are one or two problems which will need sorting out. To start with, the floppy disk device special files (/dev/fd*) usually have read and write permission turned off for ordinary users. As system administrator it is a simple matter to change the permissions appropriately, using the chmod command. A second problem arises if you want ordinary users to be able to mount floppy disks into the directory hierarchy as the mount and umount commands are root only commands. They are set up in this way to prevent ordinary users from rearranging the directory hierarchy, which could lead to all sorts of security problems. Probably the safest solution to this problem is to write a pair of simple programs in C which can only be used to mount and unmount a floppy disk over a specific directory (say /floppy).