Help getting the last snapshot for a host

Hello,

I am trying write a script which restores the latest snapshot for a host/source, on another host.
I haven’t managed to select the latest snapshot, filtered by host name, source path.
Using Windows (batch or Powershell).

For example, here is the output of snapshot list --all:

username1@host1:v:\vm
  2021-10-18 08:19:33 EEST k6f9cac979705ff9417fe3f8c0352d4da 60.9 GB drwxrwxrwx files:69 dirs:20 (latest-2,hourly-2)
  2021-10-18 10:40:03 EEST k99cee5a785abf50a2e571e9693e7a508 60.9 GB drwxrwxrwx files:69 dirs:20 (latest-1,annual-1,monthly-1,weekly-1,daily-1,hourly-1)

username1@host1:v:\vm-arhiva
  2020-10-31 09:16:00 EET k72e23c67a8b57433ed33acdeea1d1420 47.9 GB drwxrwxrwx files:51 dirs:20 (latest-16)
  2020-10-31 10:13:00 EET ka2e06eb8d52b15c416afa41eeb0007f1 56.9 GB drwxrwxrwx files:58 dirs:22 (latest-15,monthly-6)
  [...]

I try to capture the latest snapshot id for username1@host1:v:\vm (k99cee5…). The best way seems to be to output json and filter in Powershell, but so far I’m unable to do it.

It would be something like:

$json.source.host="host1" 
and $json.source.userName="username1" 
and $json.source.path="v:\\vm" 

then select the latest rootEntry.obj

Here is the an excerpt from the json output:

[
    {
        "id": "5728d05a2c03fb6e606151bf6cba9de6",
        "source":
        {
            "host": "host1",
            "userName": "username1",
            "path": "v:\\vm"
        },
        "description": "",
        "startTime": "2021-10-18T08:19:33.2625338+03:00",
        "endTime": "2021-10-18T08:33:14.1647751+03:00",
        "rootEntry":
        {
            "name": "vm",
            "type": "d",
            "mode": "0777",
            "mtime": "2021-10-18T08:16:41.4182123+03:00",
            "obj": "k6f9cac979705ff9417fe3f8c0352d4da",
            "summ":
            {
                "size": 60877753327,
                "files": 69,
                "symlinks": 0,
                "dirs": 20,
                "maxTime": "2021-10-18T08:31:01.0633734+03:00",
                "numFailed": 0
            }
        }
    },
    ...

Full json here: ZeroBin.net

You could try this command:

kopia snapshot list --all -l | perl -00pe "s/^.*(k[0-9a-f]{32}).*\)\n/\1/s"

Thanks, this looks good and would capture the last snapshot.

However, I need to capture the last snapshot for a specific host (username1@host1:v:\vm), not the latest snapshot in the repository.

Thanks, this looks good and would capture the last snapshot.

Not correct. It will capture the last snapshot of each host/folder.

However, I need to capture the last snapshot for a specific host (username1@host1:v:\vm),

You could use kopia snapshot list to just list the snapshots for that specific host/folder (instead of using --all)

That’s great, I didn’t realize that I can use kopia snapshot list username1@host1:path :slight_smile:
Here is a more verbose solution using powershell, using your example:

$snapshot_source = "username1@host1:path"

Invoke-Expression "kopia snapshot list $snapshot_source" | Tee-Object -Variable output
$last_row = $output | Select -Last 1
$snapshot_regex="k[0-9a-f]{32}"
$found_match = $last_row | Out-String | % { $_ -match $snapshot_regex }

if ($found_match) {
    $last_snapshot = $Matches[0]
    Write-Output "Selected $last_snapshot"
}

Thanks!

Thanks for sharing this.

I use the following Shell function for this:

function findObjecttID {
  rfile=$1
  objectID=`/usr/bin/kopia snapshot list $rfile  \
             | grep -P "^\s*\d{4}-\d{2}-\d{2}" | tail -1 | cut -d" " -f6`
}

Try this

# Show last snapshot from a source ( user@host:path )
kopia snapshot list --all --reverse --max-results=1 "Administrator@WinServer:C:\users"