Click. Work. Collect
LinuxEra.com arrow Interview Questions arrow What command you use for cpio to create a backup called backup.cpio of all the users' home dirs?
Friday, 09 January 2009
LinuxEra.com
Linux Articles
Discussion Board
Interview Questions
Site Map
Search
About
Contact
Members Login





Lost Password?
No account yet? Register
Featured Links
PHP.Net
MySQL.com
Apache.Org
Linux.Org
Special Note
Today Linux is used in numerous domains, from embedded systems to supercomputers, and has secured a place in server installations with the popular LAMP application stack. Torvalds continues to direct the development of the kernel. Stallman heads the Free Software Foundation, which in turn develops the GNU components. Finally, individuals and corporations develop third-party non-GNU components.
 
Syndicate

Valid XHTML 1.0 Transitional


What command you use for cpio to create a backup called backup.cpio of all the users' home dirs?
Courtesy: Navdeep Singh  
find /home | cpio -o > backup.cpio

The find command is used to create a list of the files and directories contained in home. This list is then piped to the cpio utility as a list of files to include and the output is saved to a file called backup.cpio.

 

About cpio

Creates and un-creates archived cpio files. And also is capable of copying files to things other then a hard disk.

Syntax

cpio -i [ bBcdfkmPrsStuvV6 ] [ -C bufsize ] [ -E file ] [ -H header ] [ -I file [ -M message ] ] [ -R id ] [pattern ... ]

cpio -o [ aABcLPvV ] [ -C bufsize ] [ -H header ] [ -O file [ -M message ] ]

cpio -p [ adlLmPuvV ] [ -R id ] directory

-i (copy in) cpio -i extracts files from the standard input.
-o (copy out) cpio -o reads the standard input to obtain a list of path names and copies those files onto the standard output.
-p (pass) cpio -p reads the standard input to obtain a list of path names of files.

In conjunction with the above commands the below commands can be used.

-a Reset access times of input files after they have been copied. Access times are not reset for linked files when cpio -pla is specified (mutually exclusive with -m).
-A Append files to an archive. The -A option requires the -O option. Valid only with archives that are files, or that are on floppy diskettes or hard disk partitions.
-b Reverse the order of the bytes within each word. (Use only with the -i option.)
-B Block input/output 5120 bytes to the record. The default buffer size is 512 bytes when this and the -C options are not used. -B does not apply to the pass option; -B is meaningful only with data directed to or from a character special device, for example, /dev/rmt/0m.
-c Read or write header information in ASCII character form for portability. There are no UID or GID restrictions associated with this header format. Use this option between SVR4-based machines, or the -H odc option between unknown machines. The -c option implies the use of expanded device numbers, which are only supported on SVR4-based systems. When transferring files between SunOS 4 or Interactive Unix and the Solaris 2.6 Operating environment or compatible versions use -H odc.
-C bufsize Block input/output bufsize bytes to the record, where bufsize is replaced by a positive integer. The default buffer size is 512 bytes when this and -B options are not used. (-C does not apply to the pass option; -C is meaningful only with data directed to or from a character special device, for example, /dev/rmt/0m.)
-d Creates directories as needed
-E file Specify an input file (file) that contains a list of filenames to be extracted from the archive (one filename per line).
-f Copy in all files except those in patterns. (See OPERANDS for a description of patterns.)
-H header Read or write header information in header format. Always use this option or the -c option when the origin and the destination machines are different types (mutually exclusive with -c and -6). Valid values for header are:

bar bar head and format. Used only with the -i option ( read only)

crc|CRC ASCII header with expanded device numbers and an additional per-file checksum. There are no UID or GID restrictions associated with this header format.

odc ASCII header with small device numbers. This is the IEEE/P1003 Data Interchange
Standard cpio header and format. It has the widest range of portability of any of the header formats. It is the official format for transferring files between POSIX-conforming systems. Use this format to communicate with SunOS 4 and Interactive Unix. This header format allows UIDs and GIDs up to 262143 to be stored in the header.

tar |TAR tar header and format. This header format allows UIDs and GIDs up to 2097151 to be stored in the header.

ustar|USTAR
IEEE/P1003 Data Interchange Standard tar header and format. This header format
allows UIDs and GIDs up to 2097151 to be stored in the header. 

Files with UIDs and GIDs greater than the limit stated above will be archived with the UID and GID of 60001.

 

-I file Read the contents of file as an input archive. If file is a character special device, and the
current medium has been completely read, replace the medium and press RETURN to  continue to the next medium. This option is used only with the -i option.
-k Attempt to skip corrupted file headers and I/O errors that may be encountered. If you want to copy files from a medium that is corrupted or out of sequence, this option lets you read only those files with good headers. (For cpio archives that contain other cpio archives, if an error is encountered cpio may terminate prematurely. cpio will find the next good header, which may be one for a smaller archive, and terminate when the  smaller archive's trailer is encountered.) Used only with the -i option.
-l Whenever possible, link files rather than copying them. (Usable only with the -p option.)
-L Follow symbolic links. The default is not to follow symbolic links.
-m Retain previous file modification time. This option is ineffective on directories that are being copied (mutually exclusive with -a).
-M message Define a message to use when switching media. When you use the -O or -I options and specify a character special device, you can use this option to define the message that is printed when you reach the end of the medium. One %d can be placed in message to print the sequence number of the next medium needed to continue.
- O file Direct the output of cpio to file. If file is a character special device and the current medium is  full, replace the medium and type a carriage return to continue to the next medium. Use only with the -o option.
-P Preserve ACLs. If the option is used for output, ACLs if existed are written along with other attributes to the standard output. ACLs arecreated as special files with a special file type. If the option is used for input, ACLs if existed are extracted along with other attributes from standard input. The option recognizes the special file type. Note that errors will occur if a cpio archive with ACLs is extracted by previous versions of cpio . This option should not be used with the -c option, as ACL support may not be present on all systems, and hence is not portable. Use ASCII headers for portability.
-r Interactively rename files. If the user types a carriage return alone, the file is skipped. If the user types a ``.'' the original pathname will be retained. (Not available with cpio -p.)
-R id Reassign ownership and group information for each file to user ID (ID must be a valid login ID from /etc/passwd). This option is valid only for the super-user.
-s Swap bytes within each half word.
-S Swap halfwords within each word.
-t Print a table of contents of the input. No files are created (mutually exclusive with -V).
-u Copy unconditionally (normally, an older file will not replace a newer file with the same name).
-v Verbose. Print a list of file names. When used with the -t option, the table of contents looks like the output of an ls -l command (see ls)
-V Special verbose. Print a dot for each file read or written. Useful to assure the user that cpio is working without printing out all file names.
directory A path name of an existing directory to be used as the target of cpio -p.
pattern Expressions making use of a pattern-matching notation similar to that used by the shell  for filename pattern matching, and similar to regular expressions. The following metacharacters are defined:

* Matches any string, including the empty string.

? Matches any single character.

[...] Matches any one of the enclosed characters. A pair of characters separated by `-' matches any symbol between the pair (inclusive), as defined by the system default collating sequence. If the first character following the opening `[' is a `!', the results are unspecified. 

! means not. (For example, the !abc* pattern would exclude all files that begin with abc.) 

In patterns, metacharacters ?, *, and [...] match the slash (/) character, and backslash (\) is an escape character. Multiple cases of pattern can be  specified and if no pattern is specified, the default for pattern is * (that is, select all files).

Each pattern must be enclosed in double quotes; otherwise, the name of a file in the current directory might be used.

Examples

find . -print | cpio -ocv > /dev/fd0

Above, using the find command would list all files and directories and using the cpio command copy those files listed to the floppy drive.

find . -print | cpio -dumpv /home/users/hope

In the above example the find command would find all files and directories and using the cpio command copy those files to the hope user account.

cpio -icuvd < /dev/fd0

The above command would restore the files back from the floppy.

 

 
< Prev   Next >
Your Views
What do you think about LinuxEra.com?