From fb00c25dbef792f1110adf824f9cc3edb8739ef8 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Tue, 31 Mar 2026 23:05:20 +0300 Subject: immich-export: download to .tmp then rename to avoid partial files Downloads each asset to .tmp first, renaming to the final path only on success. Also cleans up stale .tmp files from aborted previous runs. Co-Authored-By: Claude Sonnet 4.6 --- scripts/immich-export | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/scripts/immich-export b/scripts/immich-export index 4e32ae1..0d6d1c2 100755 --- a/scripts/immich-export +++ b/scripts/immich-export @@ -73,14 +73,18 @@ download_assets() { continue fi - if curl -sf -o "$dest" \ + # Download to a temp file first; rename to final dest only on success. + # This prevents half-downloaded files from being mistaken as complete. + local tmp="$dest.tmp" + if curl -sf -o "$tmp" \ -H "x-api-key: $api_key" \ "$IMMICH_URL/api/assets/$asset_id/original"; then + mv "$tmp" "$dest" ((downloaded++)) || true echo " [$n/$total] downloaded: $filename" else echo " [$n/$total] ERROR: failed to download $asset_id ($filename)" >&2 - rm -f "$dest" # Remove partial file on failure + rm -f "$tmp" # Remove partial temp file on failure fi done < "$asset_list" @@ -104,6 +108,14 @@ download_assets_for_account() { discover_assets "$api_key" > "$asset_list" echo " Found $(wc -l < "$asset_list") images total" + # Clean up any leftover .tmp files from a previously aborted run + local stale_tmp + stale_tmp=$(find "$account_dir" -name "*.tmp" 2>/dev/null | wc -l) + if [[ "$stale_tmp" -gt 0 ]]; then + echo " Cleaning up $stale_tmp leftover .tmp file(s) from previous run..." + find "$account_dir" -name "*.tmp" -delete + fi + echo " Phase 2: downloading..." download_assets "$api_key" "$account_dir" "$asset_list" -- cgit v1.2.3