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-poly1305Cipher: By default, SSH often uses AES encryption (aes128-gcmoraes256-gcm). While powerful, AES relies heavily on dedicated hardware acceleration (AES-NI instructions), which many budget SBC CPUs lack.chacha20-poly1305is 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-Ccompression (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