Skip to content

Instantly share code, notes, and snippets.

@ryanarlan0622
Created February 8, 2019 22:51
Show Gist options
  • Save ryanarlan0622/3039a26360351b95ef6c8b7a5396917c to your computer and use it in GitHub Desktop.
Save ryanarlan0622/3039a26360351b95ef6c8b7a5396917c to your computer and use it in GitHub Desktop.
#! /bin/bash
set -eu
output_dir="container-logs"
timestamp=$(date +%s)
generate_logs() {
container_ids=($(docker ps | awk '{if (NR!=1)print $1}'))
container_names=($(docker ps | awk '{if (NR!=1)print $NF}'))
if [[ $container_ids ]]; then
for i in "${!container_ids[@]}"; do
# echo "${container_ids[i]}" "${container_names[i]}"
docker logs ${container_ids[i]} > $output_dir/${container_names[i]}.log
done
else
echo "No containers running"
fi
}
mkdir -p $output_dir
generate_logs
tar czf container-logs-$timestamp.tar.gz $output_dir
rm -rf $output_dir
echo "Log bundle container-logs-$timestamp.tar.gz has been successfully created."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment