Alpine Linux Local Backup on the Raspberry Pi 4

— 3 minutes — linux, snippets

The Alpine Wiki’s page about the Raspberry Pi is always going to be far more informative than Some Blogpost™. Please refer to it as the authorative text on this subject and disregard my incoherent ramblings.

I’m writing this because my first attempt at Alpine on my Pi was disastrous. I lost nearly all of my configuration when I rebooted since I didn’t properly setup my local backup for a diskless install. If you try to follow this post as a walkthrough, you’re gonna have a bad time. Instead, it’s mainly some things I had to deduce that I couldn’t easily find online.

Two links you should follow before continuing:

This is for a diskless installation without a loopback image (i.e. the root filesystem size is limited to RAM size). A diskless install, also called toram in other distributions, is a type of setup in which all system files are loaded into RAM at boot. I chose a diskless install because I wanted to extend the lifespan of my Pi’s SD card by reducing the read/write operations performed on it. All of my server’s public documents are also kept in RAM, which should amount to faster service as well.

Supposedly, you should be able to have Alpine reinstall your apk cache at boot automatically, but I haven’t had any luck getting this to work on my Pi.

As far as I can tell, if you restart alpine in a diskless installation, you’ll need to run apk -U upgrade.


My partition scheme:

According to the wiki, setup-alpine does not automatically handle internal partitions, so add local-backup and storage to /etc/fstab.

mkdir /media/local-backup
mkdir /media/storage

echo "/dev/mmcblk0p2 /media/local-backup ext4 noatime,ro 0 0" >> /etc/fstab
echo "/dev/mmcblk0p3 /media/storage ext4 noatime,rw 0 0" >> /etc/fstab

mount -a

mkdir /media/storage/apk


Note that local-backup is mounted read-only and storage is mounted read/write.

You could keep the cache somewhere on the root filesystem and back it up with lbu, but in my opinion that’s wasting precious RAM.

By default, lbu won’t watch and store /var or /home changes, so I ran the following:

lbu add /var/www /home/derek

(Consider making your home folder something like /media/storage/home/derek, in which case don’t add /home/derek to lbu. My home folder will mostly be small scripts, so I’m okay with keeping it in RAM)

Make sure you check lbu st and run lbu commit [-d] whenever making configuration changes.

Watching lbu for changes

To easily detect if lbu needs to commit changes, you can change the color of your name.

Add to your ~/.profile

# ~/.profile
nonzero_return() {
        [ $RETVAL -ne 0 ] && echo "($RETVAL)"

nonempty_lbu_st() {
	[ -n "$(doas lbu st)" ] && echo "9" || echo "10"

PS1="\[\033[38;5;\`nonempty_lbu_st\`m\]\u\[\e[m\]@\H:\[\033[38;5;14m\]\w\[\e[m\]\[\033[38;5;13m\]\`nonzero_return\`\[\e[m\]\\$ "

If you’re going to be a user other than root, you’ll also need to setup doas to allow yourself to run lbu st as superuser without password interaction.

# /etc/doas.d/doas.conf
permit nopass derek as root cmd lbu args st

Files that are frequently updated should be excluded from lbu to avoid needless signaling for a commit.

lbu ex ~/.ash_history ~/.bash_history ~/.viminfo

Final notes

Make sure to read the Alpine Linux wiki, but keep in mind that if you stray from the Raspberry Pi page, the information will nearly always pertain to a standard x86_64 ISO installation, not the Pi (although there is plenty of overlap).

And remember:

lbu commit