Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3401295
  • 博文数量: 534
  • 博客积分: 11595
  • 博客等级: 上将
  • 技术积分: 5785
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-22 17:00
文章分类

全部博文(534)

文章存档

2015年(4)

2014年(27)

2013年(15)

2012年(38)

2011年(36)

2010年(85)

2009年(63)

2008年(142)

2007年(124)

分类: LINUX

2008-12-11 18:26:35

tar打包与bzip2配合使用
tar -cvf - MPlayer-1.0rc2 | bzip2 - > MPlayer-1.0rc2.tar.bz2
 
cd ${OUTDIR}

find . -type f -print0 |xargs -0 tar --remove-files -zcf - |tee ${BACKUPFILE}|ssh  -o PreferredAuthentications=publickey "${DESTHOST}" "(cd ${INDIR};tar -zxf -)"

cd -


----------------------------------------------------------

$ tar cvjf - * | ssh user@remote "(cd /desired/path; tar xjf -)"


----------------------------------------------------------------------------

# tar cvzf - /wwwdata | ssh root@192.168.1.201 "dd of=/backup/wwwdata.tar.gz"

# tar cvzf - /wwwdata | ssh root@192.168.1.201 "cat > /dev/nst0"

# tar cvzf - /wwwdata | ssh root@192.168.1.201 $(mt -f /dev/nst0 rewind; cat > /dev/nst0)$


# cd /

# ssh root@192.168.1.201 "cat /backup/wwwdata.tar.gz" | tar zxvf -



-----------------------------------------------------------------------------
tar的用法from tar --help   - [

[/url]

]

Tag:

[url=http://wuhaixing.blogbus.com/s5339/]
linux_Xray




GNU `tar' saves many files together into a single tape or disk archive, and can restore individual files from the archive.

GNU 'tar'是一个打包程序,它可以把很多文件输入到一个管道或磁盘存储文件中,也可从包文件中还原出单个的文件。

Usage: tar [OPTION]... [FILE]...

用法:tar [命令选项]... [文件]...

Examples:

  tar -cf archive.tar foo bar  # Create archive.tar from files foo and bar.

  tar -tvf archive.tar         # List all files in archive.tar verbosely.

  tar -xf archive.tar          # Extract all files from archive.tar.

例子:

   tar -cf archive.tar foo bar #将foo和bar打包到archive.tar包中。

   tar -tvf archive.tar        #列出archive.tar包中文件的详细信息

   tar -xf archive.tar         #释放出archive.tar中的所有文件

If a long option shows an argument as mandatory, then it is mandatory

for the equivalent short option also.  Similarly for optional arguments.

大多数命令选项有长和短两种形式,无论是必须的还是可选的选项,长短两种形式是一样的。

命令选项主要可以控制如下内容:

主要的操作模式、操作修正、文件属性的处理、设备的选择和切换、设备块、包文件格式的选择、本地文件的选择、提示信息的输出

Main operation mode:

  -t, --list              list the contents of an archive

  -x, --extract, --get    extract files from an archive

  -c, --create            create a new archive

  -d, --diff, --compare   find differences between archive and file system

  -r, --append            append files to the end of an archive

  -u, --update            only append files newer than copy in archive

  -A, --catenate          append tar files to an archive

      --concatenate       same as -A

      --delete            delete from the archive (not on mag tapes!)

主要的操作模式:

  -t, --list              列出包文件中的内容

  -x, --extract, --get    从包中释放文件

  -c, --create            创建一个新的包

  -d, --diff, --compare   找出包和文件系统的不同

  -r, --append            将文件追加到包文件的最后

  -u, --update            仅把有变化的文件重新加入到包中

  -A, --catenate          把tar文件加入到包中

      --concatenate       同 -A

      --delete            从包中删除 (not on mag tapes!)  

Operation modifiers:

  -W, --verify               attempt to verify the archive after writing it

      --remove-files         remove files after adding them to the archive

  -k, --keep-old-files       don't replace existing files when extracting

      --overwrite            overwrite existing files when extracting

      --overwrite-dir        overwrite directory metadata when extracting

  -U, --unlink-first         remove each file prior to extracting over it

      --recursive-unlink     empty hierarchies prior to extracting directory

  -S, --sparse               handle sparse files efficiently

  -O, --to-stdout            extract files to standard output

  -G, --incremental          handle old GNU-format incremental backup

  -g, --listed-incremental=FILE

                             handle new GNU-format incremental backup

      --ignore-failed-read   do not exit with nonzero on unreadable files

操作修正选项:

  -W, --verify               对包文件进行写操作后尝试验证

      --remove-files         把文件加入到包中后从磁盘中删除

  -k, --keep-old-files       释放的时候不要覆盖掉已存在的文件

      --overwrite            释放的时候覆盖掉已存在的文件

      --overwrite-dir        释放的时候覆盖掉路径的元数据

  -U, --unlink-first         释放文件前先删除原有文件

      --recursive-unlink     释放路径之前先清空原路径

  -S, --sparse               handle sparse files efficiently

  -O, --to-stdout            将文件释放到标准输出

  -G, --incremental          处理旧的GNU-format 增量备份

  -g, --listed-incremental=FILE

                             处理新的GNU-format 增量备份

      --ignore-failed-read   do not exit with nonzero on unreadable files

Handling of file attributes:

      --owner=NAME             force NAME as owner for added files

      --group=NAME             force NAME as group for added files

      --mode=CHANGES           force (symbolic) mode CHANGES for added files

      --atime-preserve         don't change access times on dumped files

  -m, --modification-time      don't extract file modified time

      --same-owner             try extracting files with the same ownership

      --no-same-owner          extract files as yourself

      --numeric-owner          always use numbers for user/group names

  -p, --same-permissions       extract permissions information

      --no-same-permissions    do not extract permissions information

      --preserve-permissions   same as -p

  -s, --same-order             sort names to extract to match archive

      --preserve-order         same as -s

      --preserve               same as both -p and -s

对文件属性的处理:

      --owner=NAME             强制指定 NAME 作为所添加的文件的所有者

      --group=NAME             强制指定 NAME 作为所添加的文件的组

      --mode=CHANGES           强制指定 (symbolic) 所添加文件的权限模式为 CHANGES

      --atime-preserve         不要改变释放(dumped)文件的访问时间

  -m, --modification-time      不要释放文件的修改时间

      --same-owner             尝试以文件原有的所有者信息释放它

      --no-same-owner          将文件作为你的文件释放

      --numeric-owner          always use numbers for user/group names

  -p, --same-permissions       extract permissions information

      --no-same-permissions    do not extract permissions information

      --preserve-permissions   same as -p

  -s, --same-order             sort names to extract to match archive

      --preserve-order         same as -s

      --preserve               same as both -p and -s

Device selection and switching:

  -f, --file=ARCHIVE             use archive file or device ARCHIVE

      --force-local              archive file is local even if has a colon

      --rsh-command=COMMAND      use remote COMMAND instead of rsh

  -[0-7][lmh]                    specify drive and density

  -M, --multi-volume             create/list/extract multi-volume archive

  -L, --tape-length=NUM          change tape after writing NUM x 1024 bytes

  -F, --info-script=FILE         run script at end of each tape (implies -M)

      --new-volume-script=FILE   same as -F FILE

      --volno-file=FILE          use/update the volume number in FILE

Device blocking:

  -b, --blocking-factor=BLOCKS   BLOCKS x 512 bytes per record

      --record-size=SIZE         SIZE bytes per record, multiple of 512

  -i, --ignore-zeros             ignore zeroed blocks in archive (means EOF)

  -B, --read-full-records        reblock as we read (for 4.2BSD pipes)

Archive format selection:

  -V, --label=NAME                   create archive with volume name NAME

              PATTERN                at list/extract time, a globbing PATTERN

  -o, --old-archive, --portability   write a V7 format archive

      --posix                        write a POSIX format archive

  -j, --bzip2                        filter the archive through bzip2

  -z, --gzip, --ungzip               filter the archive through gzip

  -Z, --compress, --uncompress       filter the archive through compress

      --use-compress-program=PROG    filter through PROG (must accept -d)

Local file selection:

  -C, --directory=DIR          change to directory DIR

  -T, --files-from=NAME        get names to extract or create from file NAME

      --null                   -T reads null-terminated names, disable -C

      --exclude=PATTERN        exclude files, given as a PATTERN

  -X, --exclude-from=FILE      exclude patterns listed in FILE

      --anchored               exclude patterns match file name start (default)

      --no-anchored            exclude patterns match after any /

      --ignore-case            exclusion ignores case

      --no-ignore-case         exclusion is case sensitive (default)

      --wildcards              exclude patterns use wildcards (default)

      --no-wildcards           exclude patterns are plain strings

      --wildcards-match-slash  exclude pattern wildcards match '/' (default)

      --no-wildcards-match-slash exclude pattern wildcards do not match '/'

  -P, --absolute-names         don't strip leading `/'s from file names

  -h, --dereference            dump instead the files symlinks point to

      --no-recursion           avoid descending automatically in directories

  -l, --one-file-system        stay in local file system when creating archive

  -K, --starting-file=NAME     begin at file NAME in the archive

  -N, --newer=DATE             only store files newer than DATE

      --newer-mtime=DATE       compare date and time when data changed only

      --after-date=DATE        same as -N

      --backup[=CONTROL]       backup before removal, choose version control

      --suffix=SUFFIX          backup before removal, override usual suffix

Informative output:

      --help            print this help, then exit

      --version         print tar program version number, then exit

  -v, --verbose         verbosely list files processed

      --checkpoint      print directory names while reading the archive

      --totals          print total bytes written while creating archive

  -R, --block-number    show block number within archive with each message

  -w, --interactive     ask for confirmation for every action

      --confirmation    same as -w

The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.

The version control may be set with --backup or VERSION_CONTROL, values are:

  t, numbered     make numbered backups

  nil, existing   numbered if numbered backups exist, simple otherwise

  never, simple   always make simple backups

GNU tar cannot read nor produce `--posix' archives.  If POSIXLY_CORRECT

is set in the environment, GNU extensions are disallowed with `--posix'.

Support for POSIX is only partially implemented, don't count on it yet.

ARCHIVE may be FILE, HOST:FILE or

[email=USER@HOST:FILE]USER@HOST:FILE[/email]

; DATE may be a textual date

or a file name starting with `/' or `.', in which case the file's date is used.

*This* `tar' defaults to `-f- -b20'.





本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/45990/showart_426168.html





--------------------------------------------------------------------


三、更多参数
以三大常见操作为序说明。
1、备份
引用
-d, --diff, --compare
 找出归档文件和文件系统的不同之处
-r, --append
 将文件附加到归档文件之后
-u, --update
 只附加比归档中新的文件
-A, --catenate
 将 tar 文件附加到归档文件之后
--concatenate
 与 -A 相同
--delete
 从归档文件中删除 (不能在磁带上!)
-N date, --newer=date, --after-date=date
 只将比指定日期更新的文件保存到备份文件
--exclude=pattern
 排除符合查找模式的文件
-P, --absolute-names
 备份文件中的文件名使用绝对路径,而不移除文件名前的“/”,默认是相对路径的
-l, --one-file-system
 复制文件或目录存放的文件系统必须和tar命令当前执行的文件系统相同,否则不执行;也就是讲,不处理其他分区的文件。(可忽略mount挂载分区的问题
--mode=permissions
 备份时,把加入备份文件中的文件属性修改为指定的属性,格式和chmod命令接受的格式相同
--group=group
 备份时,把加入备份文件中的文件所属组设定成指定的组
--owner=owner
 备份时,把把加入备份文件中的文件所有者设定成指定的用户
--numeric-owner
 以UID和GID代替用户名和组名
--recursion
 递归模式(默认)
--no-recursion
 不进行递归处理,即不把指定目录下的所有文件和文件夹都备份
--newer-mtime=date
 Add only files whose contents have changed since date to the archive.
 仅增加指定日期后修改过内容的文件到备份文件中
--anchored
 排除时,排除匹配式中匹配整个文件路径(默认)
--no-anchored
 排除时,排除匹配式中可匹配“/”后的路径
--ignore-case
 排除时,排除匹配式中忽略大小写
--no-ignore-case
 排除时,排除匹配式中大小写敏感(默认)
--wildcards
 排除匹配式支持通配符(默认)
--no-wildcards
 排除匹配式不支持通配符
--wildcards-match-slash
 排除匹配式的通配符匹配“/”(默认)
--no-wildcards-match-slash
 排除匹配式的通配符不匹配“/”
-X file, --exclude-from file
 指定样式文件,供程序执行时剔除符合条件的文件
--remove-files
 删除已经加入到备份文件中的文件

举例:
创建当前目录的全备份到backup.tar文件
(使用find的原因是,这样可以打包所有隐含文件或不符合命令规范的文件和目录)
tar cvf - `find . -print` > backup.tar

比2005年6月1日新的文件才会备份
tar -N '2005/06/01' -zcvf home.tar.gz /home

除/home/dmtsai目录外,/home和/etc目录的所有东西都打包
tar --exclude /home/dmtsai -zcvf myfile.tar.gz /home/* /etc

删除etc.tar文件中的etc/pbm2ppa.conf文件
tar --delete etc/pbm2ppa.conf -vf etc.tar

把tmp.tar文件附加到etc.tar后面
tar -A tmp.tar -vf etc.tar

根据-T参数后的样式文件list.txt,打包/etc中指定的文件,同样的,list.tx默认也是相对路径的
find . -name "*.d" -o -name "*.conf" > list.txt
tar -T list.txt -czvf etc.tar.gz /etc

通过管道,使用一条命令实现上面相同的功能
find . -name "*.d" -o -name "*.conf" | tar -czvf etc.tar.gz -T -


※值得注意几个地方
引用
1)-f后面必须紧跟备份文件,xxx.tar或者xxx.tar.gz
2)使用--delete、-u、-r、-A的时候,后面跟的备份文件都必须为未压缩的,若备份文件已经压缩,需要使用gunzip先解压,操作完后再gzip;
也就是说,不能对tar.gz、tar.bz2等文件直接操作的;
3)备份打包时,默认为相对路径;而排除匹配式默认也是全路径匹配的
所以,在打包时,会提示:“tar: Removing leading `/' from member names”
除非你使用-P参数或特定的参数指定
阅读(1382) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~