Create a backup image from your working PixieBoard setup.

Jose Ruiz
PixieBoard
Published in
8 min readAug 1, 2018

In all projects there comes a point where developers feel the need to backup their system’s setup (and not just the code), either to distribute it, install it into multiple devices or just to have a checkpoint that you know you can come back to if something breaks.

In this guide, I will walk you through each and every step from having a working setup on your PixieBoard to creating an image file that you can use later to restore your system. The best part is that the resulting image will only be the size of the ocuppied file system, no blank space will present. Meaning you don’t need to worry about wasting storage space in your hard drive.

What you’ll need.

Even though all the steps in this guide can be executed from any Linux box (Arch Linux, specifically), I assume that the working setup is a PixieBoard running Arch.

You will need two microSD cards, one is your working setup in which you will boot from and perform all tasks to create the backup. The second one is the system that will be actually be backed up. This will allow you to have a working setup with “General Utilities” without having to install extra stuff in your project’s device.

Get a copy of the microSD you want to backup

If you already have the system you want to backup in a bootable microSD card, you can skip this step and jump to the next section.

If, however, the system you want to backup is the one you are using right now, you can follow these instructions to create a bootable clone of your primary microSD card. We will be using a very useful script created by Bill Wilson that is available on his public github. Kudos to Bill for this script.

First, you will need to make sure you have a bunch of stuff installed:

sudo pacman -S git parted rsync dosfstools

Now, clone and install from Bill’s repo:

git clone https://github.com/billw2/rpi-clone.git
cd rpi-clone
sudo cp rpi-clone /usr/local/sbin/sys-clone
sudo cp rpi-clone-setup /usr/local/sbin/sys-clone-setup

It’s time to make a clone from your setup. First insert the second SD Card into the secondary SD slot. The options for the script will make the necessary adjustments to the clone so it’s bootable for PixieBoard:

sudo sys-clone -f -v -e mmcblk2p /dev/mmcblk3

You will get asked a couple of questions by the script:

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

Now, you can go get a cup of coffee. This may take a while, especially if you are using a large (and slow) microSD card.

Once the script finishes it will ask you if you are ready to unmount the clone. Hit enter and move on to the next section. At this point, you now have an exact, bootable copy of your primary microSD card. (I do recommend trying to boot it to make sure it is working correctly).

Get the software tools you need.

As always, you want to make sure that you have everything installed beforehand. First, lets install Arch install scripts with Pacman:

sudo pacman -S arch-install-scripts

Now, we need to install zerofree, a tool that will allow us to lower the system’s entropy for better compression. This tool is not available from the pacman repos, so we’ll need to install it from the AUR (I recommend you read about Arch Universal Repository if you are new to Arch). First, lets install yoaurt.

sudo pacman -S yaourt base-devel

Hit enter to install all members of the base-devel group.

Now, lets install zerofree:

yaourt zerofree

Note: If you got an error message from this step like the one bellow you will need to create a symlink to fix it:

package-query: error while loading shared libraries: libalpm.so.10: cannot open shared object file: No such file or directory

Just have libalpm.so.10 (a symlink) point to libalpm.so.11:

sudo ln -sf /lib/libalpm.so.11 /lib/libalpm.so.10

Type again yaourt zerofree. It should start the program.

In yaourt, you get a list of packages that match your search. In this case there is only one package, so select 1 and hit enter. And then, a couple of messages stating that the package is not available for this architecture. Don’t worry, we just need to add the architecture to the PKGBUILD file and compile the program. To do this, say YES [Y] to edit PKGBUILD and (if prompted) what program you want to use for this, just write nano.

Edit PKGBUILD with nano to add armv7h as supported architecture.

Now, modify the line with the “arch” statement [Line 8 in my example], to add ‘armv7h’.

arch=('i686' 'x86_64' 'armv7h')

Exit nano with CTL+X, say YES when prompted if you want to save your changes and hit ENTER to save all modifications back to PKGBUILD file. You will be asked again if you want to edit PKGBUILD. Answer NO this time and YES to continue building zerofree. The compilation process will start now, you will be asked a couple of times to confirm the installation of the package when the binary is ready. Answer YES.

Clean up your system

If you want to use the least storage space possible, the first thing you need to do is to purge your system from unnecessary files and caches that are there for performance. If you don’t mind keeping these files you can skip this step.

First, you need to mount your secondary microSD. This is done in two steps, but first you need to make sure the mounting point exists in /mnt:

sudo mkdir /mnt

Now, mount both partitions. Make sure to follow these steps in the correct order:

sudo mount /dev/mmcblk3p2 /mnt
sudo mount /dev/mmcblk3p1 /mnt/boot

Now you are going to change root so we can modify some features in the secondary microSD device. With the arch-chroot command, you will change the point where the current process (and its children) is pointing to. You can learn more about it here.

sudo arch-chroot /mnt

Now, lets clean up the system (you are running as root, so no need to sudo):

pacman -Scc
rm /root/.bash_history

Answer YES to both questions.

In the next section of this guide we will make the partition and filesystem smaller. However, if you restore this backup image sometime in the future, you’ll want your system to be able to access all the storage space available in your microSD card. For this, we have a script that will run the first time you boot your system, just run this command to enable the unit:

systemctl enable pixie-install-config

That’s it. Return to your normal session with exit.

Resize partition and filesystem

It’s time to shrink the filesystem to the minimum possible, so you save storage space. To do this, start by checking (and repairing) your linux drive.

Unmount both partitions (make sure to do this in the correct order):

sudo umount /mnt/boot
sudo umount /mnt

And check the second partition on your drive:

fsck -fy /dev/mmcblk3p2

Now, resize the filesystem to the minimum:

sudo resize2fs -M /dev/mmcblk3p2

IMPORTANT: Keep track of the resulting number of blocks, you will need this information in a later step. We’ll call this number BLOCKS.

In my case, the number of blocks is 279741.

Check the partition again:

fsck -fy /dev/mmcblk3p2

Now, lets shrink the partition.

Call fdisk with your target disk as an argument:

sudo fdisk /dev/mmcblk3

Type ‘p’ to print the partition table. Take note of the START sector of the second partition (Currently is 139264 for all PixieBoard installations, but this can change in the future).

Now, delete the second partition with the following commands:

d → This will start the delete process.

2 → We want to delete the second partition.

And create a new one. But first we need to make some calculations. Remember the number of BLOCKS resulting from resizing the filesystem? We need to use that number to calculate the number of sectors. In our case, blocks are 4KB and sectors are 512b, so the number of sectors will be:

SECTORS = (BLOCKS * 4096) / 512

(In my case, the resulting number is SECTORS=2237928).

Now, lets create a new partition using this piece of information:

n → Start the new partition process.

p →To make it a primary partition.

2 →We want to make this the second partition in the disk.

139264 →This is the START sector of our new partition.

SECTORS →This is the size of the partition. Notice the ‘+’ symbol to make it relative to the START.

n → Don’t remove the signature

w →Write all changes to the partition table.

Note the ‘+’ symbol when entering the Last Sector.

Check the partition again:

fsck -fy /dev/mmcblk3p2

Create a compressed image.

Finally, let’s create a compressed backup. For this, first lets use zerofree to improve the compression efficiency:

sudo zerofree -v /dev/mmcblk3p2

Now, we’ll need to know how many sectors to backup, for this, list the partition table:

sudo fdisk -l /dev/mmcblk3
Take note of END sector from the partition table of your disk.

Take note of the END sector in the second partition. In my case is 2377192. Sectors are 512b and we will use the dd tool with block size of bs=16M, so the calculation we need to do is:

COUNT = END * 512 / 16777216

(In my case, COUNT= 72.5 →73) Make sure to round up this number.

Install the compression tools if you don’t have them:

sudo pacman -S zip unzip gzip

Finally, let’s create the backup image. I’ll write down a couple of options, just follow what is best for you.

a) If you have enough space in your host microSD card, you can just use the following command, where COUNT is the number you just calculated (note is all in one single line).

If you plan to use Linux to restore your image, I recommend using gzip which is more efficient (Remember you can use your PixieBoard for this):

sudo dd if=/dev/mmcblk3 bs=16M count=COUNT | gzip -c > name-your-backup.img.gz

If you will use Windows or Mac, then use zip:

sudo dd if=/dev/mmcblk3 bs=16M count=COUNT | zip > name-your-backup.img.zip

b) If you don’t have enough space in your working setup or just want to put your file in a server you have ssh access to, you can do everything in a single line, like so:

dd if=/dev/mmcblk3 bs=16M count=COUNT | gzip -c | ssh user@local dd of=backup.img.gz

Final words

So, even though this guide is quite long I think it can be very useful for the reader to learn how to crate backups efficiently. I tried to split it in several steps that I hope will provide the insight of doing some advanced tasks regarding the storage units available in PixieBoard.

Don’t hesitate to comment or shoot me an email to jose.ruiz@codeandmodules.com if you think there are things that would improve this process or if you have any questions.

--

--

Jose Ruiz
PixieBoard

Also know as Chepe. Has a passion for technology, mathematics and motorcycles. And guitars.