Backing Up and Restoring Using the cpio Command

The cpio command is one of the most commonly used Linux back up tools.

The cpio command has two unusual features

Unlike tar , in which the files to back up are typed in as part of the command, cpio reads the files to work with from the standard input (in other words, the screen).

This feature means that cpio must be used as part of a multiple command or with a redirection pipe. Examples of this usage are shown in the tables below.

cpio must always be used with one of three flags. Flags are options that set the mode in which the command runs. Only one flag can be used at a time, and it must come before any other options. In addition, the choice of flags limits the options that can be used. Each flag also has a gnu option that can used in its place. The gnu option gives a convenient name for each flag: extract, create, and pass- through.

If you want to know more options and how to use check cpio man page

Backing Up using the cpio Command

To do a backup, use cpio with a search command, such as find .

The basic structure is: find -name string -print | cpio -o options > directory .

In this example:

The -name option for find lets you search for a string enclosed in double quotation marks. Metacharacters can be used. The bar character ( | ) redirects the output of find to cpio .

The -o flag sets cpio to create an archive file for backing up.

The target is a directory.

The > redirection operator redirects files to the location for the back up. Typically, this location is on a removable device.

CPIO Command Examples

Examples What it does
1) cd /u/test
2) find . -print | cpio -ocv > /dev/fd0
Reads file names using the find command and copies to the floppy drive (/dev/fd0).
find . -cpio /dev/fd0 -print Saves files in current directory and writes this info to floppy. Same command as above except much faster.
1) cd /u/test
2) cpio -icuvd < /dev/fd0
Restore files and directories saved on the floppy device. These files are restored under the current directory (/u/test) Only if relative pathnames (./<filename>) were used.
cpio -itvcC1 < /dev/rmt0 List the table of contents from a tape device.
1) cd /u/test
2) find . -print | cpio -dumpv /u/jerry
Copies all files FROM one directory TO another WITHOUT changing the permissions, owner/group or modification date of the file. Use the following command to verify that all files were copied:
find /u/test -print | wcfind /u/test1 -print | wc If the number of files encountered is the same for both directories its safe to assume that the directories are identical. NOTE: that the number of blocks allocated to the SOURCE directory (/u/test) may be larger than the DESTINATION directory (/u/test1), since compaction of the directory structure will have occurred at the destination end.
cpio -imv /home/test/.profile < /dev/fd0 Selectively restore the /home/test/.profile file from floppy
cpio -i "*.f" "*.c" </dev/fd0 Selectively restore only the *.f and *.c files from floppy