diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-31 23:05:20 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-31 23:05:20 +0300 |
| commit | fb00c25dbef792f1110adf824f9cc3edb8739ef8 (patch) | |
| tree | bd5183428fd9bae524845a2b13bbe8b5883d79e4 /scripts/immich-export | |
| parent | d5673d7280a3487f8426463b8fa8849c06b1c895 (diff) | |
immich-export: download to .tmp then rename to avoid partial files
Downloads each asset to <filename>.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 <noreply@anthropic.com>
Diffstat (limited to 'scripts/immich-export')
| -rwxr-xr-x | scripts/immich-export | 16 |
1 files 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" |
