restore-orphaned-media.sh
· 3.1 KiB · Bash
Исходник
#!/bin/bash
srcPath='/mnt/Data/Фото/!_SORT/SamsungSort'
ownerId='b3f46eb5-81a6-4283-aade-d726823d7af4'
########################################################
source .env || exit 10
libHostPath="$UPLOAD_LOCATION"
libContainerPath='/usr/src/app/upload'
sql="SELECT \"id\",
\"originalPath\",
\"originalFileName\"
FROM asset
WHERE 1=1
AND \"libraryId\" IS NULL
AND \"status\" = 'active'
# AND \"type\" = 'IMAGE'
AND \"ownerId\" = '$ownerId'
"
echo "$sql"
IFS='|' rows=($(docker exec immich_postgres psql -U "$DB_USERNAME" -d "$DB_DATABASE_NAME" -c "$sql" -t))
awk <<< ${rows[@]} "
@load \"filefuncs\"
function join(array, start, end, sep, result)
{
if (sep == \"\") {
sep = \" \"
} else if (sep == SUBSEP) { # magic value
sep = \"\"
}
result = array[start]
for (i = start + 1; i <= end; i++) {
if (array[i] == sep) {
continue
}
result = result sep array[i]
}
return result
}
{
if (NR == 20) {
exit
}
type=\$4
guid=\$1
print \"[\" NR \"] \" type \" \" guid
immichUploadPath=\$2
print \"\t* Immich upload path:\t\" immichUploadPath
immichUploadPath=sprintf(\"%s/%s\", \"$libHostPath\", substr(immichUploadPath, length(\"$libContainerPath\") + 2, length(immichUploadPath)))
n=split(immichUploadPath, arr, \"/\")
immichUploadDir=join(arr, 1, n-1, \"/\")
immichUploadStat=stat(immichUploadPath, fdata)
immichUploadSize=fdata[\"size\"]
print \"\t* Immich local dir:\t\" immichUploadDir
print \"\t* Immich local path:\t\" immichUploadPath
print \"\t* Immich local size:\t\" immichUploadSize
n=split(immichUploadPath, arr, \"/\")
immichThumbDir=join(arr, 1, n-3, \"/\") \"/\" substr(guid, 1, 2) \"/\" substr(guid, 3, 2)
immichThumbPath=immichThumbDir \"/\" guid \"-preview.jpeg\"
gsub(/upload/, \"thumbs\", immichThumbPath)
immichThumbStat=stat(immichThumbPath, fdata)
immichThumbSize=fdata[\"size\"]
print \"\t* Immich thumb dir:\t\" immichThumbDir
print \"\t* Immich thumb path:\t\" immichThumbPath
print \"\t* Immich thumb size:\t\" immichThumbSize
origHostPath=sprintf(\"%s/%s\", \"$srcPath\", \$3)
origHostStat=stat(origHostPath, fdata)
origHostSize=fdata[\"size\"]
print \"\t* Original host path:\t\" origHostPath
print \"\t* Original host size:\t\" origHostSize
if (origHostStat != 0 && immichUploadStat != 0 && immichThumbStat != 0) {
print \">>> BADBADBAD\"
} else if (origHostStat == 0) {
if (immichUploadStat == 0) {
print \">>> ORIG OK\"
} else {
system(\"mkdir -p \" immichUploadDir)
system(\"cp -f \" origHostPath \" \" immichUploadPath)
print \">>> ORIG COPIED\"
}
if (immichThumbStat == 0) {
print \">>> THUMB OK\"
} else {
# system(\"mkdir -p \" immichThumbDir)
# system(\"cp -f \" origHostPath \" \" immichThumbPath)
# print \">>> THUMB COPIED\"
print \">>> THUMB SKIPPED\"
}
}
print \"\"
}"
| 1 | #!/bin/bash |
| 2 | |
| 3 | srcPath='/mnt/Data/Фото/!_SORT/SamsungSort' |
| 4 | ownerId='b3f46eb5-81a6-4283-aade-d726823d7af4' |
| 5 | |
| 6 | ######################################################## |
| 7 | |
| 8 | source .env || exit 10 |
| 9 | libHostPath="$UPLOAD_LOCATION" |
| 10 | libContainerPath='/usr/src/app/upload' |
| 11 | |
| 12 | sql="SELECT \"id\", |
| 13 | \"originalPath\", |
| 14 | \"originalFileName\" |
| 15 | FROM asset |
| 16 | WHERE 1=1 |
| 17 | AND \"libraryId\" IS NULL |
| 18 | AND \"status\" = 'active' |
| 19 | # AND \"type\" = 'IMAGE' |
| 20 | AND \"ownerId\" = '$ownerId' |
| 21 | " |
| 22 | echo "$sql" |
| 23 | |
| 24 | IFS='|' rows=($(docker exec immich_postgres psql -U "$DB_USERNAME" -d "$DB_DATABASE_NAME" -c "$sql" -t)) |
| 25 | |
| 26 | awk <<< ${rows[@]} " |
| 27 | @load \"filefuncs\" |
| 28 | function 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 |