Comprehensive backup method for Linux system

Source: Internet
Author: User


The company has a server, moved to move over the past, it is not clear who is responsible for the machine. In vain for one months, the period also issued a notice, and no colleague response. Single-user access to the system, found inside are very old data, but not sure whether it is useful, also dare not. So, copy all the data of the hard drive, so as to avoid wasting resources in vain.

Scenario 1:

copy the entire hard disk directly, very low-level data replication, all data including boot loader and partition table can be copied completely. However, the capacity of the target disk can not be lower than the capacity of the source disk, or the file system is prone to tragedy ( strongly recommended: The best two hard drives are the same capacity and the same model, otherwise more troublesome. ). And this way you need to copy the entire hard disk, even if you do not write any files, the speed is very slow.

The DD command can copy a single hard drive:

# dd IF=/DEV/SDA Of=/dev/sdb & ddpid=$!

The USR1 signal can be sent to the DD process at another terminal to view the current progress

# KILL-USR1 $ddpid

You can also copy to multiple drives at the same time

# DD IF=/DEV/SDA | Tee > (dd of=/dev/sdb) > (dd OF=/DEV/SDC) > (dd of=/dev/sdd) >/dev/null

There are also g4l, and Clonezilla tools like this can be replicated in a full-fledged way.


Scenario 2:

1. Mount the hard drive and add the partition

First step: Add a hard disk/new partition (FDISK)

Step two: Format the partition (MKFS.EXT4)

Step three: Load partition (Mount)


1. First step: Add hard disk/new partition (FDISK)

A, view current system all hard disk and partition condition: fdisk-l
b, create a partition on the specified hard drive (example:/DEV/SDA): Fdisk/dev/sdb, follow the prompts for the next steps, such as: View Help (h), new partition (n), delete partition (d), view partition (P)
c, after the partition succeeds, write the partition table and exit (W)

Note: Fdisk supports a maximum size of 2TB for hard drives, please refer to the Linux online manual (man fdisk) or Baidu for a more detailed description.

2. Step two: Format partition (MKFS.EXT4)

To format a new partition (example:/DEV/SDA1): MKFS.EXT4/DEV/SDB1.

3. Step Three: Load partition

A, create a partitioned mount directory, for example: Mkdir/disk-cache-1 and Mkdir/disk-cache-2
B, edit the/etc/fstab configuration file, and write the partition information in.
C. Load new partition: Mount-a


2, Copy the file system root directory Note to exclude the target disk mount directory and the system's own mount directory (can be viewed with mount-l, such as/proc,/sys these directories)

# su-root# CD/# tar-cvpzf/mnt/backup.tgz--exclude=/proc--exclude=/sys--exclude=/lost+found--exclude=/mnt--exclud E=/media--exclude=/var/tmp/or # tar C--exclude=/proc/*--exclude= $EXCLUDE _dir/| (CD/MNT/SDB2; tar x)

C – Create a new backup document
v– verbose mode, the TAR program will output all the information in real time on the screen.
P – Save the license and apply it to all files.
Z – Uses ' gzip ' to compress backup files to reduce the volume of backup files.
F – Describes the path where the backup files are stored,/mnt/backup.tgz is the backup file name in this example. This backup file is backed up in a different partition, which is the original win partition. To look at the directories we want to include, we don't want to back up everything because some directories are not very useful. Confirm that you do not want to include the files, otherwise you will get a very strange result. You also don't have to back up anything under the/MNT directory unless you have other partitions hanging under it or you insist on backing it up. You must also ensure that no resources are attached to the/media directory (for example, any CD or Removable storage media), otherwise, the/media is excluded.

The last parameter is the directory we want to save. We want to save everything, using/as the root directory.

3, the method of restoration : Please note that your original data may be overwritten, and the following actions must be taken carefully.

# Tar-xvpzf/mnt/backup.tgz-c/

The parameter x is to tell the TAR program to unzip the backup file. The-c parameter is the directory to which the specified tar program is decompressed.


The situation is a little more complicated if the system is to be moved over.

Partition the new hard disk with Fdisk, then create a good file system and swap partition with Mkfs.ext4/mkswap, then mount the partition mount to the system.

Modify the/etc/fstab, now the Linux system is basically through the UUID instead of/DEV/SDC1 this way to mount, so to modify the new hard disk corresponding partition of the UUID.

Install boot loader

At first we thought that the first 446 bytes of the source drive MBR (the boot Code section) could be copied directly with DD, but the boot found that the Grub menu was not in.

In fact, grub boot contains two stages of code, the first phase of the code is in the MBR, the second stage on the file system, then how does grub jump from the first phase to the second stage? It records the disk sector location where the second-stage code resides in the MBR [1]. And this code on each hard disk location is certainly not the same, so grub can not jump to the second phase to continue execution.

So we have to reinstall boot loader:

# Grub-install--ROOT-DIRECTORY=/MNT/SDB2

Here--root-directory is telling grub that our root filesystem is on the SDB2 instead of the source hard drive.


Scenario 3:

You can try rsync the first full sync is equivalent to copying only the modified files later. (I haven't tried it.)



This article is from the "Share Your Knowledge" blog, so be sure to keep this source http://skypegnu1.blog.51cto.com/8991766/1653272

Comprehensive backup method for Linux system

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.