Back up linux using tar or afio

Source: Internet
Author: User
Article Title: use tar or afio to back up a linux system. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Linux system backup has always been a concern for users who have switched from windows to linux. As we all know, experienced windows users have developed an "Excellent Tradition" for backing up windows systems with software such as ghost. As a result, this tradition has been transplanted to linux. In fact, ghost is also very good for linux backup, and Its compression function can save a lot of hard disk space, which is indeed quite useful for hard disk shortage users. However, it also has its own disadvantage, that is, it must be used in DOS. If you only want to extract some files, you can only decompress them using windows ghost ......, Various factors make using ghost to back up linux a pain point.
  
In fact, the tar and afio of linux can be used very conveniently. The most obvious point is that you do not need to restart the computer to back up the system. Well, let's get down to the truth.
  
1. Use tar for simple backup and recovery
1.1 backup
The tat command can write archive data to a file, tape, or original tape device. The typical syntax of the tar command is as follows:
Code:
Tar-[c | x | t] [-pv]-f device path1 path2...
  
Tar requires a parameter command. Generally, c Indicates creation, x indicates extraction (extract), and t indicates test ). Option p indicates that tar retains the ownership and permissions of the original file during decompression. Option v indicates tar to list detailed operation procedures. F option and subsequent Parameters specify tar to write to the specified device. All parameters such as path1 and path2 are to be added to the directory tree or file in the archive.
  
For example, to write the/usr directory tree to $ HOME/Backup/usrbak.tar, run the following command:
Code:
Tar-cf $ HOME/Backup/usrbak.tar/usr
  
To write the/usr directory tree to the/dev/st0 tape device, run the following command:
Code:
Tar-cf/dev/st0/usr
  
Now, smart users have come up with the following command to back up the entire linux File System:
Code:
Tar-cf/dev/st0/
  
At first glance, this is indeed true, but this command cannot achieve your intention. First, this command also backs up the/proc directory. Based on your kernel version, it will increase the number of megabytes of data that you do not need during the kernel runtime in your backup, and may even cause tar interruption or segment error (segmentation fault ).
Second, back up the root directory/will also back up the/mnt Directory, which will contain the mounted CD-ROM data and any mounted Network File System or even the mounted windows partition (this is a good thing, it may also be a bad thing, depending on what you want to do ).
To back up the root directories except/proc and/mnt, run the following command:
Code:
Tar-cf/dev/st0 $ (ls/| grep-v-e proc-e mnt)
  
This command backs up the root directories except/proc and/mnt.
  
1.2 recovery
After creating a tar archive file, delete the slash "/" before each file name. This means that when files are restored, they will be restored to the relative working directory of tar running. For example, to restore the tapes in/dev/st0 to their original paths, run the following command:
  
Code:
Cd/: tar-xpf/dev/st0
  
  
You can specify an additional path during recovery. These paths indicate the paths to be restored after the files in the list are packaged. Wildcard characters are allowed. For example, to restore the/usr/X11R6 and/usr/local directory trees in the/dev/st0 tape to their original paths, run the following command:
  
Code:
Cd/; tar-xpf/dev/st0 'usr/X11r6/*/''usr/local /*'
  
  
You can also use the tee command to display the processed file list on the terminal and write the file list to a file. The command is as follows:
  
Code:
Cd/; tar-xpf/dev/st0 'usr/X11r6/*/''usr/local/* '| tee/var/log/restored. files
  
  
For more information about using tar for more complex operations, such as multi-volume, Incremental backup, and recovery, see the manual
  
Note:
1. The z and j options of tar are not mentioned here. Tar-cz (j) can indeed create an archive file for compressing tar, but both are based on data streams. That is to say, if the data is stored on the tape, and the probability of a tape error is very high, if you encounter an unrecoverable bit error, you will not only lose this file, but the entire tape will be destroyed.
Therefore, it is best to use only the medium with low error rate, such as the z and j options on the hard disk. This issue has nothing to do with tape devices based on hardware compression. For such devices, software compression is not required. Of course, the z and j options can be added to most netizens. After all, most people do not have private tape drives.
2. The demonstration command lines in this article use/dev/st0 as the backup device. If you want to back up data to a file, you only need to change this parameter to the file path, for example, $ HOME/Backup/sysbak.tar
2. Use afio for simple backup and recovery
  
The afio command is similar to the tar command, and the archive of afio can be operated together with the system that supports cpio (detailed information can be man cpio) command. afio supports compressing a single file, it is more suitable for compressing and backing up tapes without hardware compression.
  
Maybe your release version does not have the afio software package. You can get the source code of afiofrom http://scdbackup.webframe.org/afio-2.4.6.tgz.
  
Because afio archives the file list as a standard input stream, the basic syntax of afio as an archive tool contains two commands:
  
Code:
Find path1 path2... [-opts] | afio-[I | o | t] [-vZ] device
  
-I command is used to restore the tape or archive,-o command (output to) is used to write the tape or archive, and-t command is used to test the tape or archive. The-v option lists the processed files, and the-Z option compresses the files before they are written to the tape.
  
2.1 backup
Generally, the find command is used to provide a list of files to be processed by afio. You can use the special features of find and afio to construct a more professional and flexible file archiving plan.
The simplest way is to write the/usr directory tree to the tape device/dev/st0 by using the following command:
Code:
Find/usr | afio-o/dev/st0
  
To perform the same archive operation but compress each file before writing it to a tape, run the following command:
Code:
Find/usr | afio-o-Z/dev/st0
  
  
2.2 recovery
To restore the archive to the root directory, use the-I command instead of the-o command:
Code:
Cd/; afio-I/dev/st0
  
To restore the compressed document, remember to include the-Z option:
Code:
Cd/; afio-I-Z/dev/st0
  
The detailed operation of afio is the same as that of the tar command. For example, to display the list of processed files on the terminal while restoring the archive, and save the list to a file in the/var/log directory, run the following command:
Code:
Afio-I-Z-v/dev/st0 | tee/var/log/restored. files
  
Information about more complex operations using afio. See the afio manual.
  
Note:
Tar and afio can be used to back up linux, but some users may not be used to the command line tool, or some users need more complex tools with stronger network functions, this is not provided by tar or afio.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.