Raspberry Pi Backup

This post covers implementing an effective backup strategy for your Raspberry Pi

Having a plan for how to recover when things break down is essential when you depend on your server.

This is part 2 of 4 in a mini series where we will configure a headless Raspberry Pi 4 B as an efficient home server, capable of hosting Network Attached Storage (NAS), TimeMachine and Plex Media Server.
Through this series we will also look at how we can make automatic backups to keep uor data safe.

Prerequisites

Hardware Requirements

Software Requirements



In this post I assume you have read part 1 in this series and thus have a Raspberry Pi with Desktop you can SSH in to @ raspberrypi.local.

Backing up the Raspberry Pi

The easiest way to get back up and running after it’s gone all wrong, is having a full bootable system image ready to flash on to your microSD card with Raspberry Pi Imager.

On your Raspberry Pi, you can create such an image using the dd command to copy the current running installation to an .img file on some external storage plugged in to the Raspberry Pi’s USB port.


Setting up the external storage

First you need to plug your external storage in to the Raspberry Pi’s USB port.

You can use the lsblk tool to get at list of all the block devices currently attached to your Raspberry Pi, and their mount points using this command:

lsblk -f

[output]

pi@raspberrypi:~ $ lsblk -f
NAME        FSTYPE  FSVER LABEL       UUID                                 FSAVAIL FSUSE% MOUNTPOINT
sda                                                                                       
├─sda1      vfat    FAT32 EFI         D2D7-4855                                           
├─sda2      hfsplus       Monterey    99b629a1-2994-45cf-b8c1-cff6d7450694                
├─sda3      hfsplus       Ventura     3516e24d-84c2-4f9e-9559-71de730a7277                
├─sda4      ext4    1.0   TimeMachine fc924351-26c8-4c01-a25f-e44a6869c5f1  614.6G    25% /media/pi/TimeMachine
└─sda5      ext4    1.0   NAS         665507c3-aee6-41bc-8be8-3594a65468d9    1.3T    45% /media/pi/NAS
mmcblk0                                                                                   
├─mmcblk0p1 vfat    FAT32 boot        444F-BE04                             200.3M    21% /boot
└─mmcblk0p2 ext4    1.0   rootfs      665507c3-aee6-41bc-8be8-3594a65468d9   19.4G    28% /
pi@raspberrypi:~ $ 

mmcblk0 is the microSD card with your full installation on it that you wish to backup.
sda is my SSD with 5 partitions on it. In this post I will use partition sda5 labeled NAS mounted at /media/pi/NAS as the destination for the backup.

Because you installed Raspberry Pi with Desktop (in part 1), removable media will be auto mounted to /media/pi by the the pcmanfm desktop process.

pcmanfm uses udisksctl on the backend to mount your drive, so you can also mount the sda5 partition manually like this:

udisksctl mount -b /dev/sda5

Backing up the microSD card

With your drive properly mounted, you can use mkdir to create a directory to store the backup:

sudo mkdir /media/pi/NAS/Raspberry/BACKUPS

Then make your first backup of your mocroSD card with dd. This will take some time:

sudo dd if=/dev/mmcblk0 of=/media/pi/NAS/Raspberry/BACKUPS/'RaspberryPi-(year-month-date).img' bs=1M

After some time, you should have the file RaspberryPi-(year-month-date).img in your backup directory. the .img file will be the full size of your microSD card, in my case ca. 31 GB.


Compressing the backup with PiShrink

You can then use a script called PiShrink to shrink the .img files size and compresses it with gzip. PiShrink also modifies your image so that it will expand itself to take all the available space on your microSD on first boot.

This is really good because if your current microSD card gives up, your new card might not be the same size as the old one.


Install PiShrink with wget, chmod and mv:

wget https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh
chmod +x pishrink.sh
sudo mv pishrink.sh /usr/local/bin

Then shrink your image like this. Again, this will take some time:

sudo pishrink.sh -z "/media/pi/NAS/Raspberry/BACKUPS/RaspberryPi-(year-month-date).img"

This will leave you with a significantly smaller file RaspberryPi-(year-month-date).img.gz, in my case ca. 4.6 GB.

Automating the process

The backup process can easily be scripted, so it happens periodically on a cadence to your liking.

First, create a new file for the rsync backup script with touch and set execution permissions with chmod:

touch ./Documents/sd-card-backup.sh
chmod +x ./Documents/sd-card-backup.sh

Edit the file in nano:

nano ./Documents/sd-card-backup.sh

And paste the following (make sure the SD_CARD and BACKUP_DIRECTORY variables are correct for you)

#!/bin/bash

CURRENT_DATETIME=`date +"%Y-%m-%dT%H-%M-%S"`
SD_CARD="/dev/mmcblk0"
BACKUP_DIRECTORY="/media/pi/NAS/Raspberry/BACKUPS"
BACKUP_FILENAME="${BACKUP_DIRECTORY}/RaspberryPi-(${CURRENT_DATETIME}).img"

# Exit script if Backup Directory does not exist
if [ ! -d "${BACKUP_DIRECTORY}" ]; then
  echo "${BACKUP_DIRECTORY} does not exist"
  exit 1
fi

# Create dump of SD Card in Backup Directory
sudo dd if=${SD_CARD} of=${BACKUP_FILENAME} bs=1M status=progress

# Shrink backup image with PiShrink
sudo pishrink.sh -z ${BACKUP_FILENAME}

# Delete backups older than 365 days
find ${BACKUP_DIRECTORY} -maxdepth 1 -name "*.img.gz"  -type f -mtime +365  -delete

Open crontab with the commend:

crontab -e

At the end of the document, type the frequency of script execution in the following manner

0 0 1 */1 * /home/pi/Documents/sd-card-backup.sh

The above line means that the script with run At 00:00 on day-of-month 1 in every month. You can configure this according to your needs. I recommend using crontab.guru to get the right settings for you.

Sources

Christophe Knage · 2023-02-10