Clone Linux system to another computer with Borg

In this tutorial, we will see how to clone a Linux system to another computer with borg in a few steps.

You might want to clone a Linux system so that you don’t want to reconfigure and install the same packages again to your new installation. Recently I faced the same situation where I wanted to clone my office’s Linux system to my laptop. I found a few ways of cloning the system in internet along with Borg, a deduplicating backup solution which supports encryption, compression and provides very efficient & secure way of backup.

I’ve tested this method with Xubuntu (Ubuntu based distribution) and cloning works fine. It should work in other distributions too as long as you exclude the correct file systems. However, it might not work for different architectures (ex: cloning 64 bit system to 32 bit system or vice versa).

Install Borg

Borg is very well documented. Visit their documentation website and install Borg. Also check the Quick Start guide. So install the Borg in the machine from which you want to clone and initialize a repository (basically a password protected path where you want to save your backup).

Backup system

Now we will backup the root partition excluding some directories and files. So navigate to the root directory first and backup /boot/ and /etc/grub directories and files in order to avoid the grub error after restoring the backup.

sudo su
cd /
tar -czvf boot_backup.tar.gz /boot/*
tar -czvf grub_backup.tar.gz /etc/grub*

Now we will execute the following command. Make sure to replace the /path/to/repo to the repository path which you initialized in Quick Start guide. That path will store the backup files. We also excluded the repo path so that it doesn’t include in backup archive.

borg create --stats --exclude=/dev/* --exclude=/boot/* --exclude=/etc/grub* --exclude=/proc/* --exclude=/sys/* --exclude=/tmp/* --exclude=/run/* --exclude=/mnt/* --exclude=/media/* --exclude="swapfile" --exclude="lost+found" --exclude=".cache" --exclude="Downloads" --exclude=".VirtualBoxVMs"--exclude=".ecryptfs" --exclude=/etc/fstab --exclude=/path/to/repo/* --progress /path/to/repo::MyBackup_1 /

Borg will now backup the entire file system(s) excluding some system files and directories. The archive name will be MyBackup_1.  When finished, copy the backup directory to your external drive so that we can restore from it later (if you set /path/to/repo to external drive, then you are ready to restore)

Restore backup

First, install the same Linux distro as usual in another computer. And install borg. You don’t need to initialize the repo.

Connect your external hard disk/usb drive where you saved the backup. Suppose the external drive is mounted to /run/media/usb

Now execute the below commands in newly installed Linux

sudo su
cd /

Now let’s restore the backup. Make sure to replace the /path/to/repo and the archive name. Like /run/media/usb::MyBackup_1 Also we will restore the backup_tar.gz & grub_backup.tar.gz files

borg extract --progress /path/to/repo::MyBackup_1
tar -xvf boot_backup.tar.gz 
tar -xvf grub_backup.tar.gz

Wait until the process finishes. After that, restart the system. Done!

Leave a Reply

Your email address will not be published. Required fields are marked *