Kyle Rankin

Chief Security Officer
PGP ID: 0xB9EF770D6EFE360F
Fingerprint: 0DFE 2A03 7FEF B6BF C56F73C5 B9EF 770D 6EFE 360F
Librem Social

Latest posts by Kyle Rankin (see all)

Like many of my fellow Purism team members, I do a lot of internal testing on my Librem 5 phone and regularly need to backup my files and settings and restore them on a reset phone. One of the nice things about the Librem 5 is that since it runs PureOS, I have a wide range of backup and restore options at my disposal and can treat it like I was backing up a Librem laptop. On Android I typically had to root the device and trust a proprietary app for backups, or rely on Google to backup things to a Google account without knowing whether all of my settings would make it (and they never did). Backup and restore on a Librem 5 is secure and straightforward.

In my case I wanted to backup to a microSD card so I could insert it into a phone that was at factory settings, mount the disk, and then restore what I needed. I could have used a GUI backup tool, and perhaps I will in a future blog post, but as I’m very familiar with command-line backup tools due to my background as a systems administrator. I decided to use one of the oldest command-line backup tools that I knew would already be installed on the factory system: tar.

Even though I had reformatted the microSD card with an ext4 Linux file system, by using tar I didn’t have to worry about preserving permissions on the microSD card so in theory it could have any file system that supported large (> 4Gb) files. With fancy options like -M -tape-length=4096000 passed to tar to split up its backup into multiple 4Gb files, I could have even backed up to a traditional fat32 microSD card.

Get a List of Installed Packages

Before I did a backup, I first wanted to get a list of all of the packages I had installed on the phone so I could reinstall them as part of the restore. To do this I typed:

dpkg --get-selections | grep -v deinstall | cut -f1 > ~/pkglist

This dumped the list of installed packages, one line at a time, into /home/purism/pkglist.

Backup the Files

I had already been using my microSD card mounted at /mnt/sda1 (a directory I created) to store my music collection. The partition showed up as /dev/sda1 so I added the following line to /etc/fstab so it would mount at boot if it were present:

/dev/sda1 /mnt/sda1 auto auto,nofail

The auto,nofail combination of options made it so that it would mount the file system if it existed at boot, but if it didn’t exist, the boot process wouldn’t stall or error out. Of course if I didn’t already have that set up I could have just done it all manually:

sudo mkdir /mnt/sda1
sudo mount /dev/sda1 /mnt/sda1

Next, I changed to the / directory so the file paths would all be based off of it and ran the tar command. I decided to add the --one-file-system option for safety sake as I had made some links between my music collection on the microSD card and my home directory.

cd /
sudo tar --one-file-system -czf /mnt/sda1/chestnut_backup.tgz /etc /home /var/spool/cron/crontabs

Let’s talk about the directories I chose to backup. First I decided to backup the /etc directory because it contains the majority of the important system-level settings I cared about including my personal OpenVPN configuration at /etc/openvpn, my custom /etc/fstab file, and a few changes I had made to /etc/apt/sources.list to pull in staging packages for testing. Then I backed up my entire /home directory as it contained the bulk of my custom settings for my user. Finally I added /var/spool/cron/crontabs because I had made a few custom crontab additions to update podcasts automatically a couple times a day.

Restoring the Files

When I was ready to restore my settings to a “factory” reset phone, I inserted my microSD card and booted, then opened a terminal and created a directory to mount it to and mounted it manually:

sudo mkdir /mnt/sda1
sudo mount /dev/sda1 /mnt/sda1

Next I used tar to restore the settings from /etc along with my home directory and crontab:

cd /
sudo tar xf /mnt/sda1/chestnut_backup.tgz etc/apt/sources.list etc/openvpn home var/spool/cron/crontabs

Note that when I restored these files that I removed the first / symbol. That’s because the tar command I used to backup these files stripped that initial / from the path (this makes it safer to restore files to any directory you wish).

Restoring Installed Packages

Now that all of my files were in place, including the list of installed packages in /home/purism/pkglist I was ready to re-install all of the additional packages I had added:

sudo apt update
sudo apt install `cat /home/purism/pkglist`

I could have created a new list of packages on the factory system and only installed the difference between the two, but I knew apt would detect any packages it already had and only install what it needed to. This process took some time, and once it completed I then rebooted the phone so it could boot into the most recent kernel and desktop shell.

Conclusion

If you have ever performed a backup and restore on a Linux system, my process probably seems pretty familiar to you–and that’s the point! You can treat a Librem 5 phone like a standard Linux system and any know-how you already possess instantly translates over.

Discover the Librem 5

Purism believes building the Librem 5 is just one step on the road to launching a digital rights movement, where we—the-people stand up for our digital rights, where we place the control of your data and your family’s data back where it belongs: in your own hands.

Preorder now

Recent Posts

Related Content

Tags