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.

Disclaimer:

The Sample Scripts provided below are adapted from third-party Open-Source sites.

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:

To add an external drive as a backup location, use the command:

“backupdrive” is the name of the drive plugged into your Mac as the backup destination.

To check added backup destinations, use the command :

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:

When –a is not provided, the current list of destinations is replaced by , the backup location.

You can remove destinations using the command:

destination_id is the ID returned by destinationinfo command.

You can exclude files and folders from Time Machine backup using the command:

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.

To confirm if a file has been excluded, use the command:

To remove an exclusion; you can use the command:

Once you have excluded all unwanted files and folders, you can start the backup process.

Enable and start the backup process using the commands:

You can check the status of your backup using the command:

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:

Disable time machine backup with the command

To view your backups, use the command

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:

  1. Restart your computer in Recovery Mode.
  2. Launch Terminal from the utilities menu and run the command:
  3. Restart your computer.
Disclaimer:

Disabling SIP leaves your computer vulnerable to malicious code. Disable protections temporarily to perform low level tasks and re-enable as soon as possible.

To enable SIP, follow the same steps as before and run the command:

You can modify time machine configurations by using the commands:

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.

Notes:

  • Ensure your device is plugged in and has sufficient space before initiating Time Machine Backup.
  • It is recommended to manually validate the script execution on a system before executing the action in bulk.
  • Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.

  • Sample Script Repository