Universal backup script for LVM volumes and regular directories. It supports
rsync and tar backups with include, exclude, or all filtering
rules while preserving extended filesystem metadata.
For a more comprehensive backup solution, consider restic. It provides encrypted backups, efficient transfers of changed data, integrity checks, and support for a wide range of local and remote storage backends.
Choose SafeData when you want direct control over familiar backup tools:
- Standard backup formats: keep files in an rsync directory tree or a tar
archive and restore them with standard
rsyncortarcommands, without a SafeData-specific repository format or restore tool. For rsync backups, use the documented--fake-superrestore options to recover privileged metadata. - Built-in LVM snapshot workflow: back up an LVM volume from a temporary
snapshot while continuing to use the original volume. Regular directories
are also supported through the
folder_*methods. Applications that need coordinated backups still require their own preparation. - Filesystem metadata preservation: retain ownership, permissions, ACLs, extended attributes, hard links, and sparse files with the documented backup and restore options.
- Explicit selection rules: choose included paths, excluded paths, or the whole filesystem using plain-text rules and transfer backups over SSH.
SafeData fits Linux systems where transparent backup files and an explicit LVM/rsync/tar workflow matter more than an integrated backup repository.
One script, three rule modes:
- INCLUDE mode - backup only listed items
- EXCLUDE mode - backup everything except listed items
- ALL mode - backup everything visible on the selected filesystem (no filters)
./safedata.sh <RULES_FILE> <BACKUP_METHOD> <VOLUME1> [VOLUME2 ...]The script requires root privileges and automatically restarts itself with
sudo while preserving all SAFEDATA_* environment variables. Run it as your
regular user as shown below instead of prefixing the command with sudo.
Mode is detected from filename pattern. Rules files are stored in ./rules/ directory.
INCLUDE mode:
*_include.rulesor*_included.rulesorincluded.rules
EXCLUDE mode:
*_exclude.rulesor*_excluded.rulesorexcluded.rules
ALL mode:
*_all.rulesorall.rules
rsync- Rsync with timestamp (new backup each time)rsync_notimestamp- Rsync into a stable directory without timestamped versionstar- Tar archive with timestampfolder_rsync- Non-timestamped rsync without LVM snapshotfolder_tar- Tar without LVM snapshot
rsync_notimestamp and folder_rsync update an existing destination but do not
delete remote files that have disappeared from the source.
- LVM volume name (e.g.,
lv_home,lv_var) - Directory path (e.g.,
/boot) for folder_* methods
SafeData creates backups but does not provide a separate restore command. Restore
data directly with rsync or tar, depending on the backup method used. Always
use the metadata options shown below: rsync privileged metadata is encoded using
--fake-super and would otherwise not be restored. The commands use the
environment variables from the Configuration section.
Restore into an empty temporary directory first. Verify its contents before copying data to the original filesystem.
Backup:
# Back up only paths listed in included.rules
./safedata.sh included.rules rsync_notimestamp lv_homeRestore:
# Create a temporary restore directory
sudo mkdir -p /mnt/restore/lv_home
# Restore the non-timestamped rsync backup
sudo rsync -aHAXS --numeric-ids -M--fake-super \
-e "ssh -i ${SAFEDATA_SSH_KEY_PATH} -o UserKnownHostsFile=${SAFEDATA_SSH_KNOWN_HOSTS_PATH} -p ${SAFEDATA_SSH_PORT:-22}" \
"${SAFEDATA_REMOTE_SSH_USER}@${SAFEDATA_REMOTE_SSH_HOST}:${SAFEDATA_REMOTE_BASE_DIR}/lv_home/" \
/mnt/restore/lv_home/Backup:
# Back up everything except paths listed in excluded.rules
./safedata.sh excluded.rules tar lv_homeRestore:
Replace <hostname> and <timestamp> with the values in the archive name.
# Create a temporary restore directory
sudo mkdir -p /mnt/restore/lv_home
# Download and extract the selected tar archive
ssh -i "${SAFEDATA_SSH_KEY_PATH}" \
-o "UserKnownHostsFile=${SAFEDATA_SSH_KNOWN_HOSTS_PATH}" \
-p "${SAFEDATA_SSH_PORT:-22}" \
"${SAFEDATA_REMOTE_SSH_USER}@${SAFEDATA_REMOTE_SSH_HOST}" \
"cat '${SAFEDATA_REMOTE_BASE_DIR}/<hostname>_lv_home_<timestamp>.tar.gz'" \
| sudo tar --extract --gzip --same-owner --same-permissions --numeric-owner \
--acls --xattrs --xattrs-include='*' --selinux --sparse \
-C /mnt/restore/lv_home/Backup:
# Back up the complete lv_root volume without filters
./safedata.sh all.rules tar lv_rootRestore:
Replace <hostname> and <timestamp> with the values in the archive name.
# Create a temporary restore directory
sudo mkdir -p /mnt/restore/lv_root
# Download and extract the complete tar archive
ssh -i "${SAFEDATA_SSH_KEY_PATH}" \
-o "UserKnownHostsFile=${SAFEDATA_SSH_KNOWN_HOSTS_PATH}" \
-p "${SAFEDATA_SSH_PORT:-22}" \
"${SAFEDATA_REMOTE_SSH_USER}@${SAFEDATA_REMOTE_SSH_HOST}" \
"cat '${SAFEDATA_REMOTE_BASE_DIR}/<hostname>_lv_root_<timestamp>.tar.gz'" \
| sudo tar --extract --gzip --same-owner --same-permissions --numeric-owner \
--acls --xattrs --xattrs-include='*' --selinux --sparse \
-C /mnt/restore/lv_root/Backups created with the rsync method have a timestamp in their directory
name. Replace <timestamp> with the required backup version.
Backup:
# Create a new timestamped backup of the complete lv_home volume
./safedata.sh all.rules rsync lv_homeRestore:
# List the available lv_home backup versions
ssh -i "${SAFEDATA_SSH_KEY_PATH}" \
-o "UserKnownHostsFile=${SAFEDATA_SSH_KNOWN_HOSTS_PATH}" \
-p "${SAFEDATA_SSH_PORT:-22}" \
"${SAFEDATA_REMOTE_SSH_USER}@${SAFEDATA_REMOTE_SSH_HOST}" \
"ls -1d '${SAFEDATA_REMOTE_BASE_DIR}'/lv_home_*"
# Create a temporary restore directory
sudo mkdir -p /mnt/restore/lv_home
# Restore the selected timestamped backup
sudo rsync -aHAXS --numeric-ids -M--fake-super \
-e "ssh -i ${SAFEDATA_SSH_KEY_PATH} -o UserKnownHostsFile=${SAFEDATA_SSH_KNOWN_HOSTS_PATH} -p ${SAFEDATA_SSH_PORT:-22}" \
"${SAFEDATA_REMOTE_SSH_USER}@${SAFEDATA_REMOTE_SSH_HOST}:${SAFEDATA_REMOTE_BASE_DIR}/lv_home_<timestamp>/" \
/mnt/restore/lv_home/# File can be empty or contain only comments
# In ALL mode, all rules are ignoredThis mode backs up everything visible on the selected source filesystem. Other mounted filesystems and bind-mounted content must be backed up separately.
# Only these items will be backed up
Documents/report.pdf
Pictures/family.jpg
.ssh/configRules:
- Relative paths from volume root
- No leading/trailing slashes
- Explicit paths work with both
rsyncandtar - Parent directories of explicit paths are automatically included for
rsync - Wildcard includes are method-dependent: with
rsync, include a parent path such asDocuments/*.pdf; use explicit paths for predictabletarbackups
# These items will NOT be backed up
.cache
*.tmp
lost+foundRules:
- Relative paths
- A leading slash anchors a pattern to the root of the backup source
- Without a leading slash, a pattern may match at any directory level
- Do not use trailing slashes
- Wildcard syntax and matching follow the selected backup tool (
rsyncortar) - Everything else will be backed up
Configure SafeData with environment variables. Keep host-specific values in your shell, systemd unit, NixOS module, or another private deployment layer.
export SAFEDATA_SSH_KEY_PATH="/path/to/backup_key"
export SAFEDATA_SSH_KNOWN_HOSTS_PATH="/path/to/known_hosts"
export SAFEDATA_REMOTE_SSH_USER="backup-user"
export SAFEDATA_REMOTE_SSH_HOST="backup.example.com"
export SAFEDATA_REMOTE_BASE_DIR="/remote/backup/safedata"
# Optional: wait for a busy snapshot device to be released after unmounting.
export SAFEDATA_SNAPSHOT_CLEANUP_TIMEOUT=300
# Optional defaults:
export SAFEDATA_VG_NAME="vg_main"
export SAFEDATA_LVM_SNAP_SIZE="80G"
export SAFEDATA_LOG_FILE="$HOME/.local/share/logs/safedata/activity.log"
export SAFEDATA_SSH_PORT="22"
export SAFEDATA_SSH_CONNECT_TIMEOUT="10"
export SAFEDATA_SSH_RETRY_COUNT="6"
export SAFEDATA_SSH_RETRY_DELAY="15"
export SAFEDATA_AC_CHECK_INTERVAL="10"Required variables are SAFEDATA_SSH_KEY_PATH,
SAFEDATA_SSH_KNOWN_HOSTS_PATH, SAFEDATA_REMOTE_SSH_USER,
SAFEDATA_REMOTE_SSH_HOST, and SAFEDATA_REMOTE_BASE_DIR.
SAFEDATA_SNAPSHOT_CLEANUP_TIMEOUT accepts an integer from 1 to 86400 seconds
and defaults to 300. Empty or invalid values are rejected before creating logs
or starting a backup. Only the LVM error contains a filesystem in use is
retried. A monotonic deadline includes time spent in failed LVM commands and
is shared with subsequent exit cleanup. An already running LVM command is not
interrupted by this deadline; allow additional time in the service stop timeout
(for example, 360 seconds for the default budget). A persistent failure keeps
the snapshot and reports diagnostics instead of deleting other mounts.
The directory specified by SAFEDATA_REMOTE_BASE_DIR must already exist on the
remote server. The SSH user must be able to write files and user extended
attributes there.
Before every rsync run, SafeData creates and removes a small hidden probe in the
configured target. The backup starts only if the probe confirms that remote
--fake-super metadata can be stored successfully.
- Script locks the selected VG/LV, checks free VG extents, and creates a uniquely named LVM snapshot
- Mounts snapshot to a private temporary directory under
/mnt/safedata.* - Backs up data from snapshot (consistent point-in-time)
- Unmounts and removes snapshot
Advantage: System can run normally while snapshot captures the state at a specific moment.
Before creating the mount directory or snapshot, SafeData checks that the VG
has enough free extents for SAFEDATA_LVM_SNAP_SIZE, rounded up to the VG extent
size. Sizes use the same binary units as LVM (case insensitive; MiB when no unit
is given). Insufficient space or an unreadable capacity report stops the run
with an error in the normal log; no snapshot is created or automatically deleted.
Other processes can still allocate space after this check, so lvcreate remains
the final authority and its failure is handled by the usual owned-resource cleanup.
The lock applies to the selected VG/LV for the duration of the run. Cleanup only handles the snapshot and mount created by that process. If unmounting fails, the snapshot is retained and the run exits with an error. A completion message is issued only after successful cleanup. A snapshot or mount left behind after an error requires administrator inspection.
If LVM reports contains a filesystem in use after unmounting, SafeData retries
removal once per second within SAFEDATA_SNAPSHOT_CLEANUP_TIMEOUT (300 seconds
by default), sharing the monotonic deadline with the exit handler and counting
time spent in LVM commands. Other removal errors fail immediately. On failure, the log
includes LVM state, the device open count, and visible mount namespace holders.
Diagnostic failures do not make cleanup successful. Later runs never delete
old snapshots based only on their names.
On HUP, INT, TERM, or unexpected exit, the transfer process group is
stopped before snapshot cleanup begins. Cleanup cannot run after SIGKILL or
a power failure.
Tar transfers write to a unique file ending in .tar.gz.partial. The file is
renamed to .tar.gz only after both tar and SSH complete successfully. An
incomplete file retains the .partial suffix and is not a completed backup.
The archive name also includes a run identifier after the timestamp to avoid
overwriting another archive created in the same second. Rules are passed as
arguments rather than shell code, so names containing spaces retain their meaning.
The package runs regression tests during the build. The tests use only temporary files, substitutes for SSH/LVM commands, and their own helper processes:
python3 -B -m unittest discover -s tests -v
nix flake check --no-update-lock-fileThe tests do not verify access to a particular NAS or the restorability of an existing backup.
Rsync backups use -aHAXS --numeric-ids to preserve standard Unix metadata,
hard links, ACLs, extended attributes, numeric ownership, and sparse allocation.
Because the remote SSH user is not expected to be root, --fake-super stores
privileged metadata in user.rsync.* extended attributes. The remote filesystem
must support user xattrs, and restoration must use -M--fake-super as shown in
the examples.
Tar backups store numeric ownership, ACLs, extended attributes, SELinux labels, sparse files, and the metadata GNU tar preserves by default, including modes, timestamps, symlinks, and hard links.
This is a filesystem-level backup, not a forensic disk image. Filesystem-specific flags, inode numbers, creation time, and change time are not guaranteed to be preserved.
Script preserves symlinks as symlinks (not their content) with original absolute paths. For proper restoration, restore both source directory and symlink targets.
LVM snapshot does not capture bind mounts! Bind mounts are at filesystem level.
Solution: Back up bind mounts separately from their original location:
# If you have: mount --bind /mnt/data/photos /home/user/Photos
# Back up the original location
./safedata.sh included.rules folder_rsync /mnt/data/photosOn a system that exposes an AC adapter, SafeData starts only while external
power is connected. It also stops a running backup if external power is
disconnected. SAFEDATA_AC_CHECK_INTERVAL controls how often the power state is
checked. Desktops and virtual machines without a detectable AC adapter are not
blocked.
| Situation | Mode | Reason |
|---|---|---|
| Only Documents + Photos | include | Clearly define what you want |
| Entire /home except cache | exclude | Simpler than listing everything |
| Everything on one filesystem | all | No filtering rules are applied |
| First system backup | all | Broadest coverage of the selected filesystem |
| Regular /home backup | include | Save space, backup only important |
| System partition (/var, /root) | all or exclude | Better to have everything |
SafeData appends operational messages to:
$HOME/.local/share/logs/safedata/activity.logThe same operational messages are also sent to the systemd journal:
journalctl -t safedataThe activity.log file does not contain the complete rsync output required by
the statistics viewer. To collect statistics, capture each rsync backup run in
a separate timestamped log:
# Make pipeline failures propagate to the shell
set -o pipefail
# Create the directory used by the statistics viewer
mkdir -p "$HOME/.local/share/logs/safedata"
# Capture one complete rsync backup run in a compatible log file
backup_log="$HOME/.local/share/logs/safedata/safedata_$(date +%F_%H-%M-%S).log"
./safedata.sh all.rules rsync lv_home 2>&1 | tee "$backup_log"Compatible log names use the format safedata_YYYY-MM-DD_HH-MM-SS.log or
safedata_shared_YYYY-MM-DD_HH-MM-SS.log.
SafeData includes a tool for extracting and visualizing statistics from captured
rsync logs. Tar backups do not produce the transfer fields expected by the
statistics viewer and are therefore not included.
First-time setup - Clone repository with submodules:
git clone --recursive https://github.com/tomasmark79/safedata.gitOr if you already cloned without --recursive:
git submodule update --init --recursiveView statistics and graphs:
./show_stats.shWith the Nix package:
safedata-stats
# or from the repository:
nix run .#statsThis will:
- Automatically extract statistics from all compatible logs (if needed)
- Show an interactive menu with graphs and statistics
- Sent Over Time (MB) - Graph of transferred data per backup in megabytes
- Transfer Speed Over Time (MB/sec) - Network/disk transfer speed trends
- Elapsed Time Over Time (seconds) - Backup duration trends
- Total Backup Size Over Time (GB) - Total size of backed up data in gigabytes
- All graphs - Display all 4 graphs at once
- Summary statistics - Overview with key graphs
You can also run specific graphs directly:
./show_stats.sh 1 # Show sent bytes graph
./show_stats.sh 5 # Show all graphs
./show_stats.sh 6 # Show summary statistics=== Sent Over Time (MB) ===
Sent Over Time (MB)
21958 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠁⠀⠀
9762 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠂⠀⠀
6 │⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⣀⢄⣀⣀⣀⣀⣀⣀⡀⣀
└──────────────────────────────────────────
=== Transfer Speed (MB/sec) ===
Transfer Speed (MB/sec)
9.2 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠁⠀⠀
4.2 │⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
0.1 │⠌⣀⢄⣀⣀⣀⣀⡠⣀⡀⠤⢄⠤⠤⡠⠤⠤⠤⠤⠤⠤⠤⠤⠔⠠⣀⣀⣀⣀⢂⡀
└──────────────────────────────────────────
From each compatible rsync log, the script extracts:
- Sent/received bytes - Amount of data transferred
- Transfer speed (bytes/sec) - How fast data was sent
- Backup duration (seconds) - Total elapsed time
- Total backup size - Complete size of backed up data
- Speedup factor - rsync efficiency metric
Extracted data is stored in CSV format at:
~/.local/share/logs/safedata/stats.csv
The statistics tool uses the directory containing SAFEDATA_LOG_FILE, just as
the backup script does. If the variable is unset, the default directory is
$HOME/.local/share/logs/safedata. Set it in the environment when running
safedata-stats or show_stats.sh as well; a systemd service's environment is
not inherited by the terminal. LOGS_DIR can explicitly override the directory
read for logs, and STATS_FILE can override the output CSV path. Without these
overrides, stats.csv is stored alongside the logs. The script updates the CSV
automatically when it finds a newer matching log.
- Python 3 (for uchart visualization)
show_stats.sh- Main script (includes extraction + visualization)uchartsubmodule - Chart renderer (MIT License)
Note: The uchart tool is included as a git submodule. Make sure to clone the repository with --recursive flag or run git submodule update --init --recursive to download it.
This tool uses uchart by Danlino for terminal-based chart rendering.
- Project: https://github.com/Danlino/uchart
- License: MIT License
- uchart is a standalone Python script with zero dependencies that creates beautiful charts using Unicode Braille characters.
# Test exclude (what WILL be backed up):
rsync -avn --exclude-from=rules/excluded.rules /source/ /dest/
# Quick overview of changes:
rsync -avn --exclude-from=rules/excluded.rules /local/ user@host:/remote/
# Check backup size:
du -sh /home/user/Documents