Skip to content

Instantly share code, notes, and snippets.

@nlitsme
Created November 14, 2019 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nlitsme/dc309d3ba72a3de24da4f6c5bcce136e to your computer and use it in GitHub Desktop.
Save nlitsme/dc309d3ba72a3de24da4f6c5bcce136e to your computer and use it in GitHub Desktop.
Search timemachine backups and apfs snapshots for a specific file
#!/bin/bash
#
# Script which scans all your mounted timemachine volumes, and apfs snapshots for a specific file.
#
# Willem Hengeveld <itsme@xs4all.nl>
#
if [[ $(id -u) -ne 0 ]]; then
sudo $0 "$@"
exit $?
fi
tmutil listlocalsnapshots / | while read sn; do
mkdir "/Volumes/$sn" 2>/dev/null
mount_apfs -o ro -s "$sn" / "/Volumes/$sn" 2>/dev/null
done
bkdir=
while [[ -n "$1" ]]; do
cd "$(dirname "$1")"
realpath=$(pwd -P)
case "$realpath" in
/Volumes/*)
realpath="${realpath:9}"
;;
*)
realpath="$realpath"
;;
esac
# the perl code filters out duplicates from timemachine volume, based on inode number.
# list timemachine backups.
ls -ild /Volumes/*/Backups.backupdb/*/20*/*"$realpath/$(basename "$1")" | perl -nle 'if (/^\s*(\d+)\s(.*)/ && !$x{$1}++) { print $2; }'
# list local snapshots.
ls -ild /Volumes/com.apple.TimeMachine.20*"$realpath/$(basename "$1")" | perl -nle 'if (/^\s*(\d+)\s(.*)/ && !$x{$1}++) { print $2; }'
shift
done
for sd in /Volumes/com.apple.TimeMachine.20*; do
umount -f "$sd" 2>/dev/null
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment