Category filter
Script for Time Machine Backup in Mac
Time Machine is an inbuilt backup feature in macOS devices that allow automatic backup of data. Having a backup allows you to recover files that you delete or can’t access. You can use custom scripts with Hexnode to manage and configure Time Machine backups on macOS devices.
First, ensure tmutil
(time machine utility) command and Terminal app have full disc access. This privacy feature introduced in macOS 10.14 prevents applications from accessing important data without permission. This has to be done manually on each device. To allow the tmutil
operation, select Full Disk Access in the Privacy tab of the Security & Privacy preference pane, and click the “+” button, then hit the cmd+shift+G shortcut and enter /usr/bin/tmutil in the field, then click “Add”. tmutil
will be added to the list of applications which are allowed Full Disk Access. Also, add Terminal if not already added to the list.
Script to backup macOS devices
You can back up your data to external storage connected to your Mac or available in your network. You can use the command to add your backup location for a shared Mac:
1 |
tmutil setdestination –a smb://<user>:<passoword>@<ip address>:/<backup hard drive name> |
To add an external drive as a backup location, use the command:
1 |
tmutil setdestination –a /Volumes/backupdrive |
“backupdrive” is the name of the drive plugged into your Mac as the backup destination.
To check added backup destinations, use the command :
1 |
tmutil destinationinfo |
For each backup location, the command displays name, type and ID. For network destination, the URL used is displayed and for external drives mounted, the Mount Point path in the file system is displayed.
If multiple backup locations are added, you could choose one using the command:
1 |
tmutil setdestination <arg> |
When –a
is not provided, the current list of destinations is replaced by
You can remove destinations using the command:
1 |
tmutil removedestination <destination_id> |
destination_id is the ID returned by destinationinfo command.
You can exclude files and folders from Time Machine backup using the command:
1 |
tmutil addexclusion <path> |
These exclusions will remain in the Time Machine exclusion list even if you remove them. You should add the –p
flag to remove it from the exclusion list.
1 |
tmutil addexclusion -p <path> |
To confirm if a file has been excluded, use the command:
1 |
tmutil isexcluded <path> |
To remove an exclusion; you can use the command:
1 |
tmutil removeexclusion <path> |
Once you have excluded all unwanted files and folders, you can start the backup process.
Enable and start the backup process using the commands:
1 2 |
tmutil enable tmutil startbackup |
You can check the status of your backup using the command:
1 |
tmutil status |
This command returns ClientID, percentage, running status, destination ID and other data that returns the status of the backup process.
You could stop the backup process if your device is running slow. Pause backup using the command:
1 |
tmutil stopbackup |
Disable time machine backup with the command
1 |
tmutil disable |
To view your backups, use the command
1 |
tmutil listbackups |
There are several time machine related configurations such as interval, delay, grace period, start interval etc. To modify these, one must edit the com.apple.backupd-helper.plist file. This is feasible only if you disable System Integrity Protection temporarily.
To disable SIP:
- Restart your computer in Recovery Mode.
- Launch Terminal from the utilities menu and run the command:
1csrutil disable
- Restart your computer.
To enable SIP, follow the same steps as before and run the command:
1 |
csrutil enable |
You can modify time machine configurations by using the commands:
1 2 3 4 |
sudo defaults write /System/Library/LaunchDaemons/com.apple.backupd-helper StartInterval -int 60 sudo defaults write /System/Library/LaunchDaemons/com.apple.backupd-helper Interval -int 1800 sudo defaults write /System/Library/LaunchDaemons/com.apple.backupd-helper Delay -int 2700 sudo defaults write /System/Library/LaunchDaemons/com.apple.backupd-helper GracePeriod -int 180 |
Interval
– Time between next backup and previous backup (by default one hour)
Delay
– When a Mac wakes up from sleep, a backup is done immediately, unless one was done within previous hour (default one hour). This resets the schedule.
GracePeriod
– When Mac is restarted and time machine schedule is reset, it won’t backup immediately even if it’s been more than the delay set. It will wait 30 minutes (default period) to avoid conflicts with automatic or manual processes set up by the user after a restart.
StartInterval
– Time between the charger is plugged in and the start of time machine backup.
-int 1800
signify 1800 seconds or 30 minutes.
When the com.apple.backupd-helper.plist file is edited, only the StartInterval period is seen as modified. The other configurations remain in their default values, but the backup action is triggered at intervals modified using the script. Note that the configurations are affected by the size of the backup initiated.