LINUX系统备份还原

时间:2024-07-27 23:35:56

1、Linux系统备份工具 REAR (RELAX-AND-RECOVER)

https://blog.****.net/qq43748322/article/details/78710754?locationNum=2&fps=1

https://www.suse.com/zh-cn/documentation/sle_ha/book_sleha/data/sec_ha_rear_concept.html

以下是关于backup和rescue备份到同一个ISO的测试过程,供您参考:

~~~ 1. # cat /etc/rear/local.conf

OUTPUT=ISO OUTPUT_URL=file:///10.66.219.236/srv/backup <===

BACKUP=NETFS BACKUP_URL=iso:///10.66.219.236/backup <===

BACKUP_PROG_EXCLUDE=("${BACKUP_PROG_EXCLUDE[@]}" '/media' '/var/tmp' '/var/crash')

NETFS_KEEP_OLD_BACKUP_COPY= ISO_MAX_SIZE=4000 <===

2. # rear -v mkbackup

3. # ls -lugh /10.66.219.236/srv/backup/mail/ //生成多个文件

total 4.9G -rw------- 1 root 202 Aug 22 04:36

README -rw------- 1 root 194K Aug 22 04:36

rear.log -rw------- 1 root 973M Aug 22 05:39

rear-mail_01.iso -rw------- 1 root 4.0G Aug 22 05:40

rear-mail.iso -rw------- 1 root 283 Aug 22 04:36 VERSION

4. 将ISO文件拷贝到本地,然后创建新的虚拟机并按照顺序打开iso文件,在恢复过程中会提示更换iso文件,更换完成后,继续恢复,结果显示,系统正常恢复; ~~~

*****

showmount -e *.*.1.80 查看机器的所有共享目录。


2、How to backup and restore a whole Red Hat Enterprise Linux system with the dump/restore commands?

阿里云详解:https://www.aliyun.com/jiaocheng/205464.html
 SOLUTION 已验证 - 已更新 2016年二月22日17:04 -

环境

  • Red Hat Enterprise Linux 4
  • Red Hat Enterprise Linux 5
  • Red Hat Enterprise Linux 6
  • Red Hat Enterprise Linux 7
  • ext2, ext3 or ext4 filesystem
  • dump and restore

问题

  • How to backup and restore a whole Red Hat Enterprise Linux system with the dump/restore commands?
  • Need to migrate, upgrade, or perform disaster recovery on the OS, how to create a backup image?
  • Is it possible to take complete back of OS+ application in RHEL like ignite or system state back up?

决议

1. Introduction

This article shows a test in which a whole Red Hat Enterprise Linux 5.2 system is backed-up to another disk partition, the files system is then destroyed using fdisk (to simulate a disk failure), and finally the system is restored successfully. All the steps can also be used for Red Hat Enterprise Linux 4.

NOTE 1: Since there are different kinds of storage, we could not make detailed test on them, and also consider that any mistake will destroy the system and difficult to recover, so we STRONGLY RECOMMEND to test all the steps on a test machine.

NOTE 2:Since there is now a GPT partitions you may use parted instead of fdisk.

2. Configuration Procedure

Backup Using Dump

  1. All data must be backed up before attempting any kind of restore operation. Data backups should be made on a regular basis. In addition to data, there is configuration information that should be saved, including "/etc/fstab" and the output of "fdisk -l". Running a sosreport/sysreport will capture this information and is strongly recommended.

    # cat /etc/fstab
    LABEL=/ / ext3 defaults 1 1
    LABEL=/boot1 /boot ext3 defaults 1 2
    LABEL=/data /data ext3 defaults 0 0
    tmpfs /dev/shm tmpfs defaults 0 0
    devpts /dev/pts devpts gid=5,mode=620 0 0
    sysfs /sys sysfs defaults 0 0
    proc /proc proc defaults 0 0
    LABEL=SWAP-sda5 swap swap defaults 0 0
    /dev/sda6 /backup-files ext3 defaults 0 0 # fdisk -l
    Device Boot Start End Blocks Id System
    /dev/sda1 * 1 13 104391 83 Linux
    /dev/sda2 14 1925 15358140 83 Linux
    /dev/sda3 1926 3200 10241437+ 83 Linux
    /dev/sda4 3201 4864 13366080 5 Extended
    /dev/sda5 3201 3391 1534176 82 Linux swap / Solaris
    /dev/sda6 3392 4864 11831841 83 Linux

    In this example, we will use the /dev/sda6 partition to save backup files, and we assume that /dev/sda6 is mounted on /backup-files.

  2. Bootup your system into Single User Mode

  3. Use "dump" to backup the contents of the partitions:

    Notes

    • If the system has been running for a long time, it is advisable to run e2fsck on the partitions before backup.
    • dump should(may) not be used at heavily loaded and mounted filesystem as it could backup corrupted version of files. This problem has been mentioned on dump.sourceforge.net and Red Hat Manual

      # dump -0uf /backup-files/sda1.dump /dev/sda1
      # dump -0uf /backup-files/sda2.dump /dev/sda2
      # dump -0uf /backup-files/sda3.dump /dev/sda3

    If you want to do a remote backup, you can use both ssh or configure a non-password login. For information on how to configure ssh for a non-password login, please see How do you set-up SSH with DSA public key authentication?
    Note: If using standard redirection, the '-f' option must be passed separately.

    # dump -0u -f - /dev/sda1 | ssh root@remoteserver.example.com dd of=/tmp/sda1.dump
  4. We then simulate a disk failure by using fdisk to delete the partitions that have been backed up.

    Notice: DO NOT delete /dev/sda6 because it saves our backup files. In general, backup files should of course be stored on in a different location.

Restoring the Backed-Up Data

  1. Bootup your system into Rescue Mode

  2. Rebuild sda1/sda2/sda3/sda4/sda5 by using the fdisk command

    Notice: When rebuild all the partitions, please use the same "Start" and "End" number as before.

  3. format sda1/sda2/sda3/sda5 by using the mkfs command, as shown below.

    Notice: DO NOT format /dev/sda6 because it saves backup files.

    # mkfs.ext3 /dev/sda1
    # mkfs.ext3 /dev/sda2
    # mkfs.ext3 /dev/sda3
  4. Re-label all the partitions so they match how they were before. This step is important for the restore to work.

    # e2label /dev/sda1 /boot1
    # e2label /dev/sda2 /
    # e2label /dev/sda3 /data
    # mkswap -L SWAP-sda5 /dev/sda5
  5. Prepare the working directories

    # mkdir /mnt/sda1
    # mount -t ext3 /dev/sda1 /mnt/sda1
    # mkdir /mnt/sda2
    # mount -t ext3 /dev/sda2 /mnt/sda2
    # mkdir /mnt/sda3
    # mount -t ext3 /dev/sda3 /mnt/sda3
    # mkdir /backup-files
    # mount -t ext3 /dev/sda6 /backup-files
  6. Restore the data

    # cd /mnt/sda1
    # restore -rf /backup-files/sda1.dump
    # cd /mnt/sda2
    # restore -rf /backup-files/sda2.dump
    # cd /mnt/sda3
    # restore -rf /backup-files/sda3.dump

    If want remote restore, you can use both ssh or rsh, maybe you need to configure a none-password login, the following is examples:

    login into 10.0.0.87, and restore sda1 from local sda1.dump file:

    # ssh 10.0.0.87 "cd /mnt/sda1 && cat /backup-files/sda1.dump | restore -rf -"

    login into 10.0.0.87, and restore sda1 from a remote 10.66.0.124 sda1.dump file:

    # ssh 10.0.0.87 "cd /mnt/sda1 && RSH=/usr/bin/ssh restore -r -f 10.66.0.124:/tmp/sda1.dump"
  7. Reboot

3. Other Notices

  1. dump/restore command can only be used for ext2/ext3/ext4 partitions, some IA64 system has a boot partition in vfat format, tar command can be used to backup a vfat partition.

    The ext4 filesystem is supported by dump/restore command only in Red Hat Enterprise Linux 6:
    Is dump available for ext4 file systems on RHEL 6?

    Red Hat Enterprise Linux 5 dump program does not support the ext4 file system.

  2. The MBR is not backup for it is not belong to any partition! So, if you backup your system to another disk, the system is not able to boot up. Please run grub-install in rescue mode first.

  3. Some system will setting immunity flag on some important files which means these files could not be deleted/modified/overwrited even by root user, so restore will failed to overwrite these file and output "Permission Denied" error. In this situation, before restore the system, please find out the files have immunity flag with lsattr command, and remove the immunity flag with "chattr -i" command.

  4. You can backup your data to tape device, another hard disk or another disk partition, just change the dump file name to tape device like following:

    # dump -0uf /dev/st0 /dev/sda1
  5. When use tape device, you need mt command to control forward/backward of tape, please take care of the operation order to make sure the right data is restored into a right partition.

  6. There are some factors can influence the backup/restore, such as the tape device' driver, type(rewind/non-rewind), and the size of data block, so I suggest you to use the same tape device in backup and restore.

  7. In the test of this article selinux is enabled, and some files' selinux property have been modified, but seems everything can be restored correctly.

  8. At last, only one dump/restore can not ensure the data security of your system, the real data security need some detail security policy and backup policy.

  9. If the root partition is a lvm partition. When restoring the root partition, you should run in the rescue mode.
    and do the things as follows.

    # vgscan
    # vgchange -ay VolGroup
    # mount /dev/VolGroup/lv_root /mnt/sysimage
    # cd /mnt/sysimage
    # restore -rf <the dump file of root partition>
  10. For how to backup and restore partition table, please refer to How can I backup and restore partition table?



中文显示:

如何通过 dump/restore 命令备份和恢复整个 Red Hat Enterprise Linux 系统?

 SOLUTION IN PROGRESS - 已更新 2015年四月23日17:49 -

环境

  • Red Hat Enterprise Linux 4
  • Red Hat Enterprise Linux 5
  • Red Hat Enterprise Linux 6
  • Red Hat Enterprise Linux 7
  • ext2, ext3 or ext4 文件系统
  • dump 和 restore

问题

  • 如何通过 dump/restore 命令备份或恢复整个 Red Hat Enterprise Linux (RHEL) 系统?
  • 需要在需要进行 OS 迁移,升级或者灾难恢复,该如何创建一个备份镜像文件?

决议

1. 说明

该文档介绍了一个测试案例。将一个 RHEL 5.2 的系统备份到另一个磁盘,然后使用 fdisk 命令(模拟磁盘故障)破坏该文件系统,并最终成功恢复该系统。注,以下所有步骤均适用于 RHEL 4。

注意: 由于存储的方式有很多,我们无法针对每一种情况进行详细测试。此外,由于操作过程中出现的问题可能会损坏系统且难以恢复,所以我们 强烈建议 您在测试机上执行以下步骤。

2. 配置步骤

使用 Dump 备份数据

  1. 在您执行任何恢复数据的操作之前,请务必备份数据,而且您应该定期备份数据。 除了数据,您还需要保存一些配置信息,包括"/etc/fstab" 和 "fdisk -l" 的输出结果。我们强烈建议您执行sosreport/sysreport 来捕获这些信息。

    # cat /etc/fstab
    LABEL=/ / ext3 defaults 1 1
    LABEL=/boot1 /boot ext3 defaults 1 2
    LABEL=/data /data ext3 defaults 0 0
    tmpfs /dev/shm tmpfs defaults 0 0
    devpts /dev/pts devpts gid=5,mode=620 0 0
    sysfs /sys sysfs defaults 0 0
    proc /proc proc defaults 0 0
    LABEL=SWAP-sda5 swap swap defaults 0 0
    /dev/sda6 /backup-files ext3 defaults 0 0 # fdisk -l
    Device Boot Start End Blocks Id System
    /dev/sda1 * 1 13 104391 83 Linux
    /dev/sda2 14 1925 15358140 83 Linux
    /dev/sda3 1926 3200 10241437+ 83 Linux
    /dev/sda4 3201 4864 13366080 5 Extended
    /dev/sda5 3201 3391 1534176 82 Linux swap / Solaris
    /dev/sda6 3392 4864 11831841 83 Linux

    在这个例子中,我们使用 /dev/sda6 分区来存储备份的文件,而且我们假设该分区挂载在 /backup-files 目录下。

  2. 启动系统,使其运行在 单用户模式 下。

  3. 使用 "dump" 备份分区里的内容:

    注意

    • 如果该系统已经运行了很长时间,我们建议您在备份数据之前对该分区执行 e2fsck 命令。
    • 由于可能会备份修改中的文件,所以请不要在系统高负荷运转或者文件系统被挂载时,使用 dump。dump.sourceforge.net 和 Red Hat Manual里提到了这个问题。

      # dump -0uf /backup-files/sda1.dump /dev/sda1
      # dump -0uf /backup-files/sda2.dump /dev/sda2
      # dump -0uf /backup-files/sda3.dump /dev/sda3

    如果您想在将数据备份到其他服务器的磁盘上,您可以使用 ssh ,且通过更改配置使之登录时不需要密码。关于如何配置免密码登录 ssh,请参考 How do you set-up SSH with DSA public key authentication?
    注: 如果您想使用标准重定向,-f 参数必须单独书写。

    # dump -0u -f - /dev/sda1 | ssh root@remoteserver.example.com dd of=/tmp/sda1.dump
  4. 然后我们可以通过使用 fdisk 命令来删除已经备份好的分区,模拟磁盘故障。

    注意: 不要删除 /dev/sda6,因为它存有我们备份的文件。一般情况下,备份的文件应该保存在不同的位置。

Restoring the Backuped Data

  1. 启动系统进入 救援模式

  2. 通过 fdisk 命令重新创建 sda1/sda2/sda3/sda4/sda5 分区。

    注意: 在重新创建分区时,请使用与之前一样的 "Start" 和 "End" 号。

  3. 使用 mkfs 命令格式化 sda1/sda2/sda3/sda5 ,如下所示:

    注意: 不要格式化 /dev/sda6,因为它存有备份文件。

    # mkfs.ext3 /dev/sda1
    # mkfs.ext3 /dev/sda2
    # mkfs.ext3 /dev/sda3
  4. 重新给所有分区贴标签,这样它们就可以与之前所匹配。这一步对于恢复工作来说至关重要。

    # e2label /dev/sda1 /boot1
    # e2label /dev/sda2 /
    # e2label /dev/sda3 /data
    # mkswap -L SWAP-sda5 /dev/sda5
  5. 创建需要用来挂载分区的目录

    # mkdir /mnt/sda1
    # mount -t ext3 /dev/sda1 /mnt/sda1
    # mkdir /mnt/sda2
    # mount -t ext3 /dev/sda2 /mnt/sda2
    # mkdir /mnt/sda3
    # mount -t ext3 /dev/sda3 /mnt/sda3
    # mkdir /backup-files
    # mount -t ext3 /dev/sda6 /backup-files
  6. 恢复数据

    # cd /mnt/sda1
    # restore -rf /backup-files/sda1.dump
    # cd /mnt/sda2
    # restore -rf /backup-files/sda2.dump
    # cd /mnt/sda3
    # restore -rf /backup-files/sda3.dump

    如果您想备份到远程存储上,您可以使用 ssh 或者 rsh。请参考以下实例配置无密码登录。

    login into 10.0.0.87, and restore sda1 from local sda1.dump file:

    # ssh 10.0.0.87 "cd /mnt/sda1 && cat /backup-files/sda1.dump | restore -rf -"

    login into 10.0.0.87, and restore sda1 from a remote 10.66.0.124 sda1.dump file:

    # ssh 10.0.0.87 "cd /mnt/sda1 && RSH=/usr/bin/ssh restore -r -f 10.66.0.124:/tmp/sda1.dump"
  7. 重启

3. 注意事项

  1. dump/restore 命令只能用于 ext2/ext3/ext4 分区,有些 IA64 系统的 boot 分区是 vfat 格式,此时您可以使用 tar 命令备份 vfat 分区。

    对于 ext4 文件系统进行 dump/restore 命令,仅仅在RHEL6 上是被支持的
    Is dump available for ext4 file systems on RHEL 6?

    对于 ext4 文件系统进行 dump/restore 命令,在RHEL5上是不被支持的

  2. 由于 MBR 不属于任何分区,它不会被备份。所以,如果您将系统备份到另一块磁盘,该系统将不能启动。进入救援模式后,请先执行 grub-install。

  3. 某些系统在一些重要的文件上设置了 immunity 标签,这表明即使是 root 用户也不能删除/修改/覆盖这些文件,所以当恢复系统时会因为覆盖这些文件而导致失败并且输出 "Permission Denied" 的报错信息。这种情况下,在恢复系统之前,请通过 lsattr 命令找出有 immunity 标签的文件,然后通过 "chattr -i" 命令移除 immunity 标签。

  4. 您可以将您的数据备份到磁带设备,其他硬盘或者其他磁盘分区上。您只需要将转储文件名改为磁带设备,如下所示:

    # dump -0uf /dev/st0 /dev/sda1
  5. 当您使用磁带设备时,您需要使用 mt 命令控制磁带的前进/后退。请注意操作顺序以确保正确的数据被恢复到正确的分区。

  6. 由于某些因素可能会影响系统的备份/恢复,比如磁带设备驱动,类型(重绕/非重绕)以及数据块的大小,所以我们建议您使用相同的磁带设备用来行进备份和恢复。

  7. 在该测试案例中,selinux 设成了 enable,而且修改了某些文件的 selinux 属性,但是仍可以正确恢复系统数据。

  8. 最后,仅仅通过 dump/restore 不能保证您系统的数据安全,真正的数据安全需要一些具体的安全和备份的策略。

  9. 如果根分区是 lvm 分区,当您恢复根分区时,请在救援模式下运行系统,并按照以下步骤操作。

    # vgscan
    # vgchange -ay VolGroup
    # mount /dev/VolGroup/lv_root /mnt/sysimage
    # cd /mnt/sysimage
    # restore -rf <the dump file of root partition>
  10. 关于如何备份和恢复分区表,请参考 How can I backup and restore partition table?