RAID Time Machine on Linux

My main backup mechanism is a pair of Western Digital Red 2TB hard drives (arranged in RAID1) in my desktop at home. Since they are in RAID1, I only have 2TB of usable space but I can sustain a single drive failure. In order to backup to the RAID array, I've set up Time Machine over SMB to back up my Macbook and I've written a few other services to do things like backup my repositories on GitHub (work in progress).

Below, I'll go through how to set up a RAID array and run a container to expose a folder on the array as an SMB share for Time Machine backups. On my new machine with NixOS, I'm using the old AFP protocol (instead of SMB) running outside of a container. I've included an overview of my new setup on NixOS at the bottom, though much of the RAID setup is still pertinent.

Configure the raid array

Digital Ocean has a great article on setting up RAID arrays on Ubuntu. For my old Debian setup I roughly followed this guide. Here's what I did to erase my two disks (assigned at /dev/sdb and /dev/sdc), set up a RAID1 array (which I call /dev/md0), and format and mount the array at /raid.

BE WARNED: the instructions below will wipe both disks prior to setting up the array.

# get info and wipe free devices
$ lsblk -o NAME,SIZE,FSTYPE,TYPE,MOUNTPOINT
$ wipefs -a /dev/sdb # Did you read the warning above?
$ wipefs -a /dev/sdc # Did you read the warning above?

# create RAID1 array and verify
$ sudo mdadm --create --verbose /dev/md0 --level=1 --raid-devices=2 /dev/sdb /dev/sdc
$ watch -d cat /proc/mdstat
$ sudo mdadm --detail --scan --verbose

# set up configs, format the array (ext4), and mount
$ sudo mdadm --detail --scan | sudo tee -a /etc/mdadm/mdadm.conf
$ cat /etc/mdadm/mdadm.conf
$ sudo update-initramfs -u
$ sudo mkfs.ext4 -F /dev/md0
$ sudo mkdir -p /raid
$ sudo mount /dev/md0 /raid

# verify
$ df -h -x devtmpfs -x tmpfs

# append to /etc/fstab to mount at every boot
$ echo '/dev/md0 /raid ext4 defaults,nofail,discard 0 0' | sudo tee -a /etc/fstab

# done!
$ lsblk
$ df -h

Run a Time Machine container

Now we can run Samba in a docker container to expose a folder (/raid/timemachine) on the RAID array as an SMB share. (See Docker's docs if you need to install Docker).

The one I've had luck with is https://github.com/mbentley/docker-timemachine, which allows you to run either Samba (SMB) or AFP (netatalk) and enable/disable Avahi discovery. Below is an example of using /raid/timemachine as a backup location for Samba without Avahi discovery.

$ mkdir -p /raid/timemachine
# time machine smb fileserver
$ docker run -d --restart=always \
  --name timemachine \
  --hostname timemachine \
  -p 137:137/udp \
  -p 138:138/udp \
  -p 139:139 \
  -p 445:445 \
  -e CUSTOM_SMB_CONF="false" \
  -e CUSTOM_USER="false" \
  -e DEBUG_LEVEL="1" \
  -e HIDE_SHARES="no" \
  -e EXTERNAL_CONF="" \
  -e MIMIC_MODEL="TimeCapsule8,119" \
  -e TM_USERNAME="timemachine" \
  -e TM_GROUPNAME="timemachine" \
  -e TM_UID="1000" \
  -e TM_GID="1000" \
  -e PASSWORD="<password>" \
  -e SET_PERMISSIONS="false" \
  -e SHARE_NAME="TimeMachine" \
  -e SMB_INHERIT_PERMISSIONS="no" \
  -e SMB_NFS_ACES="yes" \
  -e SMB_METADATA="stream" \
  -e SMB_PORT="445" \
  -e SMB_VFS_OBJECTS="acl_xattr fruit streams_xattr" \
  -e VOLUME_SIZE_LIMIT="0" \
  -e WORKGROUP="WORKGROUP" \
  -v /raid/timemachine:/opt/timemachine \
  -v timemachine-var-lib-samba:/var/lib/samba \
  -v timemachine-var-cache-samba:/var/cache/samba \
  -v timemachine-run-samba:/run/samba \
  mbentley/timemachine:smb

Start backups

On macOS, connect to the server with Finder (Finder > Go > Connect to Server…) and enter smb://<server-ip-address>. Then open System Preferences > Time Machine, and select the disk for backups.

NixOS

Now, on NixOS, I've scrapped the docker configuration and opted for the old AFP protocol. With NixOS's services.netatalk I'm able to set up AFP with a few lines in my configuration.nix.

rssfacebookgithubmailinstagramlinkedinstackoverflowexperimentalkeylock