Linux system Backup and recovery

Source: Internet
Author: User
Tags system log

Preface: The previous article briefly introduces the concepts of backup and recovery for Linux systems, followed by a previous article on two commonly used backup and restore commands.

1 Common Backup commands

Before you introduce the backup RESTORE command below, let's briefly explain:

If we are just going to implement a full backup, we can use the TAR command directly, package the important data that needs to be backed up, and then use the CP command to copy it for backup. Write a backup script that runs the script at a specified time by a timed task to achieve a full backup.

However, if you want to implement incremental backups, it is very cumbersome to write your own backup scripts, because you need to decide which ones are the new files to be backed up and so on.

To make it easier to use incremental backups, Linux systems provide a very handy backup recovery tool. The following is a description of two more commonly used backup recovery commands dump and restore.

1.1 Backup command: Dump command

Note: The dump command is not installed by default in some CentOS systems and needs to be installed manually. Then we can use the following command to check in advance whether the system has installed the dump command.

[HTML]View Plaincopy  
    1. [Email protected]~]# rpm-qa |grep Dump

The results of operation 1 are as follows:

Figure 1 See if the system has the dump command installed

As shown in 1, in the query results we only see a tcpdump command. This is the grab command for the Linux system, not the dump command. So this shows that the system does not have the dump command installed and we need to install it manually. At this point, if you determine that the system's Yum source can be used, simply execute the following command and the dump command will be installed automatically by default.

[HTML]View Plaincopy  
    1. [Email protected]~]# yum-y Install dump

The results of Operation 2 are as follows:

Figure 2 Installing the dump command

After installing the dump command, take a look at the basic command format for the dump command:

[[Email protected]~]# dump [option] File name original file or directory after backup

Option Description:

-level: Backup level. (There are 10 backup levels, with a format of "-0" ~ "-9": "-" with no spaces between the specific level numbers.) 0 is full backup, 1 is the first incremental backup, 2 is the second incremental backup ... Up to 9 incremental backups are supported. )

-F FileName: Manually specify the file name after the backup.

-U: After the backup is successful, the backup time is recorded in the/etc/dumpdates file.

-V: Shows the backup process China more output information.

-j: Call the Bzlib Library to compress the backup file, in fact, the backup file is compressed into the. bz2 format.

-W: Displays the backup level and backup time of the partition that is allowed to be dump.

Let's take a look at the backup partition first, you can view the system partition with the following command:

[HTML]View Plaincopy  
    1. [[Email protected]~]# df-h

The results of Operation 3 are as follows:

Figure 3 Viewing the system partition

In this case, because boot has the smallest partition capacity and saves time for backup, a backup demo is done with the boot partition. However, in the actual work, generally, the boot partition does not need to backup, because each installed Linux system, as long as the version is consistent, its boot partition content is the same. This is just for the convenience of demonstrating the dump command, saving backup demo time and choosing the boot partition for backup.

Example:

[HTML]View Plaincopy  
    1. [Email protected]~]# dump-0uj-f/root/boot.bak.bz2/boot/

Description: The backup command. Perform a full backup first, and compress and update the backup time

The results of Operation 4 are as follows:

Figure 4 Full backup partition

Note: When compressing the file, be sure to indicate the compression format (i.e., ". bz2" After the name of the compressed file), to avoid the need for manual decompression after the time because the compression format is not clear the complexity of the decompression process.

[HTML]View Plaincopy  
    1. [[Email protected]~]# cat/etc/dumpdates

Note: Review the backup time file to see when the file was backed up, that is, the time of the dump operation.

The results of Operation 5 are as follows:

Figure 5 Viewing the backup time file

Note: The above is a full backup, then to make an incremental backup, you need to first add new data to the original file. First, let's look at the size of the backup file with the following command

[HTML]View Plaincopy  
    1. [[Email protected]~]# ll-h

Description: View the details of a backup file

The results of Operation 6 are as follows:

Figure 6 View details of a full backup partition file

As you can see, the compressed file size is 30M after the full backup, and now we will add a new file to the original file, for example, add the installation log file Install.log shown above. Copy the 28K system installation log file Install.log to the/boot partition by using the command shown below:

[HTML]View Plaincopy  
    1. [Ro[email protected]~] #cp install.log/boot/

Description: Copy the log file to the/boot partition

Of course, for boot partition boot, it is not recommended to store too much content, to avoid causing the partition's memory consumption is too large, causing the system to fail to start.

This is just for the convenience of the demo, and the added files will be deleted after the demo operation is complete.

Once the data has been added, let's take a look at the incremental backup operation at level 1:

[HTML]View Plaincopy  
    1. [Email protected]~] #dump -1uj-f/root/boot.bak1.bz2/boot/

Description: Incremental backup/boot partition, and compression.

The results of Operation 7 are as follows:

Figure 7 1-level incremental backup partition

After the backup is complete, check out the file details:

[HTML]View Plaincopy  
    1. [[Email protected]~]# ll-h

The results of Operation 8 are as follows:

Figure 8 View 1 level backup partition details

As you can see, the 1-level backup file boot.bak1.bz2 is only 19K in size and does not contain the data content of the previous 30M full backup. The same. Check the backup time file again.

[HTML]View Plaincopy  
    1. [[Email protected]~]# cat/etc/dumpdates

The results of Operation 9 are as follows:

Figure 9 Viewing Backup time file information

As shown in 9, you can see a clear distinction between backup levels.

At this point, if you need to make an incremental backup over time, you can write a script that implements the backup operation as described above by a timed task.

Finally, take a look at the features of the option "W":

[HTML]View Plaincopy  
    1. [[Email protected]~]# dump-w

Description: Query the backup time of the entire partition and the details of the backup level

The results of operation 10 are as follows:

Figure 10 Querying the backup time of the entire partition and the details of the backup level

As you can see, the root and home partitions show that they have never been backed up, and the boot partition shows the backup level and backup time for the last backup.

The above is the basic operation of using the dump command to back up partitions. The next thing you need to know is what to do if you want to back up a file or directory:

[HTML]View Plaincopy  
    1. [Email protected]~]# dump-0j-f/root/etc.dump.bz2/etc/

Description: Full backup of the/etc/directory.

The results of Operation 11 are as follows:

Figure 11 Backup Directory

Note: If you are backing up a directory, you can use only 0 levels for full backups, and incremental backups are no longer supported. In other words, dump can perform an incremental backup only when the partition is backed up, or 12, the system will be presented with an error:

Figure 12 1-Level incremental backup catalog error

1.2 Restore command: Restore command

[[Email protected]~]# restore [mode options] [options]

Mode options Description: The RESTORE command commonly used in the following four kinds of patterns, and in use when the four mode can only choose one, not mix.

-C: Compare the changes in backup data and actual data.

-I: Enter interactive mode and manually select the files that need to be recovered.

-T: view mode for viewing what data is in the backup file.

-R: Restore Mode for data restore.

Option Description:

-F: Specifies the file name of the backup files

Example:

(1) Compare the changes of backup data and actual data

First, you need to simulate a scenario where the original data is corrupted or lost, such as destroying the data or files in the boot partition. See what files are in the boot partition with the following command:

[HTML]View Plaincopy  
    1. [[Email protected]~]# ll/boot

The results of operation 13 are as follows:

Figure 13 Viewing the files in the boot partition

Then use the following command to simulate the destruction of some of these files, such as image file vmlinuz-2.6.32-642.el6.x86_64:

[HTML]View Plaincopy  
    1. [Email protected]~] #mv/boot/vmlinuz-2.6.32-642.el6.x86_64/boot/vmlinuz-2.6.32-642.el6.x86_64.bak

Description: Change the kernel image file in the/boot directory to a name.

Note: When doing a presentation, be sure to remember to change the file name after the presentation is complete, or it will cause the system to fail to start after the shutdown.

Let me show you whether the "-C" mode can compare the presence of files with backup files (that is, the ability to discover existing files has changed).

[HTML]View Plaincopy  
    1. [Email protected]~] #restore-C-f/root/boot.bak.bz2

The results of operation 14 are as follows:

Figure 14-c Mode function Demo

As you can see, the system prompts me that the previously modified image file cannot be found and there is a comparison error. This is the function of the "-C" mode.

At this point, we can restore the boot partition through backup recovery, or we can modify the file name directly as we just did. Here, I first directly change the file name, to avoid the back forget the recovery caused by the system shutdown can not start, and so on after the presentation of all modes to demonstrate the recovery function,

Similarly, we can also look at the file name modified correctly after the use of "-C" to compare what will be the effect of:

[HTML]View Plaincopy  
    1. [Email protected]~] #mv/boot/vmlinuz-2.6.32-642.el6.x86_64.bak/boot/vmlinuz-2.6.32-642.el6.x86_64
    2. [Email protected]~] #restore-C-f/root/boot.bak.bz2

The results of operation 15 are as follows:

Figure 15-c Mode function Demo 2

As you can see, the system will no longer prompt for a comparison error.

(2) Viewing mode

[HTML]View Plaincopy  
    1. [Email protected]~] #restore-T-F boot.bak.bz2

The results of operation 16 are as follows:

Figure 16 Viewing What data information is in the backup file

(3) Restore Mode

Here's a demonstration of how to restore a BOOT.BAK.BZ2 partition backup:

First create a directory with the following command, and then go to the directory to restore operations, to avoid the process of recovery files are everywhere.

[HTML]View Plaincopy  
    1. [Email protected]~] #mkdir Bootbak
    2. [Email protected]~] #cd bootbak/

Restore the fully backed up data first:

[HTML]View Plaincopy  
    1. [Email protected]]# restore-r-f/root/boot.bak.bz2

Description: The recovery will be decompressed at the same time.

The results of Operation 17 are as follows:

Figure 17 Recovering the full backup data

You can see that the data files of the/boot partition are all extracted to the current directory. To restore the incremental backup data:

[HTML]View Plaincopy  
    1. [Email protected]]# restore-r-f/root/boot.bak1.bz2

The results of Operation 18 are as follows:

Figure 18 Recovering incremental backup data

As you can see, the system log files copied to the/boot partition are also extracted to the current directory.

Take a look at how to restore the directory backup, for example, to restore the backup etc.dump.bz2 of the previously backed up/etc/directory. For the sake of simplicity, we will restore the backup of the/etc/directory in the directory that was created when the backup partition was just restored:

First, delete the partition backup data that was just recovered in the file, and then restore our catalog backup:

[HTML]View Plaincopy  
    1. [Email protected]]# RM-RF *
    2. [Email protected]]# restore-r-f/root/etc.dump.bz2

The results of Operation 19 are as follows:

Figure 19 Recovering a catalog backup

Finally, remember to delete the system log files that were copied to the boot partition at the time of the previous demo. However, remember, must not delete the wrong file, mistakenly deleted the boot partition of the original file!

Linux system Backup and recovery

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.