This tutorial continues where we left of in the Backing up your Pi tutorial.
We’ve already seen how you can create a backup/clone of your pi to an SD card with the same or bigger size as the one you are using at that moment.
In this tutorial I’ll demonstrate a way to clone your SD card (live) to a smaller SD card, this time without the need of a bigger SD card as in the previous tutorial.
Requirements
For this to work we need a running system with the Raspberry Pi OS (previously known as Raspbian OS). It doesn’t matter what version of pi you are running.
The next we need is a smaller SD card (eg. a 8GB one) then the one we are currently using.
O and don’t forget an SD card reader, one must have a reader.
Preparing your system
So before we go and clone our system we’ll download the required tools to do so.
First, as always, we’ll update our system (you can skip this).
Reasons for skipping is compatibility issues with newer software versions… Yes I had problems with that in the past.
sudo apt update && sudo apt upgrade -y
If you haven’t done this in a while, then this can take some time. I suggest you take a break while waiting.
When you get back from your break the pi should have finished and we can clone the git repository to our Pi.
As always when I didn’t do the work myself, I need to give credit to the one who did.
This cloning software has been written by a git user known as ‘billw2’, many thanks for this work bill.
cd Downloads
git clone https://github.com/billw2/rpi-clone.git
cd rpi-clone
In some cases the download folder does not exists, its not required to have one but its the cleanest way of working so we’ll make it ourself
mkdir Downloads
Downloading the software shouldn’t take to long, unless you have a slow internet connection.
After completion of the download we go into the rpi-clone folder, this time we copy the files to the sbin folder.
Copying a file to the sbin folder allows us to run it from every folder instead of specifying the folder where the executable file is located. This also makes it a bit more dummy proof as one probably won’t delete files from there.
sudo cp rpi-clone rpi-clone-setup /usr/local/sbin
cd
So we are ready to clone our SD card, lets see how that works.
Cloning
Back in our home directory we’ll see that the ‘rpi-clone’ software is no where to see. But didn’t I tell you that wasn’t a problem?
Lets try to run it.
rpi-clone

Ok so it runs, but we need to run it as a root user. That shouldn’t be a problem but before we do lets check out what it can do.
sudo rpi-clone -h
A quote from the help file:
Clone a booted file system to a destination disk which is bootable.
The destination disk is a SD card (USB card reader) or USB disk ‘sdN’ plugged into a USB port. The ‘sdN’ name should be a full disk name like sda and not a partition name like sda1.
sudo rpi-clone -h
That means we need to insert the SD card using a SD card reader.
We also need to know where it is located on the Linux OS. If no other drives are connected this usually will be SDA but better save then sorry and actually check it out ourself:
sudo fdisk -l

Our inserted SD card is sda as expected.
In my case i want to clone the image of my server to create a cluster. So i want to change the hostname while I clone it. there are 2 ways of doing this:
sudo rpi-clone-setup -t 'hostname'
or
sudo rpi-clone sda -s 'hostname'
Note that the second method directly starts the cloning process and the first one only stores the new hostname in a tmp file for later use. I recommend the 2nd approach as the first one doesn’t always work.

Running the cloning does take some time, its advisable to do so when you don’t need the system.
sudo rpi-clone sda -s 'hostname' -f
When finished its time to check out the good work we’ve done.
It doesn’t hurt to check if it actually works, so put it into a spare pi and check it out.
If you added a fixed IP I suggest you change it before putting it into the new pi.
Conclusion
In my humble opinion this is the best way of cloning a system. It might even be a good way to back it up thought I prefer my backups on network drive or local PC.
This is the easiest way of backing up so I’ll keep using it in the future, even for backups. Thought I’ll use my previous way for the backup I probably will replace the piclone script with this script, even when it takes longer there is less haggle.