Skip to content

Commit cf37de0

Browse files
committed
Store metrics files in a temp file
Revert to the behavior of storing the output of `find` in a temp file. This guarantees `tar` and `rm` operate on the same set of files to prevent data loss.
1 parent 859beb0 commit cf37de0

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

files/metrics_tidy

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,13 @@ paths_regex="$(IFS='|'; echo "${valid_paths[*]}")"
5252
find "$metrics_directory" -type f -ctime +"$retention_days" -delete
5353

5454
# Compress the remaining files in a Puppet service metrics directory.
55-
# The return code of a pipeline is the rightmost command, which means we trigger our trap if tar fails
56-
find "$metrics_directory" -type f -name "*json" | \
57-
tar --create --gzip --file "${metrics_directory}/${metrics_type}-$(date +%Y.%m.%d.%H.%M.%S).tar.gz" --files-from -
55+
# Store the list of files in a temp file so that `tar` and `rm` will operate on the same files
56+
metrics_tmp="$(mktemp)"
57+
find "$metrics_directory" -type f -name "*json" >"$metrics_tmp"
58+
tar --create --gzip --file "${metrics_directory}/${metrics_type}-$(date +%Y.%m.%d.%H.%M.%S).tar.gz" \
59+
--files-from "$metrics_tmp"
5860

5961
# Cleanup the backed up json files so that we do not duplicate files in the tarballs.
60-
find "$metrics_directory" -type f -name "*json" -delete
62+
# We can assume that the json files have no spaces as they are created by our scripts
63+
xargs -a "$metrics_tmp" rm
64+
rm "$metrics_tmp"

0 commit comments

Comments
 (0)