2024-03-04
cpio operates by reading a list of files and directories (typically provided via find) and either creating an archive file (using the -o option for output) or extracting an archive file (using the -i option for input). Its strength lies in its flexibility; you can specify precisely which files and directories to include, the archive format, and the destination.
Let’s start with creating a backup archive of important files in the /etc directory. The following command uses the -o option (for output) with the -c (create) and -v (verbose) options to create a POSIX-compliant archive named etc_backup.cpio:
find /etc -print0 | cpio -ovc > etc_backup.cpioThis command uses find to locate all files and directories within /etc, separating the filenames with a null character (-print0), a critical step to handle filenames containing spaces or special characters. cpio then processes this list, creating the archive in the current directory. The -v option provides a detailed output of files being added.
For a more advanced example, let’s create a backup of specific directories, excluding some files:
find /etc/ -type f \( -name "*.conf" -o -name "*.txt" \) -print0 | cpio -ovc > config_backup.cpioThis command backs up only the .conf and .txt files within /etc.
You can also specify the archive format:
find /var/log -print0 | cpio -ovB > log_backup.cpio # Creates a binary archiveThe -B option creates a binary archive, often resulting in smaller file sizes compared to the default ASCII format.
To extract the archive, use the -i option (for input):
cpio -ivc < etc_backup.cpioThis command extracts the contents of etc_backup.cpio into the current directory. The -v option (verbose) shows the files being extracted. You can specify a target directory using -d:
cpio -ivcd /tmp/restored < etc_backup.cpioThis command extracts the archive to /tmp/restored. The -d option creates missing directories automatically during extraction.
cpio offers powerful filtering options using patterns. To extract only specific files:
cpio -ivc --pattern "*.conf" < config_backup.cpioThis will only extract files ending in .conf.
cpio works seamlessly with other Linux commands. For instance, you can compress the created archive using gzip:
find /etc -print0 | cpio -ovc | gzip > etc_backup.cpio.gzAnd decompress and extract using:
gzip -dc etc_backup.cpio.gz | cpio -ivcThis showcases how cpio can be incorporated into a backup and recovery strategy, providing flexibility and control over the process. Remember to always test your backup and recovery procedures in a non-production environment before implementing them in a critical system.