This article shows a test in which a whole Red Hat Enterprise Linux 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 be used for CentOS/RHEL 4,5,6,7.
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:
– 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.
# 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.
Note: If using standard redirection, the ‘-f‘ option must be passed separately.
# dump -0u -f - /dev/sda1 | ssh email@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.
Restoring the Backed-Up Data
1. Bootup your system into Rescue Mode.
CentOS / RHEL 6 : How to boot into rescue mode
CentOS / RHEL 7 : How to boot into Rescue Mode or Emergency Mode
2. Rebuild sda1/sda2/sda3/sda4/sda5 by using the fdisk command
Note: When rebuilding 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.
Note: 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 192.168.1.87, and restore sda1 from local sda1.dump file:
# ssh 192.168.1.87 "cd /mnt/sda1 && cat /backup-files/sda1.dump | restore -rf -"
login into 192.168.1.87, and restore sda1 from a remote 192.168.1.124 sda1.dump file:
# ssh 192.168.1.87 "cd /mnt/sda1 && RSH=/usr/bin/ssh restore -r -f 192.168.1.124:/tmp/sda1.dump"
# shutdown -r now
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. Red Hat Enterprise Linux 5 dump program does not support the ext4 file system.
2. The MBR is not backup for it does 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 be setting immunity flag on some important files which means these files could not be deleted/modified/overwrote even by the root user, so restore will fail 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 using tape device, you need the 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 the data block, so I suggest you use the same tape device in backup and restore.
7. In the test of this article, SELinux is enabled, and some files’ SELinux property has been modified but seems everything can be restored correctly.
8. At last, only one dump/restore cannot ensure the data security of your system, the real data security needs 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]