Skip to content

Instantly share code, notes, and snippets.

@asanikovich
Last active March 30, 2021 14:29
Show Gist options
  • Save asanikovich/84080ac4be0155366aae9a634ba8706d to your computer and use it in GitHub Desktop.
Save asanikovich/84080ac4be0155366aae9a634ba8706d to your computer and use it in GitHub Desktop.
A simple script to print the size of each your Redis databases. (Calculate size per Redis database)
#!/usr/bin/env bash
human_size() {
awk -v sum="$1" ' BEGIN {hum[1024^3]="Gb"; hum[1024^2]="Mb"; hum[1024]="Kb"; for (x=1024^3; x>=1024; x/=1024) { if (sum>=x) { printf "%.2f %s\n",sum/x,hum[x]; break; } } if (sum<1024) print "<1kb"; } '
}
# change your connect settings (DSN)
# redis_cmd='redis-cli -h host -p 6379 -a pass'
redis_cmd='redis-cli'
for db in `$redis_cmd INFO keyspace | grep -Eo 'db[0-9]{1,4}' | grep -Eo '[0-9]{1,4}'`; do
sum=0
for k in `$redis_cmd -n $db keys "*"`; do
size=$($redis_cmd -n $db debug object $k | perl -wpe 's/^.+serializedlength:([\d]+).+$/$1/g')
case $size in
''|*[!0-9]*) size=0 ;;
esac
sum=$(( $sum + $size))
done;
echo Database \#$db size `human_size $sum`
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment