linux中cpio文件如何,linux cpio命令的使用
linux cpio命令的使用
cpio -o > [file|device] <==备份
cpio -i < [file|device] <==还原
-o :将数据 copy 输出到档案或装置上
-i :将数据自档案或装置 copy 出来系统当中
-t :查看 cpio 建立的档案或装置的内容
-c :一种较新的 portable format 方式储存
-v :让储存的过程中文件名称可以在屏幕上显示
-B :让预设的 Blocks 可以增加至 5120 bytes ,预设是 512 bytes !
这样的好处是可以让大档案的储存速度加快(请参考 i-nodes 的观念)
-d :自动建立目录!由于 cpio 的内容可能不是在同一个目录内,
如此的话在反备份的过程会有问题! 这个时候加上 -d 的话,
就可以自动的将需要的目录建立起来了!
-u :自动的将较新的档案覆盖较旧的档案!
准备数据
[root@node1 tmp]# ll
total 104
-rw-r--r-- 1 root root 8 Jan 13 12:35 test
-rw-r--r-- 1 root root 3071 Jan 13 12:13 top.bak
-rw-r--r-- 1 root root 21473 Jan 13 12:22 top.bak1
-rw-r--r-- 1 root root 21473 Jan 13 12:22 top.bak2
-rw-r--r-- 1 root root 21473 Jan 13 12:02 top.bak3
-rw-r--r-- 1 root root 21473 Jan 13 12:34 top.txt
[root@node1 tmp]# mkdir abc
[root@node1 tmp]# ll
total 108
drwxr-xr-x 2 root root 4096 Jan 13 12:35 abc
-rw-r--r-- 1 root root 8 Jan 13 12:35 test
-rw-r--r-- 1 root root 3071 Jan 13 12:13 top.bak
-rw-r--r-- 1 root root 21473 Jan 13 12:22 top.bak1
-rw-r--r-- 1 root root 21473 Jan 13 12:22 top.bak2
-rw-r--r-- 1 root root 21473 Jan 13 12:02 top.bak3
-rw-r--r-- 1 root root 21473 Jan 13 12:34 top.txt
[root@node1 tmp]# cd abc
[root@node1 abc]# echo "hello cpio" > abc1.txt
[root@node1 abc]# ll
total 4
-rw-r--r-- 1 root root 11 Jan 13 12:35 abc1.txt
[root@node1 tmp]# pwd
/tmp
[root@node1 tmp]# ll
total 108
drwxr-xr-x 2 root root 4096 Jan 13 12:35 abc
-rw-r--r-- 1 root root 8 Jan 13 12:35 test
-rw-r--r-- 1 root root 3071 Jan 13 12:13 top.bak
-rw-r--r-- 1 root root 21473 Jan 13 12:22 top.bak1
-rw-r--r-- 1 root root 21473 Jan 13 12:22 top.bak2
-rw-r--r-- 1 root root 21473 Jan 13 12:02 top.bak3
-rw-r--r-- 1 root root 21473 Jan 13 12:34 top.txt
1、使用cpio备份
[root@node1 tmp]# cpio -ov top.bak > top.bak.cpio --cpio不支持直接写入文件名,需要使用ls或echo或find或
cpio: Too many arguments
1)使用ls
[root@node1 tmp]# ls top.bak1 top.bak2
top.bak1 top.bak2
[root@node1 tmp]# ls top.bak1 top.bak2 | cpio -ov > top.bak12.cpio
top.bak1
top.bak2
85 blocks
[root@node1 tmp]# cpio -tv < top.bak12.cpio --查看 cpio 建立的档案或装置的内容
-rw-r--r-- 1 root root 21473 Jan 13 12:22 top.bak1
-rw-r--r-- 1 root root 21473 Jan 13 12:22 top.bak2
85 blocks
2)使用echo
[root@node1 tmp]# echo top.bak | cpio -ov > top.bak.rel.cpio --相对路径的备份
top.bak
7 blocks
[root@node1 tmp]# echo /tmp/top.bak | cpio -ov > top.bak.abs.cpio --绝对路径的备份
/tmp/top.bak
7 blocks
[root@node1 tmp]# cpio -tv < top.bak.rel.cpio
-rw-r--r-- 1 root root 3071 Jan 13 12:13 top.bak
7 blocks
[root@node1 tmp]# cpio -tv < top.bak.abs.cpio
cpio: Removing leading `/' from member names
-rw-r--r-- 1 root root 3071 Jan 13 12:13 tmp/top.bak
7 blocks
3)使用find
[root@node1 tmp]# find /tmp -name abc1.txt
/tmp/abc/abc1.txt
[root@node1 tmp]# find /tmp -name abc1.txt -print
/tmp/abc/abc1.txt
[root@node1 tmp]# find /tmp -name abc1.txt | cpio -ov > abc1.txt.cpio
/tmp/abc/abc1.txt
1 block
4)使用
[root@node1 tmp]# cat test
top.txt
[root@node1 tmp]# cpio -ov < test > top.txt.cpio
top.txt
43 blocks
[root@node1 tmp]# cpio -tv < top.txt.cpio
-rw-r--r-- 1 root root 21473 Jan 13 12:34 top.txt
43 blocks
2、使用cpio恢复
[root@node1 dev]# cd /tmp
[root@node1 tmp]# ll
total 164
drwxr-xr-x 2 root root 4096 Jan 13 12:35 abc
-rw-r--r-- 1 root root 512 Jan 13 12:37 abc1.txt.cpio
-rw-r--r-- 1 root root 8 Jan 13 12:35 test
-rw-r--r-- 1 root root 3071 Jan 13 12:13 top.bak
-rw-r--r-- 1 root root 3584 Jan 13 12:36 top.bak.abs.cpio
-rw-r--r-- 1 root root 0 Jan 13 12:36 top.bak.cpio
-rw-r--r-- 1 root root 3584 Jan 13 12:36 top.bak.rel.cpio
-rw-r--r-- 1 root root 21473 Jan 13 12:22 top.bak1
-rw-r--r-- 1 root root 43520 Jan 13 12:36 top.bak12.cpio
-rw-r--r-- 1 root root 21473 Jan 13 12:22 top.bak2
-rw-r--r-- 1 root root 21473 Jan 13 12:02 top.bak3
-rw-r--r-- 1 root root 21473 Jan 13 12:34 top.txt
[root@node1 /]# pwd
/
[root@node1 /]# mkdir tmp2
[root@node1 /]# cd /tmp2
[root@node1 tmp2]# cpio -iv < /tmp/top.bak.rel.cpio
top.bak
7 blocks
[root@node1 tmp2]# ll
total 4
-rw-r--r-- 1 root root 3071 Jan 13 12:38 top.bak
[root@node1 tmp2]# cpio -iv < /tmp/top.bak.abs.cpio
cpio: Removing leading `/' from member names ---------------------注意这里,不使用绝对路径
cpio: tmp/top.bak: No such file or directory
tmp/top.bak
7 blocks
[root@node1 tmp2]# ll
total 4
-rw-r--r-- 1 root root 3071 Jan 13 12:38 top.bak
--由于/tmp2下没有tmp目录,所以报错,未恢复成功
[root@node1 tmp2]# cpio -idv < /tmp/top.bak.abs.cpio --加上-d自动创建目录
cpio: Removing leading `/' from member names
tmp/top.bak
7 blocks
[root@node1 tmp2]# ll
total 8
drwxr-xr-x 2 root root 4096 Jan 13 12:39 tmp
-rw-r--r-- 1 root root 3071 Jan 13 12:38 top.bak
[root@node1 tmp2]# cpio -iv < /tmp/abc1.txt.cpio
cpio: Removing leading `/' from member names
cpio: tmp/abc/abc1.txt: No such file or directory
tmp/abc/abc1.txt
1 block
[root@node1 tmp2]# cpio -idv < /tmp/abc1.txt.cpio
cpio: Removing leading `/' from member names
tmp/abc/abc1.txt
1 block
加上--absolute-filenames参数使用绝对路径来恢复
[root@node1 tmp]# pwd
/tmp
[root@node1 tmp]# rm -f top.bak
[root@node1 tmp2]# pwd
/tmp2
[root@node1 tmp2]# cpio -iv --absolute-filenames < /tmp/top.bak.abs.cpio
/tmp/top.bak
7 blocks
[root@node1 tmp2]# cpio -iv --absolute-filenames < /tmp/top.bak.abs.cpio
cpio: /tmp/top.bak not created: newer or same age version exists
/tmp/top.bak
7 blocks
[root@node1 tmp2]# cpio -uiv --absolute-filenames < /tmp/top.bak.abs.cpio --加上-u参数自动的将较新的档案覆盖较旧的档案
/tmp/top.bak
7 blocks
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26524307/viewspace-1071207/,如需转载,请注明出处,否则将追究法律责任。