Linux--Backup and Restore--tar Command

  

File Compression and Decompression


tar Command

The tar command can create files for linux files and directories.

With tar, you can create a file (backup file) for a particular file, change the file in the file, or add a new file to the file. Tar was originally used to create files on tape, and now users can create files on any device.

With the tar command, you can package a whole bunch of files and directories into one file, which is very useful for backing up files or combining several files into one file for network transmission.

First of all, you need to understand two concepts: packaging and compression.

Packaging refers to turning a large number of files or directories into a total large file; compression is to convert a large file into a small file through some compression algorithm.

Why do you want to distinguish between these two concepts? This stems from the fact that many compression programs in Linux can only compress one file, so when you want to compress a large number of files, you must first make this large file into a package (tar command), and then use The compression program compresses (gzip bzip2 command).





Syntax Structure

tar (option) (parameters (new files need to be packaged) ))


Options
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 -A or --catenate: Added File to the existing backup file; -B: Set the block size; -c or --create: create a new backup file; -C<directory>: This option is used for decompression, to decompress in a specific directory , you can use this option. -d: record the difference of the file; -x or --extract or --get: restore the file from the backup file; -t or --list: list the contents of the backup file; -z or --gzip or --ungzip : processing the backup file by gzip command; -Z or --compress or --uncompress: processing the backup file by the compress command; -f<backup file> or --file=<backup file>: specify backup file; v or --verbose: display instruction execution process; -r: add file to already compressed file; -u: add changed and existing file to existing compressed file; -j: support bzip2 to extract file; -v : Display operation process; -l: File system boundary setting; -k: Keep original file not overwritten; -m: Keep file is not overwritten; -w: Confirm correctness of compressed file; -p or --same-permissions : Restore files with the original file permissions -P or --absolute-names: The filename uses an absolute name, without removing the “/” number before the file name; -N<date format> or --newer=<datetime>: only Files updated from the specified date are saved to the backup file; --exclude=<template style>: Exclude files that match the template style.


Parameters

File or directory: Specify a list of files or directories to package


Example: Backup

Files are all packaged into tarballs
1 2 3 tar -cvf log.tar log2017.log #Package only, no compression. Tar -zcvf log.tar.gz log2017.log #packaged, gzip compressed tar -jcvf log.tar.bz2 log2017.log #packaged, bzip2 compressed

file name after option f is taken by yourself We are used to using .tar for identification. If you add the z option, use .tar.gz or .tgz to represent the gzip-compressed tarball; if you add the j option, use .tar.bz2 as the tarball name.


Check out the files in the above tarball
1 2 3 tar -tvf log.tar #View the files in the tarball, you can view the two types of compression packages. Tar -ztvf log.tar.gz #View the files in the tarball, in gzip compressed format. Tar -jtvf log.tar.bz2 #View the files in the tarball and compress them in bzip2 format.

Back up your files and save their permissions:
1 tar -zcvpf log31.tar.gz log2014.log log2015.log log2016.log

Copyright © Windows knowledge All Rights Reserved