-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdsave
More file actions
executable file
·69 lines (58 loc) · 1.72 KB
/
dsave
File metadata and controls
executable file
·69 lines (58 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env bash
set -euo pipefail
trap 'echo "Interrupted. Killing all jobs..."; kill 0; exit 1' SIGINT
WORKDIR="docker-tars"
mkdir -p "$WORKDIR"
# Get image for container
get_image_for_container() {
docker inspect --format '{{.Config.Image}}' "$1"
}
# Determine image list
if [[ "$#" -gt 0 ]]; then
echo "Using images from specified containers: $*"
mapfile -t IMAGES < <(
for container in "$@"; do
if docker ps --format '{{.Names}}' | grep -qx "$container"; then
get_image_for_container "$container"
else
echo "Warning: container '$container' not found or not running" >&2
fi
done | sort -u
)
else
echo "No container names provided. Using all running containers."
mapfile -t CONTAINERS < <(docker ps --format '{{.Names}}')
if [[ ${#CONTAINERS[@]} -eq 0 ]]; then
echo "No running containers found. Exiting."
exit 0
fi
mapfile -t IMAGES < <(
for container in "${CONTAINERS[@]}"; do
get_image_for_container "$container"
done | sort -u
)
fi
if [[ ${#IMAGES[@]} -eq 0 ]]; then
echo "No images to save. Exiting."
exit 0
fi
echo "Saving ${#IMAGES[@]} unique images to $WORKDIR..."
save_image() {
local image="$1"
local safe_name
safe_name=$(echo "$image" | tr '/:@' '_' | tr -c '[:alnum:]_-' '_')
local tar_path="$WORKDIR/$safe_name.tar"
echo "Saving $image to $tar_path"
docker image save "$image" -o "$tar_path"
}
export -f save_image
export WORKDIR
if command -v parallel &>/dev/null; then
printf "%s\n" "${IMAGES[@]}" | parallel --will-cite save_image {}
else
echo "Warning: GNU parallel not found. Falling back to sequential execution." >&2
for image in "${IMAGES[@]}"; do
save_image "$image"
done
fi
echo "Image tars saved in $WORKDIR"