update: 14. 04. 2016
Pozice: Linux / Vlastní Scripty / Replikace systemu /

Zkopíruje linuxový system za běhu na připojený disk, nebo do adresáře. /Alfa verze!/

Pozor, muže obsahovat odlišnosti v závislosti na použité linuxové distribuci!


repsys.sh: Zkopíruje system do adresáře(Bzpečnější)!


root# cd /tmp && wget http://www.mtom.cz/projects/repsys/repsys.sh
#!/bin/sh

# --- Create NFS Root FS --------------------------------------------- #

if [ "$1" = "" ]; then

    echo "$0 "
    exit
fi

COPYTO="$1"

# ------------------------------------------------------------------------------------ #
# cd /tmp && wget http://www.mtom.cz/projects/repsys/repsys.sh && sh repsys.sh         #
# ------------------------------------------------------------------------------------ #

mkdir -p $COPYTO

# --- Install rsync ---
if [ "`which rsync`" = "" ]; then
    apt-get install rsync
fi

echo "Replicate Start from / too $COPYTO/"

mkdir -p $COPYTO/run
mkdir -p $COPYTO/proc
mkdir -p $COPYTO/sys
mkdir -p $COPYTO/mnt
mkdir -p $COPYTO/media
mkdir -p $COPYTO/lost+found

cp /proc/modules $COPYTO/proc/

for DIR in `ls / | grep -v 'proc\|sys\|media\|mnt\|lost+found\|export\|run'`; do

    echo -n " -> copy /$DIR to $COPYTO/$DIR"
    rsync -a -H --delete /$DIR $COPYTO/
    echo " done."

done

echo "Replicate Completed."

#################################################################

repsys2dev.sh: Vytvoří kopii na fyzický disk! (Velký rysk, zlobí instalace grubu!)


root# cd /tmp && wget http://www.mtom.cz/projects/repsys/repsys2dev.sh
#!/bin/sh

# --- Create Clone system -------------------------------------------- #

REPTO="/dev/sdc1"
GRUBINSTALL="yes"

# -------------------------------------------------------------------- #
# cd /tmp && wget http://www.mtom.cz/repsys.sh && sh repsys.sh         #
# -------------------------------------------------------------------- #

repsys()
{

COPYTO="$1"
DEVTO="$2"
GRUBINSTALL="$3"

# --- Install rsync ---
if [ "`which rsync`" = "" ]; then
    apt-get install rsync
fi

# --- get mount dev --
MDIR="`mount |grep $COPYTO|cut -f1 -d' '`"
BDEV="`echo $MDIR|sed 's;[0-9];;g'`"

# --- get UUID from mount dev --
NBLKID="`blkid grep $MDIR`"

# --- boot device ---
DEVBOOT="`hddmaplist fp $DEVTO`"

# --- read param from UUID (uuid, type) ---
for lstblkid in $NBLKID; do

    echo $lstblkid

    if [ "`echo $lstblkid|grep \"^UUID=\"`" != "" ]; then 
	NEWUUID="`echo $lstblkid |sed 's;UUID=\|\";;g'`"
    fi
    
    if [ "`echo $lstblkid|grep '^TYPE='`" != "" ]; then 
	NEWFS="`echo $lstblkid |sed 's;TYPE=\|\";;g'`"
    fi
done

# --- get actual uuid and type in fstab ---
OLDUUID="`cat /etc/fstab|grep " / "|grep -v \"^#\"|sed 's;UUID=;;g'|cut -f1 -d' '`"
OLDFS="`cat /etc/fstab |grep -v \"^#\"|grep \" / \"|cut -f3 -d \" \"`"
echo "Replicate Start from / --> $COPYTO/"

# --- create media directory ---
echo " -> Create Dir $COPYTO/media"
mkdir -p  $COPYTO/media
echo " -> Create Dir $COPYTO/mnt"
mkdir -p  $COPYTO/mnt 

# --- create sys directory ---
echo " -> Create Dir $COPYTO/sys"
mkdir -p $COPYTO/sys
echo " -> Create Dir $COPYTO/run"
mkdir -p $COPYTO/run
echo " -> Create Dir $COPYTO/proc"
mkdir -p $COPYTO/proc
cp /proc/modules $COPYTO/proc

# --- clone files all directory ---
for DIR in `ls / | grep -v 'proc\|sys\|mnt\|lost+found\|export\|media\|run'`; do
    echo -n " -> copy /$DIR to $COPYTO/$DIR"
    rsync -a -H --delete /$DIR $COPYTO/
    echo " done."
done

# --- change uuid for any disk ---
if [ "$OLDUUID" != "$NEWUUID" ]; then

    echo " -> Change param boot device"
    echo "    ROOT_UUID: $OLDUUID -> $NEWUUID"
    echo "    FS: $OLDFS -> $NEWFS"
    echo "    BOOT_DEVICE: $DEVBOOT"

    # --- create new fstab for new device ---
    echo " -> Create new $COPYTO/etc/fstab"
    mv $COPYTO/etc/fstab $COPYTO/etc/fstab.rep.bak
    cat $COPYTO/etc/fstab.rep.bak |grep "$OLDUUID" | sed 's;UUID='$OLDUUID' / '$OLDFS';UUID='$NEWUUID' / '$NEWFS';g'  > $COPYTO/etc/fstab

    # --- create new grub.cfg for new device ---
    echo " -> Create new $COPYTO/boot/grub/grub.cfg"
    mv $COPYTO/boot/grub/grub.cfg $COPYTO/boot/grub/grub.cfg.bak
    cat $COPYTO/boot/grub/grub.cfg.bak | sed 's;'$OLDUUID';'$NEWUUID';g' > $COPYTO/boot/grub/grub.cfg
    
    # --- create new device.map for new device ---
    echo " -> Create new $COPYTO/boot/grub/device.map"
    mv $COPYTO/boot/grub/device.map $COPYTO/boot/grub/device.map.bak
    echo "(hd0)	$DEVBOOT" > $COPYTO/boot/grub/device.map

fi

# --- create new /etc/network/interfaces ---
echo " -> Create new $COPYTO/etc/network/interface."
mv $COPYTO/etc/network/interfaces $COPYTO/etc/network/interfaces.bak
echo "# This file describes the network interfaces available on your system" > $COPYTO/etc/network/interfaces
echo "# and how to activate them. For more information, see interfaces(5)." >> $COPYTO/etc/network/interfaces
echo "" >> $COPYTO/etc/network/interfaces
echo "# The loopback network interface" >> $COPYTO/etc/network/interfaces
echo "auto lo" >> $COPYTO/etc/network/interfaces
echo "iface lo inet loopback" >> $COPYTO/etc/network/interfaces
echo "" >> $COPYTO/etc/network/interfaces
echo "# The primary network interface" >> $COPYTO/etc/network/interfaces
echo "allow-hotplug eth0" >> $COPYTO/etc/network/interfaces
echo "iface eth0 inet dhcp" >> $COPYTO/etc/network/interfaces

# --- Grub Install ---
if [ "$GRUBINSTALL" = "yes" ]; then
    echo " -> Grub Install on $BDEV for device $DEVBOOT"
    grub-install --root-directory=$COPYTO --no-floppy $DEVBOOT 
fi

echo "----------------------------------------------------"
echo "After boot from new disk Change configuration files:"
echo
echo "/etc/fstab"
echo "/etc/network/interface"
echo
echo "----------------------------------------------------"

}

# -------------------------------------------------------------------- #

MOUNTDIR="/media/`basename $REPTO`"

if [ -d "$MOUNTDIR" ]; then

    echo "Exist working diractory! ..."
    exit
else

    mkdir -p "$MOUNTDIR" 
    mount $REPTO "$MOUNTDIR" && repsys "$MOUNTDIR" "$REPTO" "yes"
    umount "$MOUNTDIR"
    rmdir "$MOUNTDIR" 
    echo "Clone System Completed and UMount $REPTO."
fi

# -------------------------------------------------------------------- #

Example:
# repsys2dev
Replicate Start from / --> /mnt/repsys/
 -> Create Dir /mnt/repsys/media
 -> Create Dir /mnt/repsys/mnt
 -> Create Dir /mnt/repsys/sys
 -> Create Dir /mnt/repsys/run
 -> Create Dir /mnt/repsys/proc
 -> copy /bin to /mnt/repsys/bin done.
 -> copy /boot to /mnt/repsys/boot done.
 -> copy /dev to /mnt/repsys/dev done.
 -> copy /etc to /mnt/repsys/etc done.
 -> copy /home to /mnt/repsys/home done.
 -> copy /initrd.img to /mnt/repsys/initrd.img done.
 -> copy /lib to /mnt/repsys/lib done.
 -> copy /lib64 to /mnt/repsys/lib64 done.
 -> copy /opt to /mnt/repsys/opt done.
 -> copy /root to /mnt/repsys/root done.
 -> copy /sbin to /mnt/repsys/sbin done.
 -> copy /selinux to /mnt/repsys/selinux done.
 -> copy /srv to /mnt/repsys/srv done.
 -> copy /temp to /mnt/repsys/temp done.
 -> copy /tmp to /mnt/repsys/tmp done.
 -> copy /usr to /mnt/repsys/usr done.
 -> copy /var to /mnt/repsys/var done.
 -> copy /vmlinuz to /mnt/repsys/vmlinuz done.
 -> Change param boot device
    ROOT_UUID: 08f1f3d5-bd9a-41f2-bd4b-7a42e3836e95 -> 7ad56627-ec40-40ee-9345-8234986fe939
    FS: ext3 -> SEC_ext2
    BOOT_DEVICE: /dev/disk/by-id/ata-Hitachi_HDS721616PLA380_PVF904Z22L9E6N
 -> Create new /mnt/repsys/etc/fstab
 -> Create new /mnt/repsys/boot/grub/grub.cfg
 -> Create new /mnt/repsys/boot/grub/device.map
 -> Create new /mnt/repsys/etc/network/interface.
 -> Grub Install on /dev/sdc for device /dev/disk/by-id/ata-Hitachi_HDS721616PLA380_PVF904Z22L9E6N
Installation finished. No error reported.
----------------------------------------------------
After boot from new disk Change configuration files:

/etc/fstab
/etc/network/interface

----------------------------------------------------

Soubory ke stažení

NazevtypDelkaVytvořeno
repsyssh1 KB26. 01. 2015
repsys2devsh4 KB28. 01. 2015