Whole backup via SSH of my Dietpi setup

Good evening everyone, @MichaIng

As the title suggests, I need to back up the SD cards for my various SBCs for peace of mind. To do this, I like to create an image file of the entire SD card, which I can then re-flash if a crash occurs.

To avoid opening the cases every time to retrieve the SD cards, I created a script that handles the task via SSH, saving the image to my laptop’s hard drive and keeping a maximum of three backups.

To use the script

nano ~/Bureau/Sbc-rpi-opi/your-board.sh

Copy and paste

#!/bin/bash

# ==============================================================================
# Configuration: Paths and Variables (Change these to match your setup)
# ==============================================================================
IP_SBC="192.168.1.xx"
NOM_SBC="Dietpi Your Board trixie "
# ==========================================

DOSSIER_SAUVEGARDE="/home/your-home/Bureau/Sbc-rpi-opi/Images-OS"
NOM_FICHIER="$NOM_SBC($(date +%d-%m-%Y)).img"
CHEMIN_COMPLET="$DOSSIER_SAUVEGARDE/$NOM_FICHIER"

echo "=== Starting optimized SBC backup ($IP_SBC) ==="
echo "Saving to: $CHEMIN_COMPLET"
echo "Please wait..."

# 1. Run backup with progress display and SSH cipher optimization (Powerline/PLC friendly)
ssh -C -c chacha20-poly1305@openssh.com root@"$IP_SBC" "dd if=/dev/mmcblk1 bs=4M status=progress" > "$CHEMIN_COMPLET"

echo "=== Backup completed successfully ==="

# ==============================================================================
# 2. Cleanup: Keep Only the 3 Most Recent Backups
# ==============================================================================
echo "Checking quota (max 3 images) for $NOM_SBC..."
cd "$DOSSIER_SAUVEGARDE" || exit

# List matching files sorted by modification time (newest first).
# 'tail -n +4' skips the 3 newest files and targets older ones for deletion.
ls -t "$NOM_SBC"*.img 2>/dev/null | tail -n +4 | while read -r ancien_fichier; do
    echo "Deleting oldest backup: $ancien_fichier"
    rm "$ancien_fichier"
done

echo "=== Operation completed! ==="

Why this script uses chacha20-poly1305 and SSH compression (-C)

When streaming a full SD card image over your local network, your main performance bottlenecks are usually network bandwidth and the SBC’s CPU encryption speed.

Here is why this specific SSH command (ssh -C -c chacha20-poly1305@openssh.com...) optimizes the backup process, especially over Powerline adapters (PLC / CPL) or Wi-Fi:

  • chacha20-poly1305 Cipher: By default, SSH often uses AES encryption (aes128-gcm or aes256-gcm). While powerful, AES relies heavily on dedicated hardware acceleration (AES-NI instructions), which many budget SBC CPUs lack. chacha20-poly1305 is a stream cipher designed to be extremely fast in pure software execution. It significantly reduces CPU overhead on your Raspberry Pi, Orange Pi, or Radxa, allowing it to process and stream the data much faster.
  • SSH Compression (-C): This enables on-the-fly gzip compression during the network transfer.
  • The Powerline (PLC / CPL) Benefit: Powerline connections are notorious for having fluctuating bandwidth and higher latency compared to dedicated Ethernet cables. By combining chacha20 (which frees up SBC CPU cycles) with -C compression (which shrinks the amount of raw data traveling through your electrical wires), you maximize throughput. You will get a much more stable and faster backup stream without choking your home network or your SBC.

This script is intentionally left without SSH keys configuration. When you run it, SSH will prompt you for the root password of your SBC. This acts as a manual safety check and verification step, ensuring you are fully aware when a full disk backup is being initiated over your network.

To make this file executable

chmod +x ~/Bureau/Sbc-rpi-opi/your-board.sh

To run the script and perform the backup

~/Bureau/Sbc-rpi-opi/your-board.sh

Consider using DietPi/.build/images/dietpi-imager at b44e24d4a7f161b636d9cae01bd22352ba641693 · MichaIng/DietPi · GitHub, which shrinks the filesystem and partition, and enables the fs_partition_resize serves to get it expanded again on next boot, xz-compression included. But it works only on offline drives/cards, which I would also highly recommend when generating a backup, or at least do mount -o remount,ro / while creating the image from a live system. Else there can be filesystem inconsistencies causing random issues, making the image unbootable if unlucky. Integrating host root drive image generation into dietpi-imager is actually something I am thinking about since a longer time. The R/O remount however is inevitable for this to be robust.

Transferring the data via common SSH client seems very fragile. Any other random I/O will render the image broken. If you really need to do this from the live system, write into a network drive, something which is made for file transfers. Could be an sshfs as well, with SSH server on the laptop in that case.

So far, I haven’t had any issues with my backups or with writing them back to my SD cards

And yes, the cards are in use when I back them up.
I also back up 4GB cards, so there’s no need to shrink the image size, and writing the 4GB back doesn’t take too long.

I might give dietpi-imager a try at some point, but for now, my script works perfectly for the three cards I have running.