Just a simple backup solution aimed to allow 100% exact reproduction of all necessary files of a system on an ext4 file system.
This is by far not the fastest solution but it works and can still be expanded with encryption and other inter-device transformations.
As a performance hint, my USB backup drive has a normal throughput around 120 MB/s, iotop
reported that rsync ran at 33 MB/s for large files. There's definitely room for optimization, for example because every write I/O to the block device causes a timestamp update on the file itself.
file="/run/media/icedream/Backup/backup-laptop"
device="/dev/backup/$(basename "$file")"
source="/" # all files will be backed up from here
target="/mnt/backup/$(basename "$file")" # should be mounted somewhere in the ignored parts of the filesystem (see below)
ignore='{"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"}'
# Create loopback device
if [ ! -b "$device" ]; then
mkdir -p "$(dirname "$device")"
# Not so intelligent way to find next available loopback device ID
for i in $(seq 200 299); do
mknod "$device" b 7 $i && break || continue
done
# Connect with actual file
losetup "$device" "$file"
# ...or if you need encryption:
#losetup -e des "$device" "$file"
fi
# Set up ext4 file system, just to be done exactly once
mkfs.ext4 "$device"
# Mount
mkdir -p "$(dirname "$target")"
mount "$device" "$target"
# Synchronize files into backup filesystem
rsync -aAXv --delete --exclude="$ignore" "$source" "$target"
# Unmount and destroy device file
umount "$device"
losetup -d "$device"
rm "$device"