This method has been verified multiple times in the Fedora system. Suppose you have a Linux system that can work normally. Now you want to copy a system to another hard disk to enable this backup hard disk immediately when the system crashes, that does not affect the operation of the system, and the size of the backup hard disk may be different from that of the original system. It does not matter if the above demanding requirements are met! The following method solves this problem. Prerequisites for the script: 1. Back up the hard disk in/dev/sdb2. before using this script, you must manually
This method has been verified multiple times in the Fedora system.
Suppose you have a Linux system that can work normally. Now you want to copy a system to another hard disk to enable this backup hard disk immediately when the system crashes, that does not affect the operation of the system, and the size of the backup hard disk may be different from that of the original system.
It does not matter if the above demanding requirements are met! The following method solves this problem.
Prerequisites for the script:
1. Back up the hard disk in/dev/sdb
2. Before using this script, you must manually partition the backup hard disk. 1 2 3 <5 6>
/Dev/sdb1 mount/boot
/Dev/sdb2 swap
/Dev/sdb3 mount/
/Dev/sdb5 mount/var
/Dev/sdb6 mount/home
In this way, 1, 2, and 3 are the primary partitions, and 5 and 6 are in the extended partitions. The size of each partition depends on the size of each directory in your original system.
3. This script requires the root permission to run
#! /Bin/sh
# Backup total system to a Mirror Hard Disk
# Written by Visual. Wei
Rootdirlist = 'ls /'
Skiplist = "mnt proc sys"
Bskip = "0"
Export DSK =/dev/sdb
Mntpoint =/mnt/mybakroot
Makedir (){
If [! -D $1]; then
Echo $ "create directory $1"
Mkdir-p $1
Else
Echo $ "dir $1 already existing"
Fi
}
Umnt (){
If [-e/proc/mounts]; then
If grep $1/proc/mounts>/dev/null; then
Umount $1
Echo $ "unmount device $1"
Fi
Fi
}
Checkdisk (){
If [! -E $1]; then
Echo "Mirror Device $1 can not be found"
Return 1
Else
Return 0
Fi
}
If! Checkdisk "$ mongodsk"; then
Exit 1
Fi
Umnt "$ mongodsk" 1
Umnt "$ mongodsk" 5
Umnt "$ mongodsk" 6
Umnt "$ mongodsk" 3
Echo $ "Format ext3 partitions ..."
If checkdisk "$ mongodsk" 3; then
Mkfs. ext3-L/"$ mongodsk" 3>/dev/null
Else
Exit 1
Fi
If checkdisk "$ mongodsk" 1; then
Mkfs. ext3-L/boot "$ mongodsk" 1>/dev/null
Else
Exit 1
Fi
If checkdisk "$ mongodsk" 5; then
Mkfs. ext3-L/var "$ mongodsk" 5>/dev/null
Else
Exit 1
Fi
If checkdisk "$ mongodsk" 6; then
Mkfs. ext3-L/home "$ export DSK" 6>/dev/null
Else
Exit 1
Fi
Echo $ "Format swap partition"
If checkdisk "$ mongodsk" 2; then
Mkswap-v1-L newswap "$ mongodsk" 2>/dev/null
Else
Exit 1
Fi
Makedir $ mntpoint
Echo $ "mount root directory"
Mount "$ mongodsk" 3 $ mntpoint
Makedir $ mntpoint/boot
Makedir $ mntpoint/var
Makedir $ mntpoint/home
Echo $ "mount other directories ..."
Mount "$ mongodsk" 1 $ mntpoint/boot
Mount "$ mongodsk" 5 $ mntpoint/var
Mount "$ mongodsk" 6 $ mntpoint/home
Echo $ "start copy system directories... It may takes very long time"
For I in $ rootdirlist; do
Bskip = "0"
For j in $ skiplist; do
If ["$ I" = "$ j"]; then
Echo $ "skip $ I"
Bskip = "1"
Break
Fi
Done
If ["$ bskip" = "0"]; then
Echo $ "copy directory $ I"
Cp-a/$ I $ mntpoint
Fi
Done
Echo $ "create empty directories ..."
For j in $ skiplist; do
Makedir $ mntpoint/$ j
Done
Echo $ "please use <chroot $ mntpoint>, then editAnd"
The script will find/dev/sdb, umount each partition, format each partition, and finally copy the directory structure.
When the operation is complete, the data on the hard disk is the same as that on the original system.
But there is a problem. When the hard disk of the original system crashes, you must unload the hard disk of the original system and install the backup hard disk boot system. Therefore, the backup hard disk still lacks independent boot capabilities. So there are a few other things
1. chroot/mnt/mybakroot
2. vim etc/fstab
The backup hard disk may have fstab different from the original hard disk, because they may have different storage space sizes and different partition structures.
3. vim boot/grub/menu. lst
The backup hard disk may have a kernel boot menu different from the original hard disk, because they may have different storage space sizes and different partition structures.
4. grub-install/dev/sdb
Write bootloader --- grub to the backup hard disk