Last active 1753882767

IDK why but some of my photos and videos removed from HDD but meta info still persists in DB. I made a script which restores some photos from another directory by original filenames

restore-orphaned-media.sh Raw
1#!/bin/bash
2
3srcPath='/mnt/Data/Фото/!_SORT/SamsungSort'
4ownerId='b3f46eb5-81a6-4283-aade-d726823d7af4'
5
6########################################################
7
8source .env || exit 10
9libHostPath="$UPLOAD_LOCATION"
10libContainerPath='/usr/src/app/upload'
11
12sql="SELECT \"id\",
13 \"originalPath\",
14 \"originalFileName\"
15FROM asset
16WHERE 1=1
17 AND \"libraryId\" IS NULL
18 AND \"status\" = 'active'
19 # AND \"type\" = 'IMAGE'
20 AND \"ownerId\" = '$ownerId'
21"
22echo "$sql"
23
24IFS='|' rows=($(docker exec immich_postgres psql -U "$DB_USERNAME" -d "$DB_DATABASE_NAME" -c "$sql" -t))
25
26awk <<< ${rows[@]} "
27@load \"filefuncs\"
28function join(array, start, end, sep, result)
29{
30 if (sep == \"\") {
31 sep = \" \"
32 } else if (sep == SUBSEP) { # magic value
33 sep = \"\"
34 }
35
36 result = array[start]
37 for (i = start + 1; i <= end; i++) {
38 if (array[i] == sep) {
39 continue
40 }
41 result = result sep array[i]
42 }
43 return result
44}
45{
46 if (NR == 20) {
47 exit
48 }
49
50 type=\$4
51 guid=\$1
52 print \"[\" NR \"] \" type \" \" guid
53
54 immichUploadPath=\$2
55 print \"\t* Immich upload path:\t\" immichUploadPath
56
57 immichUploadPath=sprintf(\"%s/%s\", \"$libHostPath\", substr(immichUploadPath, length(\"$libContainerPath\") + 2, length(immichUploadPath)))
58 n=split(immichUploadPath, arr, \"/\")
59 immichUploadDir=join(arr, 1, n-1, \"/\")
60 immichUploadStat=stat(immichUploadPath, fdata)
61 immichUploadSize=fdata[\"size\"]
62 print \"\t* Immich local dir:\t\" immichUploadDir
63 print \"\t* Immich local path:\t\" immichUploadPath
64 print \"\t* Immich local size:\t\" immichUploadSize
65
66 n=split(immichUploadPath, arr, \"/\")
67 immichThumbDir=join(arr, 1, n-3, \"/\") \"/\" substr(guid, 1, 2) \"/\" substr(guid, 3, 2)
68 immichThumbPath=immichThumbDir \"/\" guid \"-preview.jpeg\"
69 gsub(/upload/, \"thumbs\", immichThumbPath)
70 immichThumbStat=stat(immichThumbPath, fdata)
71 immichThumbSize=fdata[\"size\"]
72 print \"\t* Immich thumb dir:\t\" immichThumbDir
73 print \"\t* Immich thumb path:\t\" immichThumbPath
74 print \"\t* Immich thumb size:\t\" immichThumbSize
75
76 origHostPath=sprintf(\"%s/%s\", \"$srcPath\", \$3)
77 origHostStat=stat(origHostPath, fdata)
78 origHostSize=fdata[\"size\"]
79 print \"\t* Original host path:\t\" origHostPath
80 print \"\t* Original host size:\t\" origHostSize
81
82 if (origHostStat != 0 && immichUploadStat != 0 && immichThumbStat != 0) {
83 print \">>> BADBADBAD\"
84 } else if (origHostStat == 0) {
85 if (immichUploadStat == 0) {
86 print \">>> ORIG OK\"
87 } else {
88 system(\"mkdir -p \" immichUploadDir)
89 system(\"cp -f \" origHostPath \" \" immichUploadPath)
90 print \">>> ORIG COPIED\"
91 }
92 if (immichThumbStat == 0) {
93 print \">>> THUMB OK\"
94 } else {
95 # system(\"mkdir -p \" immichThumbDir)
96 # system(\"cp -f \" origHostPath \" \" immichThumbPath)
97 # print \">>> THUMB COPIED\"
98 print \">>> THUMB SKIPPED\"
99 }
100 }
101
102 print \"\"
103}"
104