Marcus Str.
Husband, flight sim pilot, IT specialist

About | Tweets | Code | CV | Categories | Ortho4XP Tiles | Live Stream


Backups like Time Machine
 
Linux


Back then when I used Macs, I was highly appreciative of a tool that was once introduced to safely back up your data: Time Machine. This tool was (and is? Does Apple still ship this with Macs?) basically a super funky implementation of a collection of rsync commands, wrapped into a fancy UI. Eventually in over time you'd create something like a timeline of files and folders that had changed. You could go back to earlier versions of any files and folders, should you need it.

Since my rig is now completely running off of QEMU virtualized images, deleting those images would also mean deleting all my data. Which is, obviously, not desired. So I did some research. rsync is a native tool to the Linux/Unix world, so it should be possible to achieve the same backup mechanism as Time Machine does.

I attached my old 2TB HD to my server, slammed an ext4 filesystem on it, and, of course, called the mount point TimeMachine. Through sshfs I can mount this network backup drive to my main VM. This happens automatically when I log in.

I found this super useful gem: Rsync time backup (Link). It basically mimics exactly what Time Machine on Macs does.

Run a base backup with this script first. Then, it will create backups of files and folders that have changed, creating hard links for non-changed files along the way. The "latest" symlink in your destination folder will point to the most current version of your complete data set. This minimizes the amount of data needed to be backed up, you always have the current version of your data in the symlink, plus, you can always go back to an earlier state of any of your files at any time, at will.

Or, to summarize: It's Time Machine.

Here's a revised script of what I use now:

The "TIMEDILATION" variable points to the copy of the script provided in the above GitHub, I just renamed it.

TimeMachine.sh -

#!/bin/bash

# Location of Time Machine
TIMEMACHINE=/home/marcus/Network/TimeMachine

# Location of actual Time Machine script
TIMEDILATION=/home/marcus/Private/Scripts/TimeMachine/timedilation.sh

# Root of home folder inside which we find the folders below
BACKUPROOT=/home/marcus

# List of folders to back up
declare -a incl=(
"Desktop"
"Developer"
"Documents"
"Games"
"Pictures"
"Private"
"Production"
"Uploads"
"VR"
"Videos"
"Work"
)


# ------------------------------------------
# NO TRESPASSING BEYOND THIS POINT
# ------------------------------------------

# Check if folders exist in destination
for i in "${incl[@]}"
do
if [ ! -d "$TIMEMACHINE/$i" ]; then
mkdir $TIMEMACHINE/$i
touch $TIMEMACHINE/$i/backup.marker
fi
done


# We can now walk through the inclusion list
# and create Time Machine backups for every folder
# we have specified.

for i in "${incl[@]}"
do
$TIMEDILATION $BACKUPROOT/$i $TIMEMACHINE/$i
done


And to make it fully operational like Time Machine's "Set It Then Forget It" attribute, I created a cronjob via crontab -e like so:

0 */1 * * * if grep -qs /home/marcus/Network/TimeMachine /proc/mounts; then /home/marcus/Private/Scripts/TimeMachine/TimeMachine.sh; fi


Enjoy.


© 2024 Marcus Str.