Linux文件備份和對比

cpio

cpio 命令主要有三種基本模式:“-o”模式指的是 copy-out 模式,就是把數(shù)據(jù)備份到文件庫中;
“-i”模式指的是 copy-in 模式,就是把數(shù)據(jù)從文件庫中恢復(fù);“-p”模式指的是復(fù)制模式,就是不
把數(shù)據(jù)備份到 cpio 庫中,而是直接復(fù)制為其他文件。

[root@localhost ~]# cpio -o[vcB] > [文件|設(shè)備] #備份
選項:
-o:copy-out 模式,備份
-v:顯示備份過程
-c:使用較新的 portable format 存儲方式
-B:設(shè)定輸入輸出塊為 5120bytes,而不是模式的 512butes
[root@localhost ~]# cpio -i[vcdu] < [文件|設(shè)備] #還原
選項:
-i:copy-in 模式,還原
-v:顯示還原過程
-c:使用較新的 portable format 存儲方式
-d:還原時自動新建目錄
-u:自動使用較新的文件覆蓋較舊的文件
[root@localhost ~]# cpio -p 目標目錄
例子:利用 find 命令找到文件,備份
[root@localhost ~]# find /etc -print | cpio -ocvB > /root/etc.cpio
#利用 find 指定要備份/etc/目錄,使用>導(dǎo)出到 etc.cpio 文件
[root@localhost ~]# ll -h etc.cpio 
-rw-r--r--. 1 root root 21M 6 月 5 12:29 etc.cpio
#etc.cpio 文件生成
[root@localhost ~]# cpio -idvcu < /root/etc.cpio
#還原 etc 的備份

-p復(fù)制模式

[root@localhost ~]# cd /tmp/
#進入/tmp 目錄
[root@localhost tmp]# rm -rf *
#刪除/tmp 目錄中所有數(shù)據(jù)
[root@localhost tmp]# mkdir test
#建立備份目錄
[root@localhost tmp]# find /boot/ -print | cpio -p /tmp/test
#備份/boot/目錄到/tmp/test/目錄中
[root@localhost tmp]# ls test/
boot
#在/tmp/test/目錄中備份出了 boot 目錄
diff
[root@localhost ~]# diff 選項 old new
#比較 old 和 new 文件的不同
選項:
-a 將任何文檔當(dāng)做文本文檔處理
-b 忽略空格造成的不同
-B 忽略空白行造成的不同
-I 忽略大小寫造成的不同
-N 當(dāng)比較兩個目錄時,如果某個文件只在一個目錄中,則在另一個目錄中視作空
文件
-r 當(dāng)比較目錄時,遞歸比較子目錄
-u 使用同一的輸出格式
[root@localhost ~]# mkdir test
#建立測試目錄
[root@localhost ~]# cd test
#進入測試目錄
[root@localhost test]# vi old.txt
our 
school 
is 
atguigu
#文件 old.txt,為了一會輸出便于比較,每行分開
[root@localhost test]# vi new.txt 
our 
school 
is 
atguigu
in
Beijing
#文件 new.txt
[root@localhost test]# diff -Naur /root/test/old.txt /root/test/new.txt > txt.patch
#比較兩個文件的不同,同時生成 txt.patch 補丁文件
[root@localhost test]# vi txt.patch
#查看下這個文件
--- /root/test/old.txt 2012-11-23 05:51:14.347954373 +0800
#前一個文件
+++ /root/test/new.txt 2012-11-23 05:50:05.772988210 +0800
#后一個文件
—————————————————————————————
@@ -2,3 +2,5 @@
school
is
atguigu
+in
+beijing

打補丁

[root@localhost test]# patch –pn < 補丁文件
#按照補丁文件進行更新
選項:
-pn n 為數(shù)字。代表按照補丁文件中的路徑,指定更新文件的位置。

“-pn”不好理解,我們說明下。補丁文件是要打入舊文件的,但是你當(dāng)前所在的目錄和補丁文
件中的記錄的目錄是不一定匹配的,所以就需要“-pn”來同步兩個目錄。
比如我當(dāng)前是在“/root/test”目錄中(我要打補丁的舊文件就在當(dāng)前目錄下),補丁文件中記
錄的文件目錄為“/root/test/old.txt”,這時如果寫入“-p1”(在補丁文件目錄中取消一級目錄)
那么補丁文件就會打入“/root/test/root/test/old.txt”文件中,這顯然是不對的。那如果寫入的
是“-p2”(在補丁文件目錄中取消二級目錄)那么補丁文件打入的就是“/root/test/test/old.txt”,
這顯然也不對。如果寫入的是“-p3”(在補丁文件目錄中取消三級目錄)那么補丁文件就是打入的
“/root/test/old.txt”,我們的 old.txt 文件就在這個目錄下,所以就應(yīng)該是“-p3”。
那么我們更新下“old.txt”文件,命令如下:

[root@localhost test]# patch -p3 < txt.patch 
patching file old.txt
#給 old.txt 文件打補丁
[root@localhost test]# cat old.txt 
#查看下 old.txt 的內(nèi)容吧。
our 
school 
is 
atguigu
in
Beijing
#多出來了 in Beijing 兩行
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容