# 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](https://www.hexnode.com/mobile-device-management/help/how-to-run-scripts-on-mac-using-hexnode-mdm/) 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:

Script to add backup location

tmutil setdestination –a smb://<user>:<passoword>@<ip address>:/<backup hard drive name> 

   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:

Script to dd external backup location

tmutil setdestination –a /Volumes/backupdrive 

   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 :

Script to check added backup location

tmutil destinationinfo 

   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:

Script to choose a backup location

tmutil setdestination <arg> 

   1

  tmutil setdestination <arg> 

   

 

  

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

You can remove destinations using the command:

Script to remove backup location

tmutil removedestination <destination\_id> 

   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:

Script to add exclusion list for backup

tmutil addexclusion <path> 

   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.

Script to remove files from exclusion list

tmutil addexclusion -p <path> 

   1

  tmutil addexclusion -p <path> 

   

 

  

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

Script to confirm if a file is in the exclusion list

tmutil isexcluded <path> 

   1

  tmutil isexcluded <path> 

   

 

  

To remove an exclusion; you can use the command:

Script to remove an exclusion list

tmutil removeexclusion <path> 

   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:

Script to enable backup

tmutil enable tmutil startbackup 

   1

2

  tmutil enable 

tmutil startbackup 

   

 

  

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

Script to check the status of backup

tmutil status 

   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:

Script to stop backup process

tmutil stopbackup 

   1

  tmutil stopbackup 

   

 

  

Disable time machine backup with the command

Script to disable backup

tmutil disable 

   1

  tmutil disable 

   

 

  

To view your backups, use the command

Script to view backups

tmutil listbackups 

   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:

1. Restart your computer in Recovery Mode.
2. Launch Terminal from the utilities menu and run the command: Script to disable SIP
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    csrutil disable 
    
       1
    
    
    
      csrutil disable
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:

Script to enable SIP

csrutil enable 

   1

  csrutil enable 

   

 

  

You can modify time machine configurations by using the commands:

Script to modify time machine configurations

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 

   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.

 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.