0

I have a number of SD Cards running Raspberry Pi OS and want to keep these in sync.

Milliways
  • 54,718
  • 26
  • 92
  • 182

1 Answers1

0

It is possible (by many different methods) to create an installable image, which can be used to create copies of SD Cards (I use Backup image of SD Card ).
This works, but is slow and may require some customisation of the target.

I use the following script to synchronises necessary files from running OS on Pi to SD Card in SD Card reader on the Pi.
NOTE the SD Card must be already formatted with partitions containing OS.

This excludes 'cmdline.txt' and '/etc/fstab' - which is ESSENTIAL to ensure the SD Card boots - this, of course assumes the existence of correct matching files on the SD Card.

(You will also note a couple of other exclusions, but these can be removed.)

#!/bin/bash
# script to synchronise Pi files to SD Card
# 2019-08-12

# This script synchronises necessary files from running OS on Pi to SD Card
# SD Card (already formatted with partitions containing OS) in SD Card reader on Pi

# NOTE does not copy cmdline.txt or /etc/fstab

# NOTE file rsync EXCLUDE_FILE is required
# use 'rsync-dup-exclude.txt' as we WANT logs & .bash_history to be copied
# EXCLUDE_FILE='rsync-exclude.txt'
EXCLUDE_FILE='rsync-dup-exclude.txt'

echo $EXCLUDE_FILE
BOOT_MOUNT='/mnt/SDA1'
ROOT_MOUNT='/mnt/SDA2'

# Check/create Mount Points
if [ ! -e $BOOT_MOUNT ]; then
    mkdir $BOOT_MOUNT
fi
if [ ! -e $ROOT_MOUNT ]; then
    mkdir $ROOT_MOUNT
fi
    echo $BOOT_MOUNT  $ROOT_MOUNT
# Mount Partitions
if ! $(mountpoint -q $BOOT_MOUNT); then
    mount /dev/sda1 $BOOT_MOUNT # mount partition containing boot files
fi
if ! $(mountpoint -q $ROOT_MOUNT); then
    mount /dev/sda2 $ROOT_MOUNT # mount root partition containing OS files
fi

if $(mountpoint -q $BOOT_MOUNT) && $(mountpoint -q $ROOT_MOUNT); then
    echo $BOOT_MOUNT  $ROOT_MOUNT
    echo "Synchronise boot files to SD Card"
    rsync -a --delete-during --exclude='cmdline.txt' --exclude='/SD?' --exclude='/SD??' /boot/ $BOOT_MOUNT/
    echo "Synchronise OS files to SD Card"
    rsync -axH --progress --delete-during --exclude='/etc/fstab' --exclude='WolframEngine' --exclude='scratch2' --exclude-from=$EXCLUDE_FILE / $ROOT_MOUNT/
    umount $BOOT_MOUNT
    umount $ROOT_MOUNT
else
    echo "Backup Card not available or not writable"
fi

NOTE file rsync EXCLUDE_FILE is required either rsync-dup-exclude.txt

/proc/*
/sys/*
/dev/*
/boot/*
/tmp/*
/run/*
/mnt/*/*
/var/cache/*

/boot/cmdline.txt
/etc/fstab

.Trashes
._.Trashes
.fseventsd
.Spotlight-V100
.DS_Store
.AppleDesktop
.AppleDB
Network Trash Folder
Temporary Items

.cache
/etc/fake-hwclock.data
/var/lib/rpimonitor/stat/
/etc/hostname
/etc/hosts

OR rsync-exclude.txt

/proc/*
/sys/*
/dev/*
/boot/*
/tmp/*
/run/*
/mnt/*/*
/var/log/*
/var/cache/*

.Trashes
._.Trashes
.fseventsd
.Spotlight-V100
.DS_Store
.AppleDesktop
.AppleDB
Network Trash Folder
Temporary Items

.bash_history
.cache
/etc/fake-hwclock.data
/var/lib/rpimonitor/stat/
/etc/hostname
/etc/hosts
Milliways
  • 54,718
  • 26
  • 92
  • 182