Ok. So I wanted to configure automatic backups of my photos.
While doing that, I noticed that the Immich Postgres directory was still on my storage VM, which I no longer need since I migrated everything to my Postgres server in a separate VM about a month ago.
So I deleted it.
After writing a little Ansible config for Borg backup, I went back to the SSH session on my storage VM to check the exact backup path.
I knew I had run ls before that, so I just pressed the up arrow, adjusted
the path, and pressed Enter.
But unortunately, it wasn’t ls - it was rm -rf for the Immich-Postgres
directory from earlier.
By cancelling right away, I was definitely able to salvage quit a bit.
The problem was that I didn’t have a backup - or rather, I didn’t have a recent backup, the last one I could finde was from three-quarters of a year ago …
First steps
Throughout this post, my Immich data directory is /mnt/nvme-01/immich.
First, I wanted to know how many files I currently have (Maybe rm had only
reached things like the database backups, which would have been harmless):
find /mnt/nvme-01/immich -type f | wc -l
That printed 19,715 files, but most of them were thumbnails.
So I needed a way to find out which originals are really missing.
Finding missing files
Thankfully, Immich stores checksums and file paths in the Postgres database, which I did not delete or damage (and I have backups of this db, too :) ).
So I just pulled all images I ever put into Immich out of Postgres, including the checksums, using this command:
sudo -u postgres psql immich -At -c "select encode(checksum,'hex'), \"originalPath\" from asset where \"deletedAt\" is null" > db_assets.txt
Now I had a list of all my images! It looks like so:
c17226aea0e2a3fe290744de6e49237a5164dca6|/data/library/admin/2017/06/2017-06-24-IMG_1564.jpg
4ec052beb3832763da8dd56bf102784d0695be41|/data/library/admin/2026/04/2026-04-08-PXL_20260408_094138894.jpg
19d6228a5d52d733a91d627aca6b4d42b055097d|/data/library/admin/2026/10/2026-10-10-MVI_3940.mp4
[...]
(it’s not sorted but that’s not my problem)
The second thing I needed is a list of all files that are still on the disk.
find /mnt/nvme-01/immich -type f | sed 's|^/mnt/nvme-01/immich|/data|' | LC_ALL=C sort > on_disk.txt
Two things to note here:
- I replaced the path
/mnt/nvme-01/immichwith/databecause that’s the path inside the immich container, and I wanted the same paths everywhere. - I set
LC_ALL=Cso thatsortorders lines by plain byte values instead of language-specific rules of my locale.
Now I had a list of all files on disk:
/data/backups/.immich
/data/backups/immich-db-backup-20261120T020000-v3.4.2-pg18.6.sql.gz
/data/backups/immich-db-backup-20261121T020000-v3.4.2-pg18.6.sql.gz
[...]
/data/library/admin/2021/06/2021-06-20-IMG_5667.jpg
/data/library/admin/2021/06/2021-06-20-IMG_5667.jpg.xmp
/data/library/admin/2021/06/2021-06-20-IMG_5668.jpg
/data/library/admin/2021/06/2021-06-20-IMG_5668.jpg.xmp
/data/library/admin/2021/06/2021-06-20-IMG_5669.jpg
/data/library/admin/2021/06/2021-06-20-IMG_5669.jpg.xmp
/data/library/admin/2021/06/2021-06-20-IMG_5670.jpg
/data/library/admin/2021/06/2021-06-20-IMG_5670.jpg.xmp
[...]
The database export contains checksums (which I need later) and paths. For the comparison I only need the path, so I cut off the checksums:
cut -d'|' -f2 db_assets.txt | LC_ALL=C sort > db_paths.txt
The file is now sorted and looks like this:
[...]
/data/library/admin/2026/10/2026-10-23-PXL_20261023_082011490.jpg
/data/library/admin/2026/10/2026-10-23-PXL_20261023_082053035.jpg
/data/library/admin/2026/10/2026-10-23-PXL_20261023_082057164.jpg
[...]
Now, I can list of all missing files:
LC_ALL=C comm -23 db_paths.txt on_disk.txt > missing_paths.txt
As you can read in the manpage, comm outputs three columns:
- Lines only in file 1
- Lines only in file 2
- Lines in both files
I don’t care about files that exist only on disk, and files that exist in both
places are fine. So I suppress columns 2 and 3 with -23.
Now I have a list of all misisng paths:
[...]
/data/library/admin/2026/10/2026-10-23-PXL_20261023_082053035.jpg
/data/library/admin/2026/10/2026-10-23-PXL_20261023_082057164.jpg
/data/library/admin/2026/10/2026-10-23-PXL_20261023_082732275.jpg
[...]
Aaaand that’s a lot …
tuxilio@ice-air > cat missing_paths.txt | wc -l
7094
Planning the recovery
When you delete a file, it (normally) only deletes the index but does not overwrite the file itself directly.
Luckily, there are programs like PhotoRec
that scans the whole disk and looks for whole files. Unfortunately,
extundelete found nothing on my (ext4) disk.
The problem is that PhotoRec can’t recover the original file names. But I have the checksums form the database, so I can match the recovered files against them :)
Combining hash and file path
I want a list of all missing files that includes both the checksum and the original path.
For that I use the awk command:
awk -F'|' 'NR==FNR{m[$0]=1;next} ($2 in m)' missing_paths.txt db_assets.txt > missing.txt
To understand it, you need to know:
awkreads files line to line.- For each line, it runs a small program that you define.
- The program consists of rules of the form
condition { action }. If the condition is true, the action runs.
Now to the program itself:
$0is the complete current line.-F'|'sets|as the field separator, so$1is the hash,$2the path.
The program has two rules: NR==FNR { m[$0]=1;next } and ($2 in m).
NRcounts lines across all input files. If the first file has 7000 lines, the second file starts at 7001.FNRcounts lines within the current file and starts at 1 again for each new file.
So, NR==FNR is true while reading the first file and false while reading the
second.
Rule 1
m[$0]=1 stores the whole line as a key in the array m. The first file is
missing_paths.txt, which contains only the paths of the missing files.
I only need to remember that a path exists, so the value can be anything.
I use 1.
next skips the remaining rules for this line and moves on to the next line.
Rule 2
Rule 2 only matters in the second file, because next skips it for the first.
($2 in m) checks wether the second filed is a key in m.
The second file is db_assets.txt, whose lines look like this:
<checksum>|<path>
If the path is in m, there is no action, so awk prints the whole line
(which is exactly what I want to archieve).
tldr;
The command keeps only the lines of db_assets.txt whose part is missing, and
I redirect the output to missing.txt.
This file now looks like so:
[...]
ec1c333b53732cdc2708dec9c5c8c50ff131627d|/data/library/admin/2026/08/2026-08-08-PXL_20260808_135916775.jpg
42de088bc8dfc6d22e42b94df369f2e91ed408f7|/data/library/admin/2026/04/2026-04-10-PXL_20260410_140939013.jpg
0b374fa696f724404f1f290522d2ff311641c4ac|/data/library/admin/2026/07/2026-07-25-PXL_20260725_113910942.jpg
[...]
Recovering files using PhotoRec
I don’t have a second disk I could attach quickly, so I mounted a directory on my laptop (which has enough free space) with SSHFS.
After unmounting the disk, I ran:
sudo mkdir /mnt/recovery
sudo sshfs tuxilio@<laptop>:/home/tuxilio/bak /mnt/recovery
To see which file types are missing, count the extensions:
sed 's/.*\.//' missing.txt | tr 'A-Z' 'a-z' | sort | uniq -c | sort -rn
Then I ran PhotoRec:
sudo photorec /log /d /mnt/recovery/ /dev/sdb1
/log tells PhotoRec to create a logfile, /d /mnt/recovery sets the output
directory to /mnt/recovery.
In the menu I selected only jpg, png, mov and mkv files to reduce
junk. (The mov signature also catches mp4 files.)
To speed things up, I scanned only the free space, not the whole partition.
The scan took about 6 hours. Afterwards I had a lot of files, including a lot of junk, for example thumbnails.
But there were also many originals in there!
Restoring files to their original position
The idea is: We now calculate hashes for all file found and compare them with the original hashes. If they match, we can move the files to their original position.
Before running the first time, copy the file missing.txt to
still_missing.txt.
#!/usr/bin/env bash
set -e
src="$1"
cur=still_missing.txt
# hash the files in the source directory
find "$src" -type f -print0 | xargs -0 sha1sum > src_hash.txt
awk -F'|' 'FILENAME==ARGV[1]{p[$1]=$2;next}
{h=substr($0,1,40); f=substr($0,43); if(h in p) print h"\t"f"\t"p[h]}' \
"$cur" src_hash.txt > plan_run.tsv
# copy files to new position
while IFS=$'\t' read -r h f d; do
rel="${d#/data/}"
mkdir -p "restore_tree/$(dirname "$rel")"
cp -n "$f" "restore_tree/$rel"
done < plan_run.tsv
cat plan_run.tsv >> all_plans.tsv
cut -f1 plan_run.tsv | sort -u > got.txt
awk -F'|' 'FILENAME==ARGV[1]{g[$1]=1;next} !($1 in g)' got.txt "$cur" > "$cur.new" && mv "$cur.new" "$cur"
echo "Found in $src: $(cut -f1 plan_run.tsv | sort -u | wc -l), still missing: $(wc -l < "$cur")"
cut -d'|' -f2 missing.txt | grep -E '^/data/(library|upload)/' | sort > still_missing_originals.txt
grep '^/data/library/' still_missing_originals.txt | awk -F/ '{print $5"/"$6}' | sort | uniq -c
This (not really well readable) script hashes all files in the directory given
as $1 and compares them with the hases in still_missing.txt. Matching
files are copied to their original relative path under restore_tree/, and
their entries are then removed from the list.
After matching the PhotoRec output, about 4,900 originals in library/ were
still missing. So I collected more sources (old backups, my phone, an old CD
with photos from 2017, …), hashed them with the script and macthed them the
same way.
One problem was that if you copy files from your phone via MTP and pres Ctrl+A in Dolphin while the folder is still loading (which takes a HUGE amount of time), not every photo is selected …
After a LOT of matching and waiting, not a single photo was missing anymore!
Syncing over
First a dry run, to see waht would be copied:
rsync -avn --ignore-existing restore_tree/ tuxilio@<storage-vm>:/mnt/nvme-01/immich/
and to sync:
rsync -av --ignore-existing restore_tree/ tuxilio@<storage-vm>:/mnt/nvme-01/immich/
After I ran the jobs Generate Thumbnails, Extract Metdata, Sidecar Metadata and Transcode Videos (which took also a lot time), everything
was working again!
Lessons learned
- MAKE BACKUPS!!!
- I need a
rm -rfcommand where, for example, you have to re-enter the path