Skip to content

Instantly share code, notes, and snippets.

@voxxit
Last active October 31, 2023 16:13
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save voxxit/d1e4b4e8240ad3174d0f to your computer and use it in GitHub Desktop.
Save voxxit/d1e4b4e8240ad3174d0f to your computer and use it in GitHub Desktop.
Downloads RDS slow query logs for the last 24 hours using the AWS CLI
#!/bin/bash
instanceID=$1
date=$(date +%Y%m%d)
function downloadLog () {
local log=$1
aws rds download-db-log-file-portion \
--output text \
--db-instance-identifier $instanceID \
--log-file-name $log
}
downloadLog slowquery/mysql-slowquery.log > slow-$date.log
for i in $(seq 0 23); do
downloadLog slowquery/mysql-slowquery.log.$i >> slow-$date.log
done
# Runs pt-query-digest from the Percona toolkit, if found
if which pt-query-digest >/dev/null; then
pt-query-digest slow-$date.log > slow-digest-$date.log
fi
@cutewalker
Copy link

hi, as the docs[http://docs.aws.amazon.com/cli/latest/reference/rds/download-db-log-file-portion.html] says, when log file is greater than 1M, the cmd will download only portion, isn't it ?

@steviesteve
Copy link

You'll need to include --starting-token 0 to download-db-log-file-portion to get the full log file

@surasint
Copy link

surasint commented Sep 3, 2017

--starting-token 0 does not help to get the full log file if the file is bigger than 1 M

Copy link

ghost commented Mar 2, 2018

Thank you for your awesome command 💯

@fguillen
Copy link

The aws cli command that worked for me was:

aws rds download-db-log-file-portion --db-instance-identifier <db-instance-id> --profile <profile-name> --log-file-name slowquery/mysql-slowquery.log --output text > ~/slow-queries.log

@shubhamgoyal41
Copy link

This one worked for me:

#!/bin/bash

instanceID=$1
date=$2

function downloadLog () {
  local log=$1

  aws rds download-db-log-file-portion \
    --output text \
    --db-instance-identifier $instanceID \
    --log-file-name $log \
    --region ap-south-1
}

downloadLog slowquery/mysql-slowquery.log > slow-$date.log

for i in $(seq -w 0 23); do
  downloadLog slowquery/mysql-slowquery.log.$date.$i >> slow-$date.log
done

usage : ./download-slow-query-log.sh my-db-1 '2020-07-18'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment