Wednesday, April 1, 2020

Manually Scheduling your Mac Backups
(for macOS Sierra and later)

This will enable you to have flexibility to schedule and run your backups for your mac.


1.      Disable the automatic backups you have in your time machine preference window by unticking the Back Up Automatically checkbox.





2.     We need to configure Time Jobs using launchd, it’s the cron of MacOS for those that are familiar with Unix.  For more information on how to configure such jobs, see the man pages of launchd.plist.

To add a new backup job using launchd:

a.      Open a terminal session and switch to root user

sudo -i

<use your user admin password here that you used to login to your mac session)

b.      Go to /Library/LaunchDaemons and open a vi session and create your backup plist file with the contents below.

The below job will run every Sunday at 23:33 local time. You can adjust the date and time as per your preference.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
   <dict>
<key>Label</key>
<string>com.myimac.backup</string>
<key>ProgramArguments</key>
<array>
               <string>/usr/bin/tmutil</string>
               <string>startbackup</string>
               <string>--auto</string>
               <string>--block</string>
</array>
<key>StandardErrorPath</key>
<string>/var/root/timemachinebackup_err.log</string>
<key>StandardOutPath</key>
<string>/var/root/timemachine.log</string>
<key>StartCalendarInterval</key>
<dict>
               <key>Hour</key>
               <integer>23</integer>
               <key>Minute</key>
               <integer>33</integer>
                <key>Weekday</key>
                <integer>7</integer>
</dict>
   </dict>
</plist>

Save your file to a file ending in plist. It would help if it has the same as the Label string highlighted above.

com.myimac.backup.plist

c.      To schedule the above plist file  or backup job issue the following commands as root:

launchctl load /Library/LaunchDaemons/com.myimac.backup.plist

To check if it has been scheduled:

launchctl list | grep 'myimac'

Output:
-     0            com.giaimac.backup

To start your first backup job execution

launchctl start com.giaimac.backup

To check if its running

launchctl list | grep 'myimac'

Output:
94990        0            com.myimac.backup

       Or Open your Time Machine Preference and you will see there that the backup is running.

To Stop the said job from running in its scheduled time:

launchctl unload /Library/LaunchDaemons/com.myimac.backup.plist

You can further check the following sites for detailed information on  how to use schedule jobs in mac or use the launchctl command


Monday, March 30, 2020

How to compress your Time Machine backups that is on a network shared drive?

Compacting Time Machine File that is on a network shared drive. Do this after deleting the old backups from your time machine backups to reclaim the space the previous backups occupied.


  1. Dismount your time machine disk from Time Machine preference by selecting Remove Disk under Select Disk menu.

This is important to ensure nothing is accessing your time machine backup file. Else you will receive the "hdiutil resource temporarily unavailable" error when running the compact command. 

  1. To avoid the “hdiutil compact internal” error, ensure that the sparsebundle or your MAC backup file is not being accessed by anyone or anything. To do this, access the sparsebundle file in your NAS drive via Finder and mount it by clicking on it. Once it appears in your Finder  under Devices eject it manually. 

Sometimes it takes quite a few minutes (20-30 mins) for the sparsebundle file to be mounted after you click on it before you can eject it. If this happens, check the /var/log/fsck_hfs.log file and see if the mac is running fsck on the disk. Once its done, a window might appear saying nothing to be mounted, don’t worry and perform step 2  again.


3.     Using your user administrator account in your mac, open your Terminal Application and switch to root.

sudo -i

<enter your admin password when asked for password>

4.     Run the command below in your MAC Terminal screen after step 3 above. You can also run 'df -k' before you run the below command to compare your file system utilisation of your mac backup drive or location.

            hdiutil compact <fullpath of your sparesebundle file>

            Example:

               hdiutil compact /Volumes/MacBackup/MyImac.sparsebundle

            If you receive a “hdiutil resource temporarily unavailable”,
·       Make sure you have done step 1 above.
·       Make sure no processes is accessing the sparsebundle file even in your NAS storage.
·       Make sure you have the location of your sparsebundle file mounted in your MAC. For the case of the above example, make sure /Volumes/Macbackup is mounted in your MAC. To check this, issue “df -k” command.

      If you receive “hdiutil compact internal error”
·       Make sure you have done step 2 above properly.

       Once that is complete, you can issue 'df -k' and you'll see that the available space in your NAS Drive has increased. Now its time to attach back the newly compacted backup back to your time machine.

1. Open your finder and access the remote location where your sparsebundle is located. DO NOT mount or click at this time the sparsebundle. This step will ensure that you have mounted properly the remote file system where your sparsebundle backup file is located.     
2.  As the user administrator of your Mac, Open your Time Machine Preference and add back the remote disk you used previously. (Select Backup Disk)
3.     Quickly stop the impending backup by unchecking the Back Up Automatically check box.

4.     Open a terminal window and switch to root via 'sudo -i' command and issue this command once you are root

tmutil inheritbackup <your sparsebundle  full path file location>


For example:
           tmutil inheritbackup  /Volumes/MacBackup/MyMac.sparsebundle
           Attaching disk image...
           ** /dev/rdisk3s2 (NO WRITE)
           Executing fsck_hfs (version hfs-407.50.6).
           QUICKCHECK ONLY; FILESYSTEM CLEAN
          Claiming disk image for machine...
          Ejecting disk image...


5.     Go back to your finder in step 1 and Click on the sparsebundle file in Finder to mount it as a Time Machine drive. Normally MacOS will mount it under /Volumes/Time Machine Backups file system


6.     Issue the commands below (modify the associated command parameter to the latest backup)

tmutil associatedisk -a / <location of your latest backup in /Volumes/Time Machine Backups>

For example:

(take note of the spaces and the “\” before them, this is to “escape” them in mac os command line. To easily paste the below location to your Terminal window, from your Finder window, drag whe Macintosh HD folder in the Terminal window and it will give you the path naming convention below with "\")

tmutil associatedisk -a / /Volumes/Time\ Machine\ Backups/Backups.backupdb/MyImac/2020-03-30-010714/Macintosh\ HD 

(you can use alternatively the quoted version of the command above)

tmutil associatedisk -a / “/Volumes/Time Machine Backups/Backups.backupdb/MyImac/2020-03-30-010714/Macintosh HD”


7.     Wait for a few minutes then issue this command to check if your backups are now associated back to your time machine or restart your MAC

tmutil listbackups


8.     Reboot your mac so that you’ll see the updated information (without the -1 in their volume names)

9.     Run your Time Machine Backup to update your backup information in the Time Machine Preference window.