I recently had to remove my hard disk due to bad sectors. Then I needed to backup my Arch Linux installation and install it in my new hard disk.
So, let’s see how to do it.
First, make a bootable USB of Arch Linux.
Please ignore this if you have a working Linux installation.
I used Universal USB Installer, with an Arch Bang iso to make a USB. Please make sure to format the USB as FAT32
(or you can simply use the Format option in UUI). And an issue I came across is having an issue with the volume label of the USB. Make sure to use the volume label (which is default “UUI” when using Universal USB Installer), as the same as in the ISO name in iso file (not the iso file name). Use a mounting tool (such as PowerISO, Demon Tools) to see the ISO name.
Then boot up.
Then mount the needed partitions.
You don’t need to mount the Linux partition if you ignored the previous step.
Let’s assume that your Linux partition (to be backed-up) is sda2
and the target partition you want to save the backup is sdb1 (another disk).First create the mount points.
$ sudo mkdir /mnt/linux
$ sudo mkdir /mnt/data
Then mount the partitions.
$ sudo mount /dev/sda2 /mnt/linux
$ sudo mount /dev/sdb1 /mnt/data
Then create the backup tar file.
$ cd /mnt/linux
$ sudo tar -czvpf /mnt/data/backup.tar --exclude=proc --exclude=sys --exclude=dev --exclude=tmp --exclude=mnt .
If you have ignored the 1st step:
$ sudo tar -czvpf /mnt/data/backup.tar --directory=/ --exclude=proc --exclude=sys --exclude=dev --exclude=tmp --exclude=mnt .
Please note the . at the end of the line. And if you use z option, you’ll be using gzip compression and you must use z when extracting as well.
It is recommended to mount all the other disks at /mnt
. The reason is Arch mounts other disks at /run/media
by default and it will be a hard task to ignore it with having other contents on /run
. Since it is easy to exclude all of the disks at once when they are mounted at /mnt
.
Then restore the backup (either in the same or another machine).
Create a bootable USB as in 1st step and boot the machine with it. Then mount the disks that you’re willing to restore Arch, let’s say sda2 and the partition containing the backup, let’s say sdb1
.
$ sudo mkdir /mnt/arch
$ sudo mkdir /mnt/data
$ sudo mount /dev/sda2 /mnt/arch
$ sudo mount /dev/sdb1 /mnt/data
Then let’s extract the tar file.
$ sudo tar -zxvpf /mnt/data/backup.tar.gz -C /mnt/arch
Please note the z since we have use gzip compression. Now it’s time to have another cup of tea. May be a cup of coffee this time. 😀
Mount dev, proc and sys
Mount them into the target restoring point, since you need the machine relevant information from the session.
$ sudo cp /etc/resolve.conf /mnt/arch/etc
$ sudo mount -t proc none /mnt/arch/proc
$ sudo mount -t sysfs none /mnt/arch/sys
$ sudo mount -o bind /dev/ /mnt/arch/dev
Now already to go. Then change the root to the extracted backup.
$ sudo chroot /mnt/arch
$ source ~/.bashrc
Use the last command if you need to reload the environment parameters (such as environment variables).
Create the config files and restore the grub.
$ sudo genfstab -p . >> /etc/fstab
You can even do this beofore you change the root. Then it has to be.
$ sudo genfstab -p /mnt/arch >> /mnt/arch/etc/fstab
(Then chroot if you followed the later option.)
Then we have to create the initcpio
file again to match the new system.
$ sudo mkinitcpio -p linux
Now we’ll create the new grub config.
$ sudo grub-mkconfig -o /boot/grub/grub.cfg
It will generate the grub configuration file. If you get an error (as I did in this post), you have to edit the default configuration file.
$ sudo nano /etc/default/grub
And add the following line to the end of the file.
GRUB_DISABLE_SUBMENU = y
Now create the grub configuration file again with,
$ sudo grub-mkconfig -o /boot/grub/grub.cfg
It should work. Now install the grub on the hard disk. (Not on the partition.)
$ sudo grub-install /dev/sda
Tadaaaa…!!! Now reboot and see..