It is not clear exactly what you want to do.
There are programs which backup running Pi and create an image - search this site for details.
The Pi has a SD Card copier which will copy a running system.
You could use rsync to sync a master system to another.
All of these suffer from the same problem. Based on 50 years experience and established practice any system based on a single backup is just going you a false sense of security.
Any automated backup risks copying any errors.
You need a proper system to make multiple backups and a process to rotate them.
Some time ago I wrote a rsync script to keep my Pi in sync.
I haven't used in years but it may provide some guidelines.
It makes assumptions e.g. user pi
so will need tweaking.
#!/bin/bash
# script to synchronise current Pi from remote.
# Does not DELETE unmatched files
# 2017-04-10
# 2020-04-30
CURRENT_HOSTNAME=$(cat /etc/hostname)
echo "Current Name" $CURRENT_HOSTNAME
REMOTE_HOSTNAME="MilliwaysPi4.local"
echo "Remote Name" $REMOTE_HOSTNAME
# Resolve .local address
RemotePi=$(getent hosts $REMOTE_HOSTNAME | awk '{ print $1 }')
if [ $RemotePi ]; then
echo $RemotePi
echo "Commencing Copy"
# -a archive mode; equals -rlptgoD (no -H,-A,-X)
# -H preserve hard links
# -u skip files that are newer on the receiver
sudo rsync -aHu --exclude-from=rsync-dup-exclude.txt --exclude='/SD?' --exclude='/SD??' pi@$RemotePi:/ /
else
echo "RemotePi not available"
fi
This uses rsync-dup-exclude.txt
to exclude unwanted stuff.
/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