TrueNAS and Time Machine

Oh interesting, I never added TM that way. Learn something new everyday. I’ll give that a try and let you know what happens.

@NickF1227 ok tried doing what you suggested and got an error stating the backup destination could not be added. After reading into tmutil a bit more online, it sounds like I need to create the .sparsebundle file on the share first before I can set the destination. Is that correct?

Sorry for all the questions, I haven’t setup TM thru Terminal before so this is all new to me (TM via Terminal, not Terminal commands themselves).

I pulled up some old notes from my days managing computer labs, and ChatGPT and I just put together a variation of my Munki policy that would do this for me in the lab computers.

I’ve been meaning to do this anyway now that both my wife and I have a Macs. This’ll be probably get used because I’m lazy and have baggillions of little scripts.

I was wrong, the formatting for tmutil requires the SMB share itself, you cant cheat and just give it the local path. Don’t run more than once, it will delete your backups. I call it “Time Machine Reset”

After:

#!/bin/bash

# Ensure the script runs with superuser privileges
if [ "$EUID" -ne 0 ]; then
  echo "ERROR: Please run as root using sudo."
  exit 1
fi

# Define the Time Machine destination and SMB share details
####################################
#Change these variables 
# Formatting is "protocol://user[:password]@host/share"
SMB_SHARE="//guest@prod.fusco.me/timemachine"
MOUNT_POINT="/Volumes/timemachine"
####################################
####################################
COMPUTER_NAME=$(scutil --get ComputerName)
SPARSEBUNDLE_NAME="$COMPUTER_NAME.sparsebundle"
SPARSEBUNDLE_PATH="$MOUNT_POINT/$SPARSEBUNDLE_NAME"

# Function to mount SMB share
mount_smb_share() {
  if mount | grep "$MOUNT_POINT" > /dev/null; then
    echo "INFO: SMB share is already mounted at $MOUNT_POINT."
  else
    echo "INFO: Mounting SMB share $SMB_SHARE at $MOUNT_POINT..."
    mkdir -p "$MOUNT_POINT"
    if mount -t smbfs "$SMB_SHARE" "$MOUNT_POINT"; then
      echo "SUCCESS: Mounted $SMB_SHARE at $MOUNT_POINT."
    else
      echo "ERROR: Failed to mount $SMB_SHARE at $MOUNT_POINT."
      exit 1
    fi
  fi
}

# Mount the SMB share
mount_smb_share

# Check if the backup volume is mounted
if [ ! -d "$MOUNT_POINT" ]; then
  echo "ERROR: The destination $MOUNT_POINT is not mounted or does not exist."
  exit 1
fi

# Check if the destination is writable
if [ ! -w "$MOUNT_POINT" ]; then
  echo "ERROR: The destination $MOUNT_POINT is not writable. Please check the mount permissions."
  exit 1
fi

# Check if any Time Machine destinations are configured
echo "INFO: Checking existing Time Machine destinations..."
if tmutil destinationinfo | grep "$MOUNT_POINT" > /dev/null; then
  echo "INFO: Existing Time Machine destination found at $MOUNT_POINT."

  # Attempt to remove existing destination
  echo "INFO: Removing existing Time Machine destination..."
  if tmutil removedestination "$MOUNT_POINT"; then
    echo "SUCCESS: Removed existing Time Machine destination."
  else
    echo "ERROR: Failed to remove existing Time Machine destination. It may not exist or could be in use."
    exit 1
  fi
else
  echo "INFO: No existing Time Machine destination found at $MOUNT_POINT."
fi

# Set the Time Machine destination
echo "INFO: Setting $MOUNT_POINT as the Time Machine backup destination..."
if tmutil setdestination "smb://guest@prod.fusco.me/timemachine"; then
  echo "SUCCESS: Time Machine destination set to $MOUNT_POINT."
else
  echo "ERROR: The backup destination could not be set. Ensure the destination is correct and accessible."
  exit 1
fi

# Optionally, exclude some common system directories (optional)
echo "INFO: Excluding unnecessary directories from the backup..."
EXCLUDES=(
  "/Library/Caches"
  "/private/tmp"
)
for DIR in "${EXCLUDES[@]}"; do
  if tmutil addexclusion "$DIR"; then
    echo "SUCCESS: Excluded $DIR from backup."
  else
    echo "ERROR: Failed to exclude $DIR from backup."
  fi
done

# Enable automatic backups
echo "INFO: Enabling automatic backups..."
if tmutil enable; then
  echo "SUCCESS: Automatic backups enabled."
else
  echo "ERROR: Failed to enable automatic backups."
  exit 1
fi

# Verify Time Machine destination
echo "INFO: Verifying the Time Machine destination..."
if tmutil destinationinfo; then
  echo "SUCCESS: Time Machine destination verification complete."
else
  echo "ERROR: Failed to verify Time Machine destination."
  exit 1
fi

# Run a manual backup
echo "INFO: Running a manual backup..."
if tmutil startbackup; then
  echo "SUCCESS: Manual backup started."
else
  echo "ERROR: Failed to start the backup."
  exit 1
fi

echo "INFO: Time Machine setup and manual backup complete!"

1 Like

Ok so I’ve tried different options with the script and can’t get it to work. Below is the error:

user@MBP ~ % sudo Desktop/./script.sh
INFO: Mounting SMB share smb://user:password@192.168.20.32/time_machine at /Volumes/time_machine
mount_smbfs: server connection failed: No route to host
mount: /Volumes/time_machine failed with 68
ERROR: Failed to mount smb://user:password@192.168.20.32/time_machine at /Volumes/time_machine.

I know it says there is no route to host, however that is untrue. I can ping that interface fine, and connect to the time_machine share without any issues using the IP list above when using the Connect to Server option under the Go menu in Finder. I sue the same entry as above (smb://user:password@192.168.20.32/time_machine). So not sure what the issue could be.

Just as an update, I wasn’t able to get it working with the script. IT kept failing to connect. I did just add the location with the IP via tmutil but it wouldn’t complete even though both VLANs can talk to each other. So I created a NIC on the NAS for the 192.168.20.0/24 subnet and added the location via tmutil, backups are completing just fine now.

Thanks for the help everyone, I’m going to mark this as resolved.