Skip to content

Instantly share code, notes, and snippets.

@catleeball
Last active August 13, 2024 07:36
Show Gist options
  • Save catleeball/e85c2a1b71fe7b0c571c72310dcbb5ac to your computer and use it in GitHub Desktop.
Save catleeball/e85c2a1b71fe7b0c571c72310dcbb5ac to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Using gnu tar, make a tarball using pax format and drop unneeded metadata
# Also sha256sum all files before creating the tarball and include a text file of checksums in the tarball.
# Ref: https://www.gnu.org/software/tar/manual/html_chapter/Formats.html#Reproducibility
# Note that we delete mtime instead of setting it as the docs suggest above
# Note that gzip you may want --no-names; zstd has the same option, but it is enabled by default.
# Note that BSD tar (including mac os flavor) does not accept some of these options.
#
# License: CC0 v1.0 universal: https://creativecommons.org/publicdomain/zero/1.0/legalcode.txt
set -u
if [ "$#" -lt 2 ]; then
echo "At least two arguments are required. Output tar archive, followed by items to archive."
return 1
fi
BASENAME=$(basename -s ".tar" "$1")
SHAFILE="$BASENAME.sha256sums.txt"
for i in "${@:2}"; do
# Checksum all files in all directories to be archived
if [[ -d "$i" ]]; then
find "$i" -type f -exec sha256sum {} \; >> "$SHAFILE"
# Checksum all top-level files to be archive
elif [[ -f "$i" ]]; then
sha256sum "$i" >> "$SHAFILE"
fi
done
LC_ALL=C tar --sort=name --format=posix \
--pax-option=exthdr.name=%d/PaxHeaders/%f \
--pax-option=delete=atime,delete=ctime,delete=mtime \
--numeric-owner --owner=0 --group=0 \
--mode=go+u,go-w \
-cvf "$1" "${@:2}" "$SHAFILE"
#!/usr/bin/env bash
# Using gnu tar, make a tarball using pax format and drop unneeded metadata
#
# Ref: https://www.gnu.org/software/tar/manual/html_chapter/Formats.html#Reproducibility
#
# Note that we delete mtime instead of setting it as the docs suggest above
# Note that gzip you may want --no-names; zstd has the same option, but it is enabled by default.
# Note that BSD tar (including mac os flavor) does not accept some of these options.
#
# License: CC0 v1.0 universal: https://creativecommons.org/publicdomain/zero/1.0/legalcode.txt
set -eu
if [ "$#" -lt 2 ]; then
echo "At least two arguments are required. Output tar archive, followed by items to archive."
return 1
fi
LC_ALL=C tar --sort=name --format=posix \
--pax-option=exthdr.name=%d/PaxHeaders/%f \
--pax-option=delete=atime,delete=ctime,delete=mtime \
--numeric-owner --owner=0 --group=0 \
--mode=go+u,go-w \
-cvf "$1" "${@:2}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment