Like the TAR command, CPIO is used to backup or archive files and directories.
The difference is that tar by default backup file to a tape device and backup all of a specified directory, whereas cpio by default archive only the files you tell it to using command line arguments. That’s why it’s always (almost) used with other command to do backup/archive.
To start with, backup a directory under /home/acheng/www:
#cd /home/acheng/www
#find -d . | cpio -ovF ../wwwbak.cpio
When using the find
utility with cpio
, it is always a good idea to include either the d
or the depth
switch. This switch prevented permissions from interfering with a backup. When using this switch, either put -d
right after the word find
and before the directory to search (in this case, “.
“), or put the word -depth
after the directory to search, like so:
#find . -depth -print | cpio -ov > backup.cpio
To view the contents of a cpio archive without extracting, use:
# cpio -it < ../wwwbak.cpio
To extract, use:
#mkdir ../restore;cd ../restore
#cpio -ivd < ../wwwbak.cpio
Note the d switch, it tell cpio to create any dir when necessary. otherwise it’ll compain not being able to find the dir to restore to.
You can also use -p switch to let cpio emulate what cp can do with a directory or file, that is to copy them over to a different place.
The benefit of using cpio instead of cp is that you can fine tune the files/dirs you want to copy with utilities like FIND and you can reserve a file’s create time/modified time, plus you can reserve/change a file’s ownership at wish.
Let’s say you want to backup the www dir to /tmp:
#cd /home/acheng/www
#find -d . | cpio -pvd /tmp
Or change the ownership of all copied files to user alan:
#find -d . | cpio -pvd -R alan /tmp
To reserve all original time of these files, use:
#find -d . | cpio -pvdam -R alan /tmp
** use “man cpio” for more information and options with cpio
–end–