-->
Previous Table of Contents Next


Another way to create the tar file is to start from data’s parent directory and specify the directory name as the thing to archive. Here’s the command sequence:


$ pwd

/home/dave

$ tar cvf data.tar data

This also creates an archive of the data directory, but it puts the directory entry as the first thing in the archive. This way, when the tar file is extracted, the first thing that’s created is the directory data, and all the files in data are placed in the data subdirectory.


NOTE:  If you want to create a tar file of all the files in the directory, it’s a good idea to specify a different location for the tar file (other than the current directory). That way, if you try to archive all the files in the current directory, tar won’t get confused and try to add its tar file recursively to the tar that it’s creating.

Using cpio

cpio is a general-purpose command for copying file archives. You can use it to create backups by using the -o option, or to restore files by using the -i option. It takes its input from standard input and sends its output to standard output.

The advantages of cpio include the following:

  It can back up any set of files.
  It can back up special files.
  It stores information more efficiently than tar.
  It skips bad sectors or bad blocks when restoring data.
  Its backups can be restored on almost any Linux or UNIX system.

Some people find cpio’s syntax to be a bit more confusing than tar’s syntax. Also, to perform incremental backups, you have to do some shell programming.

Table 11.2 lists the commonly used options for cpio. See cpio’s man page for a complete description of the options you can use with this command.

Table 11.2 Commonly Used Options for cpio

Option Description

-o Copy out. Creates an archive on standard out.
-B Blocks input or output at 5,120 bytes per record; useful for efficient storage on magnetic tape.
-i Copy in. Extracts files from standard input. This is typically used when the standard input is the result of a copy out action of another cpio command.
-t Creates a table of contents of the input.

The following list provides some examples of using cpio to back up and restore files:

  The following command copies the files in the directory /home to the device /dev/fd0:

    ls /home | cpio -o > /dev/fd0

  The following command extracts the files on the device /dev/fd0 and creates an index in the bkup.indx file:

    cpio -it < /dev/fd0 > bkup.indx

  The following example uses the find command to create a list of all files in /home that have been modified in the last day:

    find /home -mtime 1 -type f -print | cpio -oB > /dev/fd0


The output of that command is piped to cpio, which creates an archive on /dev/fd0, where the data is stored at 5,120 bytes per record.
  The following command restores the file /home/dave/notes.txt from the device /dev/fd0:

   echo "/home/dave/notes.txt" | cpio -i < /dev/fd0


NOTE:  You must give the complete filename to restore a file with cpio.


TIP:  You can automate any of these commands by putting them in root’s crontab file. For example, you could put the following entry in the root’s cron file to perform a daily backup of /home at 1:30 a.m.:

30 01 * * * ls /home | cpio -o > /dev/fd0

If you need to do more complicated backups, you can create shell scripts to control your backups. You also can run these shell scripts via cron.


From Here…

You can find more information about system administration in the following chapters:

  Chapter 7, “Understanding System Administration,” gives an overview of the duties of a systems administrator.
  Chapter 10, “Managing User Accounts,” shows how to create and manage user access to your Linux system.
  Chapter 14, “Managing File Systems,” discusses the ins and outs of file systems and the various issues that you need to consider.


Previous Table of Contents Next