My Janky Backup Solution
Backups using cron, rsnapshot, SSH, and Tomb.
For the past years I have been using rsnapshot and Tomb for backing up my data. In this post I will describe my janky configuration. But at least my data is backed up. Is yours?
Background
My current desktop computer has an old and slow HDD mounted on /home.
For a long while I did not take any automatic backups since I did not have
anywhere to put them.
I also don’t like the idea of backing up my data to the cloud unencrypted.
But a few years ago I bought a new hard drive which now houses my backups. This is an internal hard drive directly plugged into my desktop computer. It has not replaced my main drive and it is instead mounted beside it.
rsnapshot
rsnapshot is a program that backups your data in different snapshots across different levels. It is common to take snapshots hourly1, daily, weekly, and monthly, each triggered by cron. These snapshots can then easily be navigated in a file tree like the original data.
# Your file /path/to/file.txt will be backupped to
/path/to/snapshots/hourly.0/path/to/file.txt
# An older version of the file is in
/path/to/snapshots/daily.2/path/to/file.txt
# And an even older version of file.txt, etc
/path/to/snapshots/weekly.1/path/to/file.txtrsnapshot makes smart use of hard links in order to massively reduce the required space.
Hard vs symbolic linksA file has two parts: metadata (filename, permissions, etc.) and a pointer to data on the hard drive. A hard link is a new set of metadata and a pointer that points to the same data as the original file. A hard link increases the reference counter of the data and are indistinguishable from the original file.
A symbolic link on the other hand is a pointer to a file (not to the file’s data).
When a new hourly snapshots is taken two things happen: the previous hourly snapshots are
rotated and then the data is synced (copied) to hourly.0.
Rotating the snapshots means that the oldest hourly is deleted and the others
are moved from hourly.{1..X} to hourly.x+1.2
Then, a hard copy of all the files in hourly.0 is made into a new hourly.1.
After that rsync is used to sync your data into hourly.0.
Since hourly.0 already contains hard links to the data in the previous snapshot it only
needs to copy new and updated files, and delete3 deleted files.
When a new daily (or less frequent) snapshot is taken it simply rotates the existing daily
snapshots and moves hourly.X to daily.0.
This step is fast since there is no syncing necessary.
Secondary Devices
rsnapshot has the ability to take backups over SSH and I have configured it to also pull4 data from my laptop to backup. One caveat is that automatic (non-interactive) backups requires an unencrypted SSH-key to the host. It also needs to know the IP of the host which, for a laptop, is not trivial. I just use the local IP and skip backups if it is not available.
My phone on the other hand does not run an SSH server but it does have
Termux with SSH and cron.
It copies its data to my desktops /home using SSH every few hours.
Because I don’t want it to have unrestricted access to my desktop I made it a
separate user that it can use.
The data that it writes is then is backupped by rsnapshot.
This is quite janky and unreliable.
It is hard to even find the important data on the phone and
it is not trivial to get access to the data from the different apps.
Some apps require manual exporting of its data.
Perhaps there are better solutions.
Backing up the backups
The snapshots are stored on a different hard drive but they are still on the same computer as the main copy. It is important to have backups on multiple devices and preferably off-site as well.
My laptop have some space available for data but there are some requirements.
- It is much easier to lose a laptop than a desktop computer and I do not want it to have unprotected access to my backups. The data has to be stored encrypted.
- Deduplication of data is necessary to keep the backups small. The snapshots have to be kept together and separating them, e.g. by encrypting each snapshot by itself, is out of the question.
- rsnapshot cannot be used directly since it would require an unencrypted SSH-key on the laptop to pull data from the desktop computer.
Tomb
I use Tomb on the laptop which lets you create a large encrypted file5 that can be mounted as a device on Linux and decrypted. With this we can use the same hard link trick as rsnapshot to store snapshots.
I have configured rsnapshot on the laptop to backup a single dummy-file when triggered,
with a single level of backups (alpha).
When I want to store a new snapshot on the laptop I decrypt the tomb,
use rsnapshot to rotate the snapshots inside the tomb, and then manually
execute rsync (using the same flags as rsnapshot uses) on the desktop pushing
the desktop’s hourly.0 into the laptop’s alpha.0.
This method is not that streamlined and it is manually triggered since the tomb needs to be decrypted before the backup can be done. It could be made more streamlined but the problem is that the laptop might not be on the network.
I wrote a script that does everything though. It is quite janky though and often I run it in parts and not the entire script at once.
#!/bin/sh
HOST="user@host" # SSH user and host
TOMB_NAME="backup"
TOMB_KEYFILE="<>" # On localhost
TOMB_MOUNTPOINT="<>" # On other host
TOMB_PATH="/path/to/$TOMB_NAME.tomb" # On other host
# The (retain) level in rsnapshot that is going to be rotated and copy into
LEVEL="alpha"
# Source (on local) and destination (on other host) of the data to be copied
SOURCE="/path/to/hourly.0/"
DEST="$TOMB_MOUNTPOINT/snapshots/$LEVEL/"
alias rsync_cmd="rsync -av --numeric-ids --delete --delete-excluded ${SOURCE} ${HOST}:${DEST}"
# Mount Tomb if necessary
OPENED=0
if ssh $HOST "test -d $DEST" ; then
echo "Tomb is already mounted"
else
echo "Mounting tomb..."
# Open a Tomb on a remote server passing the unencrypted local key on stdin
# via SSH, without saving any remote copy of it:
if gpg -d "$TOMB_KEYFILE" | ssh $HOST tomb open "$TOMB_PATH" "$TOMB_MOUNTPOINT" -k cleartext --unsafe ; then
echo "Tomb successfully opened";
else
echo "Tomb failed to open, exiting";
exit 1;
fi
OPENED=1;
fi
# Rotate snapshots if necessary (older than 24 hours)
if [[ $(ssh $HOST find "$TOMB_MOUNTPOINT/snapshots/$LEVEL.0" -maxdepth 0 -mtime +1 -print) ]] ; then
echo "Rotating $LEVEL..."
if ssh $HOST rsnapshot "$LEVEL" ; then
echo "$LEVEL rotated"
else
echo "Failed to rotate"
exit 1
fi
else
echo "No rotating necessary"
fi
# Copy over the data
echo "Copying data...";
if rsync_cmd ; then
echo "Successfully rsynced";
else
echo "Failed to rsync";
fi
# Close the tomb (if it wasn't already opened)
if [[ "$OPENED" -gt 0 ]] ; then
echo "Slamming tomb..."
ssh $HOST tomb slam "$TOMB_NAME"
else
echo "Tomb is left open";
fi
echo "Backup finished"Exclusion
rsnapshot allows you to configure paths and patterns that should be excluded from backup and these should be used. A lot of space can be wasted on useless caches and large temporary files. Once a temporary file is copied into a snapshot it can stay in there for months. The pathological case for rsnapshot is large files with frequent small updates, e.g. a large archive with all of your emails that updates multiple times per day. This forces rsnapshot to make a new copy in every snapshot. You should prefer to archive them per year instead as you are not getting any new emails from previous years.
A related issue is moving or renaming files which causes a new copy into the
next snapshot. You might want to be careful with large scale reorganizations of
your data. I have experimented with two scripts: resurrect and supermv.
resurrect is like cp but it sources the source from the most recent snapshot.
#!/bin/sh
sudo cp -a "/path/to/snapshots/hourly.0/$(uname -n)/$(pwd)/$1" "$2"supermv is like mv but it also performs the move inside of the most recent snapshot.
This prevents a second copy of the file being made in the next snapshot.
Useful for large static files which would otherwise have two copies inside the snapshots if it was moved normally.
#!/bin/bash
# This script works like `mv` in that it moves SOURCE-files into DEST. This
# script also moves the corresponding file in the most recent snapshot so that
# rsnapshot will recognize that it is the same and not making a new copy.
# Caveat: For each SOURCE it moves it in the snapshot first and then the actual
# source. If any move fails the program exits. This is not how `mv` behaves.
if [ "$#" -le 1 ]; then
echo "Usage supermv SOURCE DEST"
echo " supermv SOURCE... DIRECTORY"
exit 1;
fi
# Get the last paramater
# Via https://stackoverflow.com/questions/1215538/extract-parameters-before-last-parameter-in
#
# This seems to correctly handle the damned quoting issues in bash correctly.
# supermv aoeu htns aoeu\ htns "aoeu htns" hutenosa
# correctly produces 4 moves into 'htns aoeu'.
#
dest="${!#}"
length=$(($#-1))
sources=("${@:1:$length}")
if [[ ! "$dest" = /* ]]; then
# Prepend pwd to destination if the path is not absolute.
dest="$(pwd)/$dest";
fi
snapshot_dir="/path/to/snapshots/hourly.0/$(uname -n)"
for file in "${sources[@]}"; do
if [[ ! "$file" = /* ]]; then
# Prepend pwd to source if the path is not absolute.
file="$(pwd)/$file";
fi
# Move the file in the snapshot first.
sudo mv "$snapshot_dir$file" "$snapshot_dir$dest"
if [ $? -ne 0 ]; then
echo "sudo mv \"$snapshot_dir$file\" \"$snapshot_dir$dest\" failed. Exiting..."
exit 1;
fi
# Then move the file on the main disk.
mv "$file" "$dest"
if [ $? -ne 0 ]; then
echo "mv \"$file\" \"$dest\" failed. Exiting..."
exit 1;
fi
doneThe implementations of these are quite janky and should be handled with care.
Using .. inside the path can cause it to escape from the snapshot.
Also keep in mind of any race conditions, e.g. if a snapshot is currently being taken.
Conclusion
This is a bit janky but I am making good use of what I have. Buying a NAS is definitively a good idea but expensive. There is still the unsolved off-site backup problem.
Alternatives
As I have been writing this article I have had second thoughts with regards to the backup to the laptop. Since the data is stored encrypted at rest using Tomb this precludes automatic backups and requires one to manually open the tomb before the backup can be made.
But if the backup is manual then it is no big deal to also use an SSH-key that is protected with a password. Perhaps I should change it so that rsnapshot is used directly on the laptop to both rotate and sync the data. I can then remove my janky script.
For off-site backups there is an interesting feature in Tomb where it can decrypt over SSHFS. This allows you to open a tomb on a different computer than where it is stored on. I did not go this route for the laptop because the laptop is old, slow, and it does not have a reliable network connection. For a NAS this is more doable and perhaps good enough to make automatic off-site backups possible.
References
Notes
-
While it is called hourly it is not common or necessary to take snapshots strictly every hour. Mine are taken every four hours and they are completed in about 5-10 minutes. The levels are just names and what matters is how often, using cron, they are triggered. The current default configuration use alpha, beta, gamma, and delta as the names for the levels. ↩︎
-
How often a level is rotated is separate and independent from the number of snapshots that are stored (retained) in that level. I retain six hourly snapshots. ↩︎
-
rsnapshot takes snapshots on how your data looks like at a particular time and any deleted files will be deleted in the next snapshot. It is syncing data, not just copying. This is not like copying data to an USB and forgetting about it, knowing that it will be safe so long as it is not touched. If you want to recover data that you lost months ago you must have kept a snapshot from months ago. You can lose data if you delete or overwrite something without noticing it. ↩︎
-
rsnapshot prefers pulling data from other hosts instead of pushing. You can probably overcome this limitation using SSHFS but consider the security of the system. The backed up host should probably not have direct access the backup. If you get ransomwared they might also get your backups. ↩︎
-
My tomb is 100 GiB, much larger than any of the official examples. It works but it is slow to open. It takes more than 15 minutes to mount. Perhaps that is mostly the old laptop’s fault though. ↩︎