It is easy to use btrfs with its send/receive to backup one snapshot to an external drive. But it is real work, if you want do do this for several snapshots repeatedly. Even when you want to make these snapshots differential. …
I did program this for my own backup, but it may be useful for others.
[HowTo] Backup btrfs snapshots with send/receive
Goals:
Simply a backup of a complete system (only the snapshots)
Back up all snapshots
Differential backup of each snapshot (fast)
Use as little storage space on the backup medium as possible (compressed)
Differential backup (when repeating the backup after a few days/weeks/months)
The backup medium can be used for backups of different computers
No goal:
Automatic management of backups by age
Automatically delete old backups when there is not enough space
Backup of the current state of the subvolume
Requirements:
BTRFS on both, the computer and the backup media
snapper layout of snapshots
Java 11 or newer on the computer
Recommended: pv installed
Back snap:
The Java program Backsnap saves ALL snapshots that have been specified to a backup medium. Therefore it uses btrfs send and btrfs receive
The 1st passed parameter points to the SOURCE path where the snapshots
are reachable. Snapper puts all snapshots in directories with ascending numbering. The actual snapshot inside this directory is simply called “snapshot”.
/.snapshots
/home/.snapshots
The 2nd parameter points to where the snapshots are to be saved. To do this, the backup medium needs to be mounted"special (subvol=/)". It needs a subvolume called @snapshots and a directory named for this PC (for example the hostname). The path to this directory is specified as the TARGET path for the backup.
/mnt/BACKUP/@snapshots/manjaro
/mnt/BACKUP/@snapshots/manjaro.home
/mnt/BACKUP/@snapshots/notebook
/mnt/BACKUP/@snapshots/notebook.home
Backsnap goes through all the numbered directories in the source path in ascending order and checks wether this directory already exists at the destination. If not, the snapshot will be send there. If possible, a previous snapshot is used as a “parent”.
Each time the program is called, all snapshots of ONE subvolume can be backed up corresponding to ONE configuration of snapper.
Recommendations:
Create a btrfs Volume on your external drive with a subvolume called @snapshots
Create a shell script called /usr/local/bin/backup that handles the entire backup.
Mount the backup volume with options subvol=/,compress=zst:9
I am willing to adapt this to support timeshift if desired
P.S. The responsibility for backups never lies with a program, but always with the user!
Save this under /usr/local/bin/Backsnap and make it executable. Do not name it Backsnap.java !
chmod a+x /usr/local/bin/Backsnap
Make a backup-script at /usr/local/bin/backup like the following example:
#!/bin/sh
# Backup to USB-disk
UUID=03417033-3745-4ae7-9451-efafcbb9124e
BACKUP=/mnt/BACKUP
# mount /mnt/BACKUP
mount -o subvol=/,compress=zstd:9 /dev/disk/by-uuid/$UUID $BACKUP
# use Hostname
NAME=$HOST
# is this dir available ?
[ -d $BACKUP/@snapshots/$NAME ] || { echo "Das mounten war wohl nicht erfolgreich"; exit; }
# backup snapshots of /
Backsnap /.snapshots $BACKUP/@snapshots/$NAME
sync
# Backup snapshots of /home
Backsnap /home/.snapshots $BACKUP/@snapshots/$NAME.home
sync
umount $BACKUP
btrbk seems to be a really good solution for btrfs snapshots and backups.
It’s a bit heavy for me
I’m already running snapper, which manages the snapshots with its own strategy per configuration.
It was about quickly backing up ALL the local snapshots onto an external drive that is momentary connected.
There should be no duplicates,
the backup drive should be able to be used for several computers and, above all,
it should be able to hold as many snapshots as possible. (compression)
I’ve read that before. Correct in principle, but a btrfs snapshot is as good as any other In-system backup. Btrfs can bring a high level of security against data loss for home users. With some additional work, an external backup for btrfs can also be achieved.
Some improvements:
command-line only
only 2 parameters to do a backup
supports backup of other computers on the network over ssh
no config necessary
batchfile for easy backup suggested
batchfile example included
executable jar (when java is installed )
(It is written in java not because java is the best, but because i do “speak” java)
I think send/receive like http is for files, send/receive like btrfs is for directory . The purpose is not only for backup, ít can become www version 2.0