Use a tar backup under CentOS

Source: Internet
Author: User



Tar is a classic Unix command that has been ported to Linux. Tar is an abbreviation for tape ARchive (tape archive), originally designed to package files on tape.



It is a file-based command that essentially stacks files sequentially and end-to-end.



Using tar, you can package the entire directory tree, which makes it particularly suitable for backup. The archive can be restored in its entirety, or a separate file and directory will be expanded from it.



Backups can be saved to a file-based device or tape device. Files can be redirected at restore time so that they are re-placed in a different directory (or system) than the directory (or system) in which they were originally saved.



Tar is file system-independent and can be used on ext2, ext3, JFS, Reiser, and other file systems.



In the Linux world, the tar command is often used for backup, which involves some parameters of the tar command. The full format of the tar command is:



Tar <operation> [options] <files_to_backup_or_restore>



which


    • Operation: Used to specify the action to be made by tar
    • Options: For specifying a range of choices
    • Files_to_backup_or_restore: Used to give the name of a file or directory to be backed up or to be restored, including subdirectories in this directory when the directory is specified.


The following table shows the operation instructions for the tar command.


Operation Description
[-] A Connect multiple archive files to an archive file.
[-]c Used to create a new archive file.
[-]x Restore the backup file from the archive file.
[-]t Used to list the file names in an archive file.
[-]u

Only add files that are newer than the archive file. That is, replace the original backup file with the new file,

If the file you want to update is not found in the archive file, append it to the end of the backup file.

[-]d Compares the contents of an archive file with the current file on the file system.
[-]r Appends the file to the specified archive file.
--delete Deletes the specified file from the archive file.


The following table shows the common options descriptions for the tar command.


Options Description
-F Name Use name to specify the archive file name or device name.
-V Lists the processing details.
-Z Compress the file or unzip it with GNU Gzip.
-j Use GNU bzip2 to compress files or unzip them.
-C Directory switches the current directory to directory.
-M Create/list/recover multi-volume archive files for storage on several backup media.
-N Date specifies that only those files that are newer than date are manipulated.
-P Indicates that you want to retain file permission permissions.
-P Keep the absolute path of the file, that is, do not remove/.
-W Requires waiting for the user to confirm each action.
-W Indicates that the backup content is written to the backup device and then read out for verification to improve reliability.
-T FileName reads the file name that needs to be backed up or restored from the specified file.
-X FileName does not process files listed in the given file.
--exclude=pattern The specified file is not processed.
backing up files with tar


Typically, the backup file is stored in a separate partition, either a partition on the system's local hard disk, or a partition in another mounted mobile device. Therefore, before backing up, you should create a mount directory and mount the file system.


 
# mkdir /backups
# mount -t ext3 /dev/sdc1 /backups
# mkdir /backups/logs /backups/last-full
Specify the files or directories to back up


1. Use the following command to back up one or more of the specified directories to an archive file in the/backups directory


 
# tar -zcvpf /backups/full-backup.tar.gz /home
# tar -zcvpf /backups/full-backup.tar.gz /home /etc


2. Use the command to replace the directory to be backed up



The following command backs up the entire/system except for mnt, media, Dev, proc, backups directory, and Lost+found directory.


# Tar-zcvpf/backups/full-backup.tar.gz-c/> $ (ls/| egrep-v "Backups|mnt|media|dev|lost+found|proc")


3. Use the Exclude option to reject files or directories that do not need to be backed up



1) The following command backs up the entire/system except for mnt, media, Dev, proc, backups directory, and Lost+found directory.


# tar -zcvpf /backups/full-backup.tar.gz -C /> --exclude = mnt --exclude = media --exclude = dev --exclude = proc> --exclude = backups --exclude = * / lost + found> --exclude = var / spool / squid
>. # Here "." Means to back up the root directory (because the previous "-C /" changes the current directory to the root directory) 


2) The following command backs up directories such as etc, home, usr/local, and Var/spool (excluding Var/spool/squid).


# Tar-zcvpf/backups/full-backup.tar.gz-c/>--exclude=var/spool/squid > etc home usr/local var/spool


4. Put the file or directory name you want to back into a text file


 
# cat whatsbackup.txt
/home
/etc
/usr/local
/root
# tar-zcvpf/backups/full-backup.tar.gz-t Whatsbackup.txt


1.-t parameter after the specified file, you cannot use the file wildcard character



2. You can use the LS or find command to generate a whatsbackup.txt file


Incremental Backup


1. Use the tar command with the N option for incremental backups



The following command backs up files modified by the/home directory since 2008-01-29


# tar-n 2008-01-29-zcvpf/backups/inc-backup_$ (date +%f). Tar.gz/home


The following command backs up files that have been modified since the/home directory yesterday


# Tar-n $ (date-d Yesterday "+%f") >-zcvpf/backups/inc-backup_$ (Date +%f). Tar.gz/home


The following two ways of writing are available, but the meanings are different:


    • - N Yesterday : A new file than the current time of yesterday, for example: if the yourselves time is 2:31, it represents a new file since yesterday 2:31
    • - N $ (date-d Yesterday "+%f") : New file since yesterday 0:00


The following command replaces the date obtained from a text file with a command


# Tar-n $ (cat/backups/last-full/full-backup-date) >-zcvpf/backups/inc-backup_$ (Date +%f). Tar.gz/home


/backups/last-full/full-backup-date is a file created with the following command while creating a full backup:


Date +%f >/backups/last-full/full-backup-date


2. Use the Find command to get an incremental backup file list use the following command to find the modified files in n days (for example: 7 days), to generate a list of files for the backup contents


# find/home/etc/root-mtime-7-print >/backups/logs/inc-backup_$ (Date +%f). log


To implement incremental backups of files in the specified file list


# tar-zcvpf/backups/inc-backup_$ (Date +%f). tar.gz >-t/backups/logs/inc-backup_$ (Date +%f). log


Use a tar backup under CentOS


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.