Packaging command: Tar
Gzip and Bzip2 can also be used to compress directories. However, the two commands compress directories by compressing all files in the directories 』!
Package Multiple files or directories into a large file, "Packaging command 』
Tar
[[Email protected] ~] # Tar [-j |-Z] [CV] [-f created file name] filename... <= package and compression [[email protected] ~] # Tar [-j |-Z] [TV] [-f created file name] <= View File name [[email protected] ~] # Tar [-j |-Z] [XV] [-F file name] [-C Directory] <= decompression option and parameter:-C: Create a packaging file, you can check the file name (filename)-T packaged in the process with-V: Check the file name contained in the package, and check the file name; -X: unpacks or decompresses data. It can be used with-C (uppercase) to unpacks data in a specific directory. Note that-C,-t, -X cannot appear in a string of command columns at the same time. -J: Use Bzip2 to compress/decompress the file. In this case, the file name is * .tar.bz2-Z. Use gzip to compress/decompress the file: at this time, the file name should be * .tar.gz-V: During the compression/decompression process, the file name being processed should be displayed! -F filename: name of the file to be processed immediately after-F! -F is recommended to write an option separately! -C Directory: This option is used for decompression. to decompress the package in a specific directory, you can use this option. Other options will be used in subsequent exercises:-P: Retain the original permissions and attributes of the backup data, often used in backup (-C) important configuration files-P: retain the absolute path, that is, to allow the backup data to contain the root directory; -- exclude = file: During the compression process, do not package the file!
- Compression: name of the file or directory to be compressed by tar-JCV-F filename.tar.bz2
- Query: tar-jtv-F filename.tar.bz2
- Decompress: tar-jxv-F filename.tar.bz2-C directory to be decompressed
The filename.tar.bz2 is our own file name. Tar does not generate the file name!
Since "-f filename" is closely related, many articles in the past often write "-jcvf FILENAME". This is correct, however, the order of options can be changed theoretically. Therefore, many readers mistakenly believe that "-jvfc FILENAME" is also acceptable. In fact, this will cause the generated file name to change to C! Because-Fc! Therefore, it is recommended that you separate "-f filename" from other options when learning tar, which is not prone to problems.
- Use tar to add the-J or-Z parameter to back up the/etc/directory
[[Email protected] ~] # Tar-zpcv-F/root/etc.tar.gz/etctar: removing leading '/' from Member names <= pay attention to this warning message/etc /.... omitted in the middle .... /etc/ESD. conf/etc/crontab # is added with the-V option, so the file name in use is displayed on the screen. # If you can go to the first page, you will find the above error message! # The-P option focuses on the meaning of "retaining permissions and attributes of the original file. [[Email protected] ~] # Tar-jpcv-F/root/etc.tar.bz2/etc # The displayed information will be exactly the same as above! [[Email protected] ~] # Ll/root/etc *-RW-r -- 1 Root 8740252 Nov 15 23:07/root/etc.tar.bz2-RW-r -- 1 Root 13010999 Nov 15 23:01/ root/etc.tar.gz [[email protected] ~] # Du-Sm/etc118/etc |
The "-P" option is added to save the permissions and attributes of the original file!
- Check the data content of the TAR file (you can check the file name), and check whether the backup file name has a root directory.
[[Email protected] ~] # Tar-jtv-F/root/etc.tar.bz2 .... omitted above .... -RW-r -- root/root 1016 14:06:20 ETC/dbus-1/session. conf-RW-r -- root/root 153 19:20:54 ETC/ESD. conf-RW-r -- root/root 255 21:13:33 etc/crontab |
If the-V option is added, detailed file permissions/attributes will be listed! If you only want to know the file name, just remove-v. From the above data, we can find a very interesting thing, that is, every file name has no root directory! This is also the warning message "Tar: removing leading '/' from Member names (the '/' starting with the file name is removed)" displayed in the previous exercise!
Why remove the root directory? Mainly for security! The data backed up using TAR may need to be decompressed back for use. The file name recorded in tar (that is, the file name we just viewed using tar-jtvf) that is, the actual file name after decompression. If the root directory is removed, if you unbind the backup data from/tmp, the decompressed file name will be changed to "/tmp/etc/XXX 』. However, "If the root directory is not removed, the extracted file name will be an absolute path, that is, the extracted data will be placed in/etc/xxx !』 In this way, your original data under/etc/will be overwritten by the backup data!
TIPS:
You will say, "since it is a backup of data, there is no problem in restoring it back, right ?』 Imagine that the data you backed up is the old version centos 4.xa year ago. You just want to know what data exists in the past backup content, it is found that the/etc under your new version of centos 5.x is overwritten by the old version of backup data! What should you do now? Therefore, it is safer to remove the root directory. |
|
If you are sure that you want to back up the root directory to the TAR file, you can use the-P (uppercase) option. See the following example for analysis:
Example: Back up the (Root) directory in the file name, and check the Content File Name of the backup file [[email protected] ~] # Tar-jppcv-F/root/etc.and.root.tar.bz2/etc... the intermediate process is omitted... [[email protected] ~] # Tar-JTF/root/etc.and.root.tar.bz2/etc/dbus-1/session. conf/etc/ESD. conf/etc/crontab # This query file name does not include the-V option, so only the file name is available! No detailed attributes/permissions or other parameters. |
- Decompress the backup data and consider the decompression action of a specific directory (-C option Application)
[[Email protected] ~] # Tar-jxv-F/root/etc.tar.bz2 [[email protected] ~] # Ll ...... (Omitted earlier) ...... drwxr-XR-x 105 Root 12288 Nov 11 etc ...... (Omitted later ).... |
At this time, the package file will be decompressed in the local directory!
[[Email protected] ~] # Tar-jxv-F/root/etc.tar.bz2-C/tmp [[email protected] ~] # Ll/tmp .... (Omitted above ).... drwxr-XR-x 105 Root 12288 Nov 11 etc .... (Omitted later ).... |
In this way, you can unbind the file from different directories! I personally think that this-C option must be remembered! Okay. After processing, please remember to delete these two directories!
[[email protected] ~]# rm -rf /root/etc /tmp/etc |
Once again, this "RM-RF" is a very dangerous command! Make sure that you confirm the name of the file to be followed. We want to delete/root/etc and/tmp/etc. Do not delete/etc! The system will die ~ Pai_^
- How to unlock a single file only
We extracted all the contents of the packaged file from the above header! Imagine that if I only want to unbind one of the packaged files, what should I do? It's easy. You just need to use-jtv to find the name of the file you want and unbind it. Let's use the following example to illustrate:
#1. First find the file name, and assume that the shadow file has been unlocked: [[email protected] ~] # Tar-jtv-F/root/etc.tar.bz2 | grep 'shadow'-r -------- root/root 1230 2008-09-29 02:21:20 etc/shadow -- r -------- root/root 622 2008-09-29 02:21:20 ETC/gshadow -- r -------- root/root 636 02:21:25 ETC/gshadow-r -------- root/root 1257 02:21:25 etc/shadow <= This is what we want! # Search for important file names first! The grep is the "retrieve" keyword function! We will explain it in the third article! # Here you have a concept first! That pipeline | match grep to retrieve the meaning of the keyword! #2. unbind the file! Syntax and practice: [[email protected] ~] # Tar-jxv-F package .tar.bz2 file to be released [email protected] ~] # Tar-jxv-F/root/etc.tar.bz2 ETC/shadowetc/shadow [email protected] ~] # Ll etctotal 8-r -------- 1 Root 1257 Sep 29 shadow <== yo! There is only one file! # Interesting! At this time, only one file will be unlocked! However, the key point is that file name! Find the correct file name. # In this example, you cannot write/etc/shadow! Because it is recorded in etc.tar.bz2! |
- How to package a directory but not some files in it
Suppose we want to package important directories such as/etc/root, but we do not want to package files starting with/root/etc, because this file is a backup file that we just created! In addition, assume that the new package file is to be placed as/root/system.tar.bz2. Of course, do not package this file yourself (because this file is placed under/root !), Now we can help you with -- exclude! Exclude is not included! So you can do this:
[[email protected] ~]# tar -jcv -f /root/system.tar.bz2 --exclude=/root/etc* > --exclude=/root/system.tar.bz2 /etc /root |
The above command is a whole column ~ In fact, you can press "Tar-JCV-F/root/system.tar.bz2 -- exclude =/root/etc * -- exclude =/root/system.tar.bz2/etc/root 』, if you want to input two rows, add the backslash (\) at the end and immediately press [enter], you can continue the input in the second row. The method for issuing this command will be carefully described in Chapter 3. Through this -- exclude = "file" action, we can remove several special files or directories from the packaging column to make the packaging operation easier! Pai_^
In addition, in the new tar command, laruence found that the original "-- exclude file" seemed unable to actually run! You can see this option using man tar, but you can only find it using info tar. The option function has changed to the "-- exclude = file" mode! Please pay special attention to this place!
- Only Backup files that are newer than at a certain time
In some cases, you want to back up new files instead of old files! Now -- newer-mtime is important! There are actually two options. One is "-- newer" and the other is "-- newer-mtime". What are the differences between the two options? In the touch introduction in chapter 7, we talked about three different time parameters. When -- newer is used, it indicates that the subsequent date contains "mtime and ctime 』, -- newer-mtime is only mtime! Now you know! ^_^. Let's try it!
#1. First find out the new file than/etc/passwd [[email protected] ~] # Find/etc-newer/etc/passwd .... (process omitted ).... # At this time, it will display a new file name than the mtime of the/etc/passwd file. # The result is different on each host! You can check your host first. It's not the same as laruence! [[Email protected] ~] # Ll/etc/passwd-RW-r -- 1 Root 1945 Sep 29/etc/passwd #2. Okay, use tar for packaging! The date is 2008/09/29 [[email protected] ~] # Tar-JCV-F/root/etc.newer.then.passwd.tar.bz2> -- newer-mtime = "2008/09/29"/etc /*.... (omitted in the middle ).... /etc/smartd. conf <= a file that has been backed up .... (omitted in the middle ).... /etc/yum. repos. d/<= all directories are recorded! Tar:/etc/yum. Repos. d/CentOS-Base.repo: file is unchanged; not dumped # The last line shows "not backed up", that is, not dumped! #3. display the file [[email protected] ~] # Tar-jtv-F/root/etc.newer.then.passwd.tar.bz2 |> grep-V '/$' # Call the end non-/file name in tar.bz2 through this command! That's what we want! |
Now you know that this command is easy to use! You can even record and back up different files ~ This backup will make it easier! As you can imagine, if I have performed a full data backup a month ago, when I want to back up data this month, of course, you can only back up the upgraded files after the last month's backup time! Why? Because the original file has been backed up! Why do I have to do it again? You only need to back up new data. This can reduce the backup capacity!
- Basic name: tarfile, tarball?
It is also worth mentioning that the names of files packaged with tar are different if they are compressed! If it is only packaged, it is "Tar-CV-F file.tar". This file is called tarfile. If there is support for compression, such as "Tar-JCV-F file.tar.bz2", we will call it tarball ?)! This is just a basic title, but many books and the Internet will use this tarball name! So we need to introduce it to you.
In addition to packaging data into files, tar can also package files to some special devices. For example, a tape drive (TAPE) is a common example. The tape drive is a one-time read/write device, so we cannot use commands like CP to copy it! If you want to back up/home,/root,/etc to a tape drive (/dev/st0), you can use: "Tar-CV-F/dev/st0/home/root/etc" is simple and easy! It is common for a tape drive to be used for backup (especially for enterprise applications!
- Special Applications: using pipeline commands and data streams
In tar, there is one of the most special methods: Standard Input/standard output and pipe, package the files to be processed and decompress them to the target directory. For more detailed data about data stream redirection and pipeline commands, we will introduce it to you in Chapter 2 Bash. Let's take a look at an example!
#1. Package the entire/etc directory and unbind it in/tmp [[email protected] ~] # Cd/tmp [[email protected] TMP] # tar-CVF-/etc | tar-xvf-# This action is a bit like CP-r/etc/tmp ~ It is still useful! # Note that the output file is changed to-and the input file is changed to-, and there is another file | Yes ~ # This represents standard output, standard input, and pipeline commands respectively! # In a simple idea, you can think of-as a device (buffer zone) in the memory ). # For more detailed data flow and pipeline commands, go to the bash chapter! |
In the above example, we want to "copy the data under/etc directly to the current path, that is, under/tmp", but it is a bit troublesome to use CP-R, in this way, you can directly package the package. In the command, "-" indicates the packaged file! Because we do not want to make the intermediate file exist, we can copy it in this way!
- Example: System Backup example
There are many important directories on the system that need to be backed up. In fact, we do not recommend that you store the backup data in the/root directory! Assume that you already know the following important directories:
- /Etc/(configuration file)
- /Home/(user's home directory)
- /Var/spool/mail/(system, mail box of all accounts)
- /Var/spool/cron/(all accounts work in the configuration file)
- /Root (Home Directory of the system administrator)
We also know that the relationship between the exercises that we have done in Chapter 8 does not need to be backed up for/home/loop * and the compression files under/root do not need to be backed up, in addition, assume that you want to place the backup data to/backups, and this directory only has the root permission to enter! In addition, the file names for each Backup need to be different, such as using a file name such as a backup-system-20091130.tar.bz2 for processing. So how do you deal with this backup data? (Please take a look at it with your hands first, and check the reference answers below !)
#1. First process the Directory and permissions for storing backup data: [email protected] ~] # Mkdir/backups [[email protected] ~] # Chmod 700/backups [[email protected] ~] # Ll-D/backupsdrwx ------ 2 root Root 4096 Nov 30/backups #2. Suppose today is 2009/11/30, the backup method is as follows: [[email protected] ~] # Tar-JCV-F/backups/backup-system-20091130.tar.bz2> -- exclude =/root /*. bz2 -- exclude =/root /*. GZ -- exclude =/home/loop *>/etc/home/var/spool/mail/var/spool/cron/root .... (process omitted ).... [[email protected] ~] # Ll-H/backups/-RW-r -- 1 Root 8.4 m Nov 30 backup-system-20091130.tar.bz2 |
Chapter 9 file and file system compression and packaging 3. Packaging command: Tar