tar命令使用及tar实现全备份和增量备份[转]

本文转载这里,首先弄清楚何为全备、增量备份、差异备份。简而言之,可以这样理解:

  • 全备:对所有的文件做一次备份。
  • 增量备份:本次和上一次的的差异。
  • 差异备份:在全备的基础上做备份。

一、tar命令

#创建测试文件夹和文件  
[root@serv01 web]# cd /data  
[root@serv01 data]# ls  
[root@serv01 data]# mkdir /work  
[root@serv01 data]# mkdir /work/upload  
[root@serv01 data]# cd /work/upload/  
[root@serv01 upload]# touchaa0{1,2,3,4,5}.txt  
[root@serv01 upload]# ls  
aa01.txt aa02.txt  aa03.txt  aa04.txt aa05.txt  

#创建压缩包  
[root@serv01 data]# tar -cvf upload01.tar/work/upload/  
tar: Removing leading `/' from member names  
/work/upload/  
/work/upload/aa03.txt  
/work/upload/aa01.txt  
/work/upload/aa04.txt  
/work/upload/aa05.txt  
/work/upload/aa02.txt  
#查看压缩包里面包含的文件  
[root@serv01 data]# tar -tf upload01.tar  
work/upload/  
work/upload/aa03.txt  
work/upload/aa01.txt  
work/upload/aa04.txt  
work/upload/aa05.txt  
work/upload/aa02.txt  
#删除掉数据,模拟数据丢失  
[root@serv01 data]# rm -rf /work  
#解压  
[root@serv01 data]# tar -xvf upload01.tar-C /  
work/upload/  
work/upload/aa03.txt  
work/upload/aa01.txt  
work/upload/aa04.txt  
work/upload/aa05.txt  
work/upload/aa02.txt  
[root@serv01 data]# ls /work  
upload  
[root@serv01 data]# ls  
upload01.tar  
#压缩时包含文件路径  
[root@serv01 data]# tar -cPvf upload02.tar/work/upload/  
/work/upload/  
/work/upload/aa03.txt  
/work/upload/aa01.txt  
/work/upload/aa04.txt  
/work/upload/aa05.txt  
/work/upload/aa02.txt  
#解压时就不需要指定文件路径了  
[root@serv01 data]# tar -xPvf upload02.tar  
/work/upload/  
/work/upload/aa03.txt  
/work/upload/aa01.txt  
/work/upload/aa04.txt  
/work/upload/aa05.txt  
/work/upload/aa02.txt  
[root@serv01 data]# ls /work/  
upload  

#不一定需要f参数,可以使用重定向解决  
[root@serv01 data]# tar -cv upload03.tar/work/upload/  
tar: upload03.tar: Cannot stat: No suchfile or directory  
tar: Removing leading `/' from member names  
/work/upload/  
/work/upload/aa03.txt  
/work/upload/aa01.txt  
/work/upload/aa04.txt  
/work/upload/aa05.txt  
/work/upload/aa02.txt  
work/upload/00007550000000000000000000000000121766767060120445ustar  rootrootwork/upload/aa03.txt00006440000000000000000000000000121766767060133170ustar rootrootwork/upload/aa01.txt00006440000000000000000000000000121766767060133150ustar  rootrootwork/upload/aa04.txt00006440000000000000000000000000121766767060133200ustar  rootrootwork/upload/aa05.txt00006440000000000000000000000000121766767060133210ustar rootrootwork/upload/aa02.txt00006440000000000000000000000000121766767060133160ustar  rootroottar: Exiting with failurestatus due to previous errors  
#指定路径,重定向到upload03.tar  
[root@serv01 data]# tar -cv /work/upload/> upload03.tar  
tar: Removing leading `/' from member names  
/work/upload/  
/work/upload/aa03.txt  
/work/upload/aa01.txt  
/work/upload/aa04.txt  
/work/upload/aa05.txt  
/work/upload/aa02.txt  
#删除目录  
[root@serv01 data]# rm -rf /work/  
#解压,指定输入源  
[root@serv01 data]# tar -xv -C /  file1  
#查看文件,可以看到所有的配置文件列表都已经存在了(注意是文件路径)  
[root@serv01 data]# vim file1  
#创建conf01.tar,然后T指定文件  
[root@serv01 data]# tar -cPvf conf01.tar -Tfile1  
[root@serv01 data]# tar -tf conf01.tar  
[root@serv01 data]# ls -h conf01.tar -l  
-rw-r--r—. 1 root root 250K Aug  2 18:18 conf01.tar

 

二、tar命令实现——全备和增量备份

[root@serv01 data]# ls /work/upload/  
aa01.txt aa02.txt  aa03.txt  aa04.txt aa05.txt  aa06.txt  
[root@serv01 data]# rm -rf upload01.tar  
#这是全备,无法实现增量备份功能  
[root@serv01 data]# tar -cPvf upload01.tar/work/upload/  
#g指定标志文件  
[root@serv01 data]# tar -g flag -cPvfupload01.tar /work/upload/  
tar: /work/upload: Directory is new  
/work/upload/  
/work/upload/aa01.txt  
/work/upload/aa02.txt  
/work/upload/aa03.txt  
/work/upload/aa04.txt  
/work/upload/aa05.txt  
/work/upload/aa06.txt  
[root@serv01 data]# vim flag  
#新建文件  
[root@serv01 data]# touch /work/upload/aa07.txt  
#更改文件  
[root@serv01 data]# vim/work/upload/aa02.txt  
[root@serv01 data]# tar -g flag -cPvfupload02.tar /work/upload/  
/work/upload/  
/work/upload/aa02.txt  
/work/upload/aa07.txt  
[root@serv01 data]# tar -tPf upload02.tar  
/work/upload/  
/work/upload/aa02.txt  
/work/upload/aa07.txt  

[root@serv01 data]# touch/work/upload/aa08.txt  
[root@serv01 data]# rm -rf/work/upload/aa01.txt  
[root@serv01 data]# ls /work/upload/  
aa02.txt aa03.txt  aa04.txt  aa05.txt aa06.txt  aa07.txt  aa08.txt  
[root@serv01 data]# tar -g flag -cPvfupload03.tar /work/upload/  
/work/upload/  
/work/upload/aa08.txt  

#恢复全备,逐次恢复  
#数据丢失  
[root@serv01 data]# rm -rf /work/*  
[root@serv01 data]# ls /work/  
#先恢复全备  
[root@serv01 data]# tar -g flag -xPvfupload01.tar  
/work/upload/  
/work/upload/aa01.txt  
/work/upload/aa02.txt  
/work/upload/aa03.txt  
/work/upload/aa04.txt  
/work/upload/aa05.txt  
/work/upload/aa06.txt  
#可以看到数据回来了  
[root@serv01 data]# ls /work/upload/  
aa01.txt aa02.txt  aa03.txt  aa04.txt aa05.txt  aa06.txt  
#查看aa02.txt文件,发现内容没有  
[root@serv01 data]# cat/work/upload/aa02.txt  
#恢复文件,从增量备份upload02.tar文件中来  
[root@serv01 data]# tar -g flag -xPvfupload02.tar  
/work/upload/  
/work/upload/aa02.txt  
/work/upload/aa07.txt  
#查看aa02.txt文件,内容回来了  
[root@serv01 data]# cat/work/upload/aa02.txt  
hello world  
#恢复文件,从增量备份upload03.tar文件中来  
[root@serv01 data]# tar -g flag -xPvfupload03.tar  
/work/upload/  
tar: Deleting `/work/upload/aa01.txt'  
/work/upload/aa08.txt  
[root@serv01 data]# ls /work/upload/  
aa02.txt aa03.txt  aa04.txt  aa05.txt aa06.txt  aa07.txt  aa08.txt

 

三、附带一个备份脚本

这个脚本来自这里,linux系统下tar可以实现对文件的备份与恢复,以下shell是完全+增量方式备份

结合crontab,实现周1到周6每天两次增量备份,周日上午增量,下午为完全备份。
2 6,13 * * * /root/backupweb.sh
#!/bin/sh 
# 在crontab里设置,每周日完全备份,每天两次增量备份。 
# 2010-5-26 by lgq 

dayofweek=`date "+%u%H"` 

today=`date "+%Y%m%d%H%M"` 

source=/home/web/ 

backup=/home/web_bak/webtar/ 

cd $backup 

if [ $dayofweek -eq 018 ]; then 
                if [ ! -f "full$today.tar.gz" ];then 
                                rm -rf snapshot 
                                tar -g snapshot -zcf "full$today.tar.gz" $source 
                fi 
else 
                if [ ! -f "inc$today.tar.gz" ];then 
                                tar -g snapshot -zcf "inc$today.tar.gz" $source 
                fi 
fi

 

发布者

发表评论

电子邮件地址不会被公开。 必填项已用*标注

评论审核已启用。您的评论可能需要一段时间后才能被显示。