July 9, 2019

Using rpi-clone on a Raspberry Pi

Create swappable Micro SDHC cards. How to quickly and simply backup and restore your Raspberry Pi.

Using rpi-clone on a Raspberry Pi

Why rpi-clone

Baaa-cause... it makes backing up and restoring stupid simple.

One of the greatest things about Raspberry Pis is that for most things that you need to do with them - somebody has written a guide for it (like this one). This is also one of the greatest sources of frustration. Because... well lets face it - so many of the guids written are simple for three or four versions of Raspbian ago. Weezy, Jessie and Stretch are all great guys - but sometimes they just hurt Buster.

Things change for the better all the time in the world of open source. New versions of Raspbian are released (which we're always excited about) - but when they do, many of the guides, "how to"s and StackOverflow questions are left there to missguide and misslead you into doing something that simply doesn't work anymore. Or even abandoned projects and repositories that simply don't work on your new setup.

I can't tell you how many times I've started a long project following a guide written a while ago - and for some unforseen reason - it's simply not going to work now because of some... I dunno... bluetooth driver... or something like that. And then Boom - all of that work to setup my Pi before I started down one path to success only leads me to a point where I've painted myself into a corner with no way out.

I don't know about you - but I don't like leaving a bunch of broken packages installed on my pi that I'm not using.

rpi-clone is your savior.

It's fast, simple, and helps you create milestones that you can come back to if you ever find yourself deep in one of those guides of dispair.


Preparation

Go check out the github project, but don't stay for too long - because as awesome of a tool as it is and how simple it can be to use, it's also extremely powerful and offers lots of options which even confuse the heck out of me. Check it out, and then come back here and I'll show you a simple pattern to get you working in no time: https://github.com/billw2/rpi-clone

Have a duplicate Micro SDHC card

Okay, so rpi-clone is so cool that you don't even need the same size card to backup to. But I always do anyways because basically the way I use it is by swapping them back and forth. If I mess something up - no problem - I just sudo poweroff, swap the Micro SDHC card and power up again and you're back at it like nothing ever happened. So, just know you can use difference size cards - but I reccomend having duplicates. Heck, they are so inexpensive now anyways.

Which card reader should I use?

I go into depth about card readers in my article titled "Hello World" - feel free to go check it out to get the full download of my thoughts on this topic.

TLDR; Find one you trust, and use it forever... I use the IOGEAR MicroSD Card Reader.

A toolkit for everything, and everything in it's toolkit

Also, if you haven't decided yet where you're going to put tools and various utilities that you've downloaded I suggest doing that now.

I use the same system on my Pi that I use on my dev box - and that is, I organize most things I work on in "Documents" (hey, code is a document too...).

So, since this is one of the first things I usually install on my Pi - I usually at this point create a Documents folder. We're also going to make a place for tools and utilities to live:

mkdir -p ~/Documents/Toolkit
cd ~/Documents/Toolkit
pwd
mkdir -p ~/Documents/Toolkit
cd ~/Documents/Toolkit
pwd
/home/jeremy/Documents/Toolkit

I always name folders with capitol letters because I use zsh and it will take care of the cases for me. If you're using bash, you probably want to use lower case letters for everything to make life easier with the auto-complete available to you when you hit tab.

Put your tools wherever you like - use whatever naming structure makes sense to you. But please do think a bit about organization. I don't believe in putting everything into ~. I use "Toolkit" because of some history in unix and groups of commands being thought of as tools and toolkits. I'm probably using the word wrong here, but hey - it works for me.


Installation

Now that you have a place to download tools to. Let's go get rpi-clone.

git clone https://github.com/billw2/rpi-clone.git 
cd rpi-clone
sudo cp rpi-clone rpi-clone-setup /usr/local/sbin
git clone https://github.com/billw2/rpi-clone.git
Cloning into 'rpi-clone'...
remote: Enumerating objects: 164, done.
remote: Total 164 (delta 0), reused 0 (delta 0), pack-reused 164
Receiving objects: 100% (164/164), 77.60 KiB | 0 bytes/s, done.
Resolving deltas: 100% (62/62), done.
cd rpi-clone
sudo cp rpi-clone rpi-clone-setup /usr/local/sbin

Congratulations. Now you have rpi-clone.

Make sure you have /usr/local/sbin in your path

This is what my .zshrc file has for my PATH on the 2nd line:

export PATH=$HOME/bin:/usr/local/bin:/usr/local/sbin:$PATH

Make sure you have something similar. The important part is:

export PATH=$HOME/bin:/usr/local/bin:/usr/local/sbin:$PATH


Usage

I reccomend starting out with a completely reformatted disk.

Plug your usb card reader in, with the alternate Micro SDHC

Figure out what the device name is

sudo lsblk -o UUID,NAME,FSTYPE,SIZE,MOUNTPOINT,LABEL,MODEL
sudo lsblk -o UUID,NAME,FSTYPE,SIZE,MOUNTPOINT,LABEL,MODEL

UUID                                 NAME        FSTYPE  SIZE MOUNTPOINT LABEL  MODEL
                                     sda                29.7G                   Storage_Device
B6BB-0F0E                            ├─sda1      vfat    256M            boot
cd0f8c28-dc71-4cc8-bbf5-a34168569170 └─sda2      ext4   29.5G
                                     mmcblk0            29.7G
B6BB-0F0E                            ├─mmcblk0p1 vfat    256M /boot      boot
638417fb-7220-47b1-883c-e6fee02f51ac └─mmcblk0p2 ext4   29.5G /          rootfs

As we see here - the storage device name is sda. This is a disk that has already been backed up to. Had it not been - it would only have sda1 and it would probably be a full size fat partition. Which is most likely what you have.

Start your backup

sudo rpi-clone sda
sudo rpi-clone sda

Booted disk: mmcblk0 31.1GB                Destination disk: sda 31.0GB
---------------------------------------------------------------------------
Part      Size    FS     Label           Part   Size    FS  Label
1 /boot  268.3MB  fat32  --              1      31.0GB  --  --
2 root    30.8GB  ext4   rootfs
---------------------------------------------------------------------------
== Initialize: IMAGE mmcblk0 partition table to sda - FS types mismatch ==
1 /boot               (41.5MB used)  : IMAGE     to sda1  FSCK
2 root                (1.3GB used)   : RESIZE(30.7GB) MKFS SYNC to sda2
---------------------------------------------------------------------------
Run setup script       : no
Verbose mode           : no
-----------------------:
** WARNING **          : All destination disk sda data will be overwritten!
                       :   The partition structure will be imaged from mmcblk0.
-----------------------:

Initialize and clone to the destination disk sda?  (yes/no): yes
Optional destination  ext type file system label (16 chars max):

Initializing
  Imaging past the start of /boot partition 2.
  => dd if=/dev/mmcblk0 of=/dev/sda bs=1M count=268 ...
  Resizing last partition to end of disk ...
    Resize success.
  Changing destination Disk ID ...
  Delaying so partprobe can update /dev entries ...
  => fsck -p /dev/sda1 ...
  => mkfs -t ext4  /dev/sda2 ...

Syncing file systems (can take a long time)
Syncing mounted partitions:
  Mounting /dev/sda2 on /mnt/clone
  => rsync // /mnt/clone with-root-excludes ...
  Mounting /dev/sda1 on /mnt/clone/boot
  => rsync /boot/ /mnt/clone/boot  ...

Editing /mnt/clone/boot/cmdline.txt PARTUUID to use 803d9caa
Editing /mnt/clone/etc/fstab PARTUUID to use 803d9caa
===============================
Done with clone to /dev/sda
   Start - 02:54:05    End - 02:58:48    Elapsed Time - 4:43

Cloned partitions are mounted on /mnt/clone for inspection or customizing.

Hit Enter when ready to unmount the /dev/sda partitions ...
  unmounting /mnt/clone/boot
  unmounting /mnt/clone
===============================

Okay - a lot is going on here.

It's going to take a look at your clean disk and say "This thing needs to get reformatted" and ask you if you want to do this or not.

Type yes then hit enter.

Then it's going to ask you to give an optional name to the disk - I never do because you never really see this.

Just hit enter.

Congratulations, you have a full backup. If you foobar your Raspberry Pi disk, you can just pop, swap and go.


Unattended Backup

Once you have a backup you're working with, you can run rpi-clone again and it will just rsync the difference without doing a reformat - this is much faster. Typically it takes about 40 seconds.

to run it "unattended" use the -u option

sudo rpi-clone sda -u

Now it won't ask you any questions - it will just do the backup.

Then it will basically walk you through the setup with minimal questions. I'll format a disk and show you what that looks like later with an update to this blog.

At any rate - since this disk has already been formatted, it asks you if you want to do the backup (you can disable that with an option) and then it will rsync the files that have changed. As you can see - this took only 53 seconds to do, which is about average for syncing the diff after I've made a few blog posts. If there is litterally nothing to do it takes about 40 seconds right now.

And it's basically that simple. Every time you want to back up - just type

sudo rpi-clone sda -u
sudo rpi-clone sda -u

Booted disk: mmcblk0 31.1GB                Destination disk: sda 31.0GB
---------------------------------------------------------------------------
Part      Size    FS     Label           Part   Size    FS     Label
1 /boot  268.3MB  fat32  --              1     268.3MB  fat32  --
2 root    30.8GB  ext4   rootfs          2      30.7GB  ext4   --
---------------------------------------------------------------------------
== SYNC mmcblk0 file systems to sda ==
/boot                 (41.5MB used)  : SYNC to sda1 (268.3MB size)
/                     (1.3GB used)   : SYNC to sda2 (30.7GB size)
---------------------------------------------------------------------------
Run setup script       : no
Verbose mode           : no
-----------------------:

Syncing file systems (can take a long time)
Syncing mounted partitions:
  Mounting /dev/sda2 on /mnt/clone
  => rsync // /mnt/clone with-root-excludes ...
  Mounting /dev/sda1 on /mnt/clone/boot
  => rsync /boot/ /mnt/clone/boot  ...

Editing /mnt/clone/boot/cmdline.txt PARTUUID to use 803d9caa
Editing /mnt/clone/etc/fstab PARTUUID to use 803d9caa
===============================
Done with clone to /dev/sda
   Start - 03:03:24    End - 03:04:01    Elapsed Time - 0:37
  unmounting /mnt/clone/boot
  unmounting /mnt/clone
===============================

See, it didn't ask me for anything.

You can leave the disk plugged in and run this on a cron job with options to keep it silent if you want.

sudo rpi-clone sda -u -q

Now it won't give you any output - when it's done, you'll get a command prompt back.

It's very fast and reliable.


Okay, I've backed up - but how do I restore?

Simple. Basically it's "swap and go".

Swap out your Micro SDHC cards and go back to where you were... I mean, sure you have to power the pi off, but yeah - do this:

sudo poweroff

Swap your disks

Take the Micro SDHC out of your pi and exchange it for the one in your card reader.

Power your pi on again

when it comes back online - just run

sudo rpi-clone sda

Click yes...

And then you're back to where you started.

If you want to be double tripple sure - you can always format the alternate disk again entirely and plug it back in and run

sudo rpi-clone sda

Let it format the drive and back up - and you're back in action in typically less than five minutes.


Documentation

rpi-clone has excellent documentation.

If there is something you want to do but I haven't covered it - go take a look at the documentation, or google it. rpi-clone is so popular people use it on systems that aren't even Raspberry Pis - typically you can find an example by googling.

If there really is something you want to do with rpi-clone and I didn't mention it here, please leave a comment below. Especially if you can't figure it out - I'll help you figure it out if I can.

Enjoy.


Credit: Photo by Jonah Pettrich on Unsplash