Linux tar command advanced usage--Backing up data

Source: Internet
Author: User

Linux tar command advanced usage--Backing up data2015-12-31 Linux Learning

With the powerful tar command on Linux, Tar was originally designed to make tape backup (tape archive), which was intended to back up files and directories to tape and then extract or recover files from tape. Now we can use tar to back up the data to any storage medium. It is a file-level backup that does not have to consider the underlying file system categories and supports incremental backups.

1. Some common options

-Z,--gzip: using the Gzip tool (solution) compression, the suffix is generally. gz

-C,--create:tar package, suffix generally. tar

-F,--file=: The file name that is immediately followed by packaging or compression

-X,--extract: unpacking command, corresponding to-C

-P: retain the original permissions and properties of the backed up data

-G: Snapshot file followed by incremental backup

-C: Specify the directory to unzip

--exclude: excludes non-packaged directories or files, supports regular matching

Other

-x,--exclude-from: lists directories or files to exclude in a file (used when--exclude= is more)

-T,--list: lists the list of files in the backup archive and does not appear with-C,-X

-j,--BZIP2: using the Bzip2 tool (solution) compression, the suffix is generally. bz2

-P: retains absolute path, and automatically extracts to absolute path when decompressed

-V:(solution) The compression process shows the process of file processing, commonly used but not recommended for large files

2. Incremental backup (website) data

Many systems (applications or Web sites) have static files every day, for some of the more important static files, if there is a need for regular backup, you can use the TAR package compression backup to the specified place, especially for some of the total files compared to a large number of cases, you can also use the-G option to do incremental backup.

The backup directory is best used relative path, that is, into the root directory that needs to be backed up

The specific example method is as follows.

Back up all files in the current directory

# tar-g/tmp/snapshot_data.snap-zcpf/tmp/data01.tar.gz.

Extract recovery in a directory that requires recovery

# tar-zxpf/tmp/data01.tar.gz-c.

The-G option can be used to understand a snapshot of a directory file during a backup, record permissions and attributes, and/TMP/SNAPSHOT_DATA.SNAP not exist at the first backup, creating a new one and making a full backup. When the file under the directory has been modified, the first backup command (remember to modify the following file file name), will be automatically based on the snapshot files specified by-G, the incremental backup of modified files, including permissions and properties, not moved files are not repeated backup.

It is also important to note that the above recovery is "retention recovery", that is, files with the same file name will be overwritten, and the original directory already exists (but not in the backup file), will remain. So if you want to fully recover the same as the backup file, you need to clear the original directory. If you have an incremental backup of your files, you will also need to extract the files separately using the same method, and be aware of the sequence.

A more comprehensive example is shown below, which requires:

Back up the/tmp/data directory, but the cache directory and temporary files are excluded

Because the directory is larger (>4G), split the backed-up archive at full time (e.g. 1G maximum per backup profile)

Retain permissions and properties for all files, such as user groups and read and write permissions

# Cd/tmp/data

Make a full backup

# rm-f/tmp/snapshot_data.snap

# tar-g/TMP/SNAPSHOT_DATA.SNAP-ZCPF---exclude=./cache./| Split-b 1024M-/tmp/bak_data$ (date-i). tar.gz_

After the file name is split, the Aa,ab,ac,... is added, and the final backup archive is saved as

Bak_data2014-12-07.tar.gz_aa

Bak_data2014-12-07.tar.gz_ab

Bak_data2014-12-07.tar.gz_ac

...

Incremental backup

Can be the same as a full backup command, but note that if you backup multiple times a day, may cause the file name duplication, it will lead to

Backup implementation, because split will still be named from Aa,ab, if a day's file generation (modification) amount is not particularly large, then the proposed increment part does not

Split processing: (Be sure to split the words, the file name to add more granular time such as $ (date +%y-%m-%d_%h))

# tar-g/tmp/snapshot_data.snap-zcpf/tmp/bak_data2014-12-07.tar.gz--exclude=./cache.

Next day Add-on

# tar-g/tmp/snapshot_data.snap-zcpf/tmp/bak_data2014-12-08.tar.gz--exclude=./cache.

Recovery process

Recovering a fully backed up archive file

You can choose whether to empty the/tmp/data/directory first

# Cat/tmp/bak_data2014-12-07.tar.gz_* | TAR-ZXPF--c/tmp/data/

Restore an incremental backup of an archive file

$ tar–zxpf/tmp/bak_data2014-12-07.tar.gz-c/tmp/data/

$ tar–zxpf/tmp/bak_data2014-12-08.tar.gz-c/tmp/data/

...

Be sure to restore in chronological order, like the following file name rules can also be used in the form of the above wildcard characters

If you need regular backups, such as full weekly, one incremental backup per day, you can combine crontab implementations.

3. Backing up the file system

There are many ways to back up the file system, such as Cpio, rsync, dump, tar, which shows an example of backing up the entire Linux system through tar, and the entire backup and recovery process is similar to the above.

First Linux (here is CentOS) there is a part of the directory is not necessary to back up, such as/proc,/lost+found,/sys,/mnt,/media,/dev,/proc,/tmp, if Backup to tape/dev/st0 do not care so much, Because I am here to back up to the local/backup directory, so also need to exclude, there are some other NFS or network storage mounted directory.

Create an exclusion list file

# vi/backup/backup_tar_exclude.list

/backup

/proc

/lost+found

/sys

/mnt

/media

/dev

/tmp

$ tar-zcpf/backup/backup_full.tar.gz-g/backup/tar_snapshot.snap--exclude-from=/backup/tar_exclude.list/

4. Note

Using tar whether you are backing up data or a file system, you need to consider recovering on the original system or on another new system.

The tar backup relies heavily on the Atime property of the file.

The user is determined by the user ID of the file, and the machine recovery needs to consider the same user with the same userid

Backup and recovery process try not to run other processes, which can result in inconsistent data

Soft and hard connection files can be restored normally

From: Sean's Notes small tribal Pavilion

Links: http://seanlook.com/2014/12/08/tar_backup_filesystem/

Linux tar command advanced usage--Backing up data

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.