5

Due to APFS having a mind of its own and filling the hard disk with unwanted snapshots, I made a cron to run tmutil deletelocalsnapshots xxx. Later I found out that running this command while a backup is in progress will stop the backup. How can I know if a time machine backup is in progress to avoid running the command? The man page for tmutil doesn't list any status command.

Right now I'm doing the following in the script, since now backups mount at /Volumes/Time Machine Backups/:

ls -d /Volumes/Time* 2>/dev/null|grep Machin >/dev/null

if [[ $? != 0 ]]; then
     # run here tmutil snapshot deletion commands
fi

However, this doesn't seem to be very precise. The volume may be mounted some minutes later after a backup has started, and also stopping a backup leaves that mounted volume for a while. Finally, the name of the mounted volume might change in the future, making the check useless.

6
  • Rather than edit your post - I'll try to answer in a way that helps. Problems I see are 1) APFS is on SSD and no HD - you'd need a very detailed follow up question to explain how / why you have APFS on HD and if you are backing up to APFS or from APFS. 2) Local snapshots really don't take extra space in practice. The space is already committed and thins when you need it. What exact storage allocation is in the volume that's running low? Perhaps this is an XY question.
    – bmike
    Commented Feb 25, 2018 at 14:24
  • 1) I only have one SSD with APFS and an external ethernet time capsule. 2) I process many huge video files. Many days I end up my work day with 20GB free, leave the computer on, and in the morning I'm 140GB free. Those snapshots are really annoying when I need those 120GB now. Commented Feb 25, 2018 at 14:57
  • 1
    That's easy to script. tmutil thinlocalsnapshots \ 100... with enough zeros to get you to 120 GB. If you have paid support, I would open a case with Apple on that use case. Seems one tmdiagnose and the engineers could make sure their configuration works for your use case. I'll edit my post to get you a 1 GB purge. Great question +2 if I could vote twice.
    – bmike
    Commented Feb 25, 2018 at 15:04
  • Ugh, yeah, thanks, that's why I started this question, because I'm trying to script those commands, and deletelocalsnapshots simply works better than thinlocalsnapshots in my experience. Commented Feb 25, 2018 at 15:07
  • Excellent - feel free to list your script if no one delivers one and you need to tweak the thin example. I don't know if Apple exposed a way to measure the snapshots so I'm using thinlocalsnapshots over deletelocalsnapshots when I need to urgently free up space. (say when pre-staging a 50 GB Adobe install over a week or two period so we can install them all from local image rather than over the network)
    – bmike
    Commented Feb 25, 2018 at 15:09

3 Answers 3

9

The canonical way to check status of time machine is to use the utility and check status or for newer os check the current phase only:

 tmutil status
 tmutil currentphase

Now, you have to process the text since it reports exit 0 when running and when it's not running since the status returned - not that there's a correct status.

I would use:

tmutil status | grep "Running = 0;"

That returns 0 if the system is not currently running and 1 if you are backing up. However, back up on High Sierra implies a non-snapshot event. The snapshots literally are to external drives which literally are not APFS. Only the internal SSD gets APFS treatment and the local snapshots are not something that "run" as so much as "just exist" and the "running" if anything is the thinning process - not the backing up process.

The thinning process calls backupd-helper which doesn't have a command line tool to check when done but you can time that running by issuing a blocking call like:

 tmutil thinlocalsnapshots / 1000000000

Above has enough zeros to thin one giga byte - add two zeros to ask for 100 GB back.


I do want to correct (or at least challenge lightly) the concept of too many snapshots. APFS is a copy on write filesystem and the only examples of this being bad seem highly contrived or non-default configurations. The files that are around are already written to disk. The system just is "lazy" about cleaning them up and doesn't expend any processing time until it knows the a) system is relatively idle b) storage is relatively idle and not yet running low on free space.

Apple is clearly still in flux with how many snapshots to keep mounted, how to set the cleanup thresholds, how many intervals to keep in situations where the external drive is not connected and the local snapshots exist.

You can dig deeper with:

tmutil listlocalsnapshots /
tmutil thinlocalsnapshots / 1
tmutil deletelocalsnapshots YYYY-MM-DD-HHMMSS

Again, I've yet to see a place where I needed to thin or delete snapshots out of the automatic process on local volumes. The delete can be handy when you want to purge a large backup from an external drive. The real lever we used to have was to use Time Machine exclusions to omit the backup of large files that had tiny changes. It remains to be seen how APFS reacts to these files - my guess is you may be able to placethose type of files on a different APFS volume and thin aggressively and periodically or continue to watch the backup exclusions to see if that saves you allocated space without slowing down the copy on write performance and storage benefits.

4
  • 1
    There is also tmutil currentphase (which exists at least back to 10.11) which will give a one-word summary of what Time Machine is currently doing. It outputs 'BackupNotRunning' if Time Machine is not active. There are a variety of other "words" used if it is running. tmutil currentphase is basically the same as tmutil status | awk -F' ' '/BackupPhase/{print $NF}' except that the latter will have a ; at the end.
    – TJ Luoma
    Commented Oct 12, 2019 at 19:20
  • That’s well worth an edit @TJLuoma - thanks for all you bring to this site and community.
    – bmike
    Commented Nov 11, 2019 at 0:37
  • Thank you for the kind words @bmike. I am very grateful for this site, so I'm glad to do what I can to support it.
    – TJ Luoma
    Commented Nov 11, 2019 at 2:06
  • If you wish to convert the output of tmutil status to a more easily parseable format such as JSON, you can use tmutil status | tail -n+2 | plutil -convert json -o - -- -. Then you can throw this output at jq to pull info out. Commented Mar 19, 2020 at 2:28
3

Undocumented tmutil status

There is an undocumented, and thus subject to change, tmutil status. This call returns the response:

Backup session status:
{
    ClientID = "com.apple.backupd";
    Running = 0;
}

You will need to parse the JSON portion of the response to learn if Time Machine is running.

Look for backupd

On older versions of macOS, if the backupd process is running, then Time Machine is actively backing up.

See the StackOverflow question, Check if program is running with bash shell script? for robust methods of checking for a running process.

3
  • 1
    grep is good enough for the running check in practice - that "undocumented" tool has been thankfully stable since Lion and the grep has worked for us since then without needing change. Of course, Apple could change this all in 10.13.4 - documented or not. The whole premise here is suspect to me, though. I think we're answering a thing that's not even going to help the OP. Once they get this check in place - how are they going to use it and will it "solve" anything is curious to me.
    – bmike
    Commented Feb 25, 2018 at 14:26
  • That is not accurate. I have Time Machine configured, but it is not currently running… in fact, the drive isn't even connected, but backupd and backupd-helper -launchd are both shown in ps. So tmutil statusis still the best way, despite being undocumented.
    – TJ Luoma
    Commented Jul 11, 2019 at 2:23
  • @TJLuoma thanks. I have updated the answer. Commented Jul 13, 2019 at 13:26
-1

Time Machine itself has a menu bar that you can use. You just need to enable the menu bar item.

Inside the preference pane of Time Machine, there is a checkbox selection for the menu bar item to appear. The menu bar allows you to manually do a backup, if it is doing a backup, and how far along it is.

EDIT: This however will not allow for you to setup a cron to know when a local snapshot is being taken, you'd have to do it manually.

Edit 2: down voted or not, those searching for the question titled will find my answer to be helpful. I won't be deleting it.

2
  • How does this help in a shell script (see question)?
    – nohillside
    Commented Feb 25, 2018 at 13:30
  • That question was "How do I know if a time machine backup is running?". Why you are using a script to delete local snapshots that are used later during an actual backup I'm not sure you didn't state. If you want to prevent local snapshots from being performed, please update your question.
    – Jahhein
    Commented Feb 25, 2018 at 13:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .