Guide: one-click restore the most recent Time Machine backup of a file

I hope it’s OK to promote blog posts I write regarding use of macOS.

The latest one shows how to add an item to the Finder “right-click” context menu, and the example calls a script to restore the most recent backup of a file.

  1. right-click on a file
  2. select “Restore from Time Machine”
  3. file is trashed (recoverable using Trash’s Put Back feature)
  4. most recent backup of that file is restored from Time Machine

This takes a seconds or two, depending on the size of the file in question. Most importantly this saves interrupting my workflow by having to opening the Time Machine interface and zooming through all those windows looking for the file.

You could also install the script to be able to use it from Terminal command line.

I have a ton more such scripts that I run from the context menu, let me know if any are of interest and I’ll share them!

4 Likes

Sorry for not seeing this sooner, @gingerbeardman—it’s very cool! I tried setting it up using FiScript here, but struck out on getting the shell scripts updated to match my system.

Have you thought about doing it via Shortcuts, which I think can do the necessary bits, and which might be able to query the user for the various volume names necessary to construct the tmutil command?

I’ll look into the FiScript @ace.

There’s a way I can automatically figure out the TM path (using tmutil itself) I just haven’t had time to do that yet. The result will be one script for USB or NAS or any combination of multiple drives.

Shortcuts: I don’t have any real interest in it on macOS at this time. But I’ll give it a look at some point soon.

Aha! Figuring out the Time Machine path using tmutil would be fabulous, since that’s where I suspect my problems with modifying the script happened.

I’m not a big fan of Shortcuts either, but where I’m going with this is a way of packaging your shell script into something that any Monterey-using Mac can use with minimal fuss.

If we can get it there, I’ll definitely be writing a full article about this, since it’s shocking to me that no one before you has realized we need a “restore this particular file” command for Time Machine before. Or at least implemented it!

1 Like

I’ll look into it and get back to you.

1 Like

Making progress with a Shortcut for this:

  1. some paths are still hard-coded
    • confident I can replace this with generated paths
  2. still reliant on external trash command
    • problem, there is no built-in equivalent
    • I’m thinking on first run the Shortcut could check to see if trash is installed and if not install it (download shell script and make it executable). Worries me that this is a bit invasive.

Quick Actions menu:

My setup is quite peculiar (System installed on internal SSD, User folder on external SSD). That makes my version of these scripts quite different what is probably needed for the standard user. Will have to spend some time setting up a TM backup of an old laptop to see how the folders are different to be able to create one script that works for all.

1 Like

Could you not have an AppleScript block along the lines of:

tell application "Finder" to move theFile to trash
-- or
tell application "Finder" to move POSIX file "/path/to/file" to trash

?

2 Likes

I’ll try it.

The goal is for the trashed file to have Put Back capability from inside the trash.

1 Like

I have a clean-up script that runs every night. The key line is

delete file (tThisFile as alias)

I can verify that 1) tThisFile is moved to the Trash by this command, and 2) right-clicking the file inside the Trash reveals a context menu with a “Put Back” option, which works as expected when selected.

4 Likes

Awesome. I’ll continue my work on this.

1 Like