Use a Raspberry Pi 4 for Time Machine Backups (works with macOS 13 Ventura)
Time Machine is built into the system of macOS and I’m using it since the early days of Mac OS X Leopard. If you have a Raspberry Pi (or two) lying around at your place like me, this tutorial comes in handy to create a cheap and speedy backup solution.
Preparation
Update system software
Make sure your Raspberry Pi is running on the latest software.
sudo apt-get update && sudo apt-get upgrade
Install Samba
We’re going to first install Samba (for SMB) which is a very popular Open Source file sharing protocol which is officially supported by Time Machine for backing up data over a network. The Avahi daemon is used for Apples “Bonjour” mDNS protocol that our Raspberry Pi server is automatically discoverable within the network.
sudo apt-get install samba avahi-daemon
Storage
Connect Hard drive to USB 3.0 port
As the Raspberry Pi 4 ships with 2x USB 3.0 ports we can connect an external hard drive as our disk space for backing up data. Also we should get decent speeds as we also get a 1GBit/s wired connection with the Pi 4.
Create backup user
sudo adduser timemachine
It will ask to set a password for your newly created user.
Set a password
sudo smbpasswd -a timemachine
Create mount point
In order to mount the USB hard drive we need to create a folder where Linux can mount our device to.
sudo mkdir /mnt/timemachine
Mount device
We’ll now mount the USB drive to the previously created mount point.
sudo mount /dev/sdb1 /mnt/timemachine
Note that you’ll need to exchange /dev/sdb1 with your device
Take ownership
To allow our backup user to write on the device we’ll need to take over the ownership of the mount point:
sudo chown -R timemachine: /mnt/timemachine
Configuring Samba
Edit the Samba configuration file:
sudo nano /etc/samba/smb.conf
add the following to the end:
[backups]
comment = Backups
path = /mnt/timemachine
valid users = timemachine
read only = no
vfs objects = catia fruit streams_xattr
fruit:time machine = yes
This will create a network drive called “backups” which we’ll use for Time Machine.
Test Samba configuration
sudo testparm -s
Restart service
Not let’s restart the service to make sure our changes are active.
sudo service smbd reload
Test connection to your Time Machine
On your Mac press “Command + K” on your desktop which will open the “Connect to Server” prompt. Type in the IP Address of your Pi followed by the /backups to connect to our Time Machine volume.
smb://192.168.1.150/backups
Now you should be connected to your Time Machine volume.
Configuring Avahi deamon
In order to let MacOS automatically detect our new Time Machine we’ll need to configure Avahi. For that edit the following file:
sudo nano /etc/avahi/services/samba.service
And paste this configuration in:
<?xml version="1.0" standalone='no'?><!--*-nxml-*-->
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<service-group>
<name replace-wildcards="yes">%h</name>
<service>
<type>_smb._tcp</type>
<port>445</port>
</service>
<service>
<type>_device-info._tcp</type>
<port>9</port>
<txt-record>model=TimeCapsule8,119</txt-record>
</service>
<service>
<type>_adisk._tcp</type>
<port>9</port>
<txt-record>dk0=adVN=backups,adVF=0x82</txt-record>
<txt-record>sys=adVF=0x100</txt-record>
</service>
</service-group>
With this we tell MacOS that in fact our Raspberry Pi is a 8th Gen Time Capsule and it will appear like one in the sidebar in Finder.
Restart Avahi daemon:
sudo service avahi-daemon restart
Automate things
Determine the device ID (UUID) of your USB drive
ls -lha /dev/disk/by-uuid
Automated mount on boot via fstab
We need to tell fstab that it mounts our hard drive to the Raspberry Pi.
sudo nano /etc/fstab
Now we need to append the UUID from above like this:
UUID=7efbdfc5–9c85–4e01–873a-204e00c9aa45 /mnt/timemachine ext4 sync,noexec,nodev,noatime,nodiratime 0 0
Note: if you are using another format for your drive, don’t forget to replace it here in fstab
Automated start of the services
We need to edit crontab with:
sudo crontab -e
We’ll mount the USB drive and start the needed services on a boot/reboot:
@reboot sleep 30 && mount /mnt/timemachine && sleep 30 && umount /mnt/timemachine && sleep 30 && mount /mnt/timemachine && sleep 30 && service avahi-daemon start && service smbd start
Setup Time Machine
Choose volume
The first step is to choose our “backups” volume. For that open the Time Machine settings and you’ll find it under “Available disks”:
Use your password
Use your password to connect to the Raspberry Pi Time Machine volume:
Enjoy backuping
and we’re done! Happy backuping!
This guide was inspired by the following guides: