wsdjeg/.SpaceVim.d

Backup package installed by package manager!

wsdjeg opened this issue · 2 comments

I want to backup all the packages installed by package manager, such as pacman npm pip etc.

This func make me easy move to new PC!

====================================================
How to transfer a Linux install to another machine.
====================================================

Either the source or destination can be a virtual machine, physical machine, or possibly even a broken disk you can't boot or mount. This howto doesn't really make an assumption of which, so be intelligent and adapt to your needs.

If you want to end up with 2 machines in the end, make sure you have 2 machines to start. If the destination is a virtual machine, make a new vm with a blank disk to send the install to. Make sure the source disk is attached in a way that you can access it, such as attached to a working virtual machine.

====================================================
Full version
====================================================

either:
    1) Start both machines and boot the destination one on a LiveCD. (physical machines, or virtual machines)
    2) mount the destination disk inside the source machine (more difficult to get the bootloader working, but then you don't need a LiveCD)

On the destination machine:
    Format the disk the way you want it,  For example:
        parted /dev/sda mktable gpt
        parted /dev/sda mkpart root 1 -- -1
        mkfs.ext4 /dev/sda1

    Then mount the new disk.
        mkdir /mnt/root
        mount /dev/sda1 /mnt/root

    Transfer using rsync or scp.
        at this point, it is assumed that the source disk is not changing so fast that an rsync copy would be inconsistent. You should shut down all your services on the source machine. You should especially shut down running databases.

        scp push from the source machine (best for speed over fast network):
            source machine :
                cd /
                ls
                scp -r bin boot etc home lib lib64 opt root run sbin selinux srv test tmp usr var destMachine:/mnt/root/

            If the scp is interrupted, you can resume it with scp again, or resume with rsync. But if you use scp again, you also should run rsync afterwards just to fix any half transferred files.

        rsync pull (best for speed over slow network, or to resume an interrupted transfer):
            Then use rsync to pull the whole root system to the new machine (exclude /dev /proc /sys and anything else unneeded: /mnt /media etc).  

                rsync -avHP --exclude dev --exclude proc --exclude sys --exclude media --exclude mnt sourceMachine:/ /mnt/root/

                    (add -z if you have a very slow network, which compresses things)

    Create some directories we need
        cd /mnt/root
        mkdir dev proc sys media mnt

    Then from the destination machine, you chroot into wherever you put the files, and run "grub-install /dev/sdX" and "update-grub"

        mount -o rbind /dev dev
        mount -o rbind /proc proc
        mount -o rbind /sys sys
        chroot /mnt/root

        Now within the chroot environment (make sure device /dev/sda is correct or you can damage your other install):

            blkid
            vim /etc/fstab
                replace any UUID=... lines with the correct uuid from blkid
                if it uses /dev/sdX instead, replace it with UUID=...
                for lvm, it's fine to use /dev/vg/lv or /dev/mapper/vg-lv instead of UUID
                also /dev/disk/by-id/... and /dev/disk/by-uuid/... is fine.

            vim /etc/udev/rules.d/70-persistent-net.rules
                (Optional) remove all lines from this file, or replace the old MAC addresses with the new ones. This will cause the network devices to appear in the default order again (eth0, eth1, etc.) instead of the dest machine having the names all different when you start it up again.

                If you start up your new vm and eth0 doesn't exist and instead you have eth1, you skipped this step. 

                On RedHat and CentOS, I think this file doesn't exist, and I'm not sure what to do in this step.

            grub-install /dev/$disk
            update-grub 
                NOTE: this will not always work if you have other disks attached. With a LiveCD, this probably always works fine.

            non-debian systems don't have update-grub... instead use something like:
                grub-mkconfig -o /boot/grub/grub.cfg

            then you edit fstab and any other important files (maybe even grub.cfg) to make sure partitions match what you see in "lsblk" output. Use uuids to be safest.

            vim /boot/grub/grub.cfg
                normally you don't hand edit this file, instead update-grub or grub-mkconfig does this... but if you have other disk attached, you need to fix this once, reboot and you can relyon update-grub/grub-mkconfig afterwards. With a single disk and booted from a LiveCD, you shouldn't need to do this.

                replace any UUIDs that are wrong with the correct root one

            Then you might need to rebuild the initrd (probably depends on hardware or something changing rather than simply the files being copied to a new disk on same hardware).
                In openSUSE, this should work:
                    mkinitrd
                in Debian, it is:
                    mkinitramfs
                or
                    #(not sure if it's -c or -u)
                    update-initramfs -u

                and in some you need to specify some options
                    mkinitrd -k nameofkernelfile -i nameofinitrdfile ... etc.
                and in some, you need to install this tool because it's not there by default.

            Then you need to know the 2 machines aren't running at the same time, or at least don't have the same IP. So at this point you can either:
                1) shut down the source machine
                2) change the IP on one machine to something else
                (the mac address should also be different... make sure this is true; check ifconfig output)

                    debian based:
                        vim /etc/network/interfaces
                    openSUSE:
                        vim /etc/sysconfig/network/ifcfg-eth0
                        ( also ifcfg-eth1, ifcfg-br0, etc.)

            you should be done now. hit ctrl+d to exit the chroot

    Then unmount everything (optional since reboot does this)

        umount /mnt/root/{dev,proc,sys}
        umount /mnt/root

    Then try to boot your new system without the LiveCD


====================================================
The short short version
====================================================
 (just to show it's really a shorter procedure than it appears above):

    parted /dev/sda mktable gpt
    parted /dev/sda mkpart root 1 -- -1

    mkfs.ext4 /dev/sda1

    mount /dev/sda1 /mnt/root

    rsync -avHP --exclude /dev --exclude /proc --exclude /sys --exclude /media --exclude /mnt sourceMachine:/ /mnt/root/        

    cd /mnt/root
    mkdir dev proc sys media mnt

    mount -o rbind /dev dev
    mount -o rbind /proc proc
    mount -o rbind /sys sys
    chroot /mnt/root

        blkid
        vim /etc/fstab
            make sure UUIDs are right
        vim /etc/udev/rules.d/70-persistent-net.rules
            (Optional) remove all lines from this file

        grub-install /dev/$disk
        grub-mkconfig -o /boot/grub/grub.cfg

        vim /boot/grub/grub.cfg
            make sure UUIDs are right

        #debian based: (not sure if it's -c or -u)
            update-initramfs -u
        #openSUSE:
            mkinitrd

        #debian based:
            vim /etc/network/interfaces
        #openSUSE:
            vim /etc/sysconfig/network/ifcfg-eth0

        exit