Skip to content

Instantly share code, notes, and snippets.

@khsthomas
Forked from inkrement/clickhousedump
Last active July 12, 2024 09:11
Show Gist options
  • Select an option

  • Save khsthomas/d1dbdc2020baf154c76905abfb557916 to your computer and use it in GitHub Desktop.

Select an option

Save khsthomas/d1dbdc2020baf154c76905abfb557916 to your computer and use it in GitHub Desktop.
dump all clickhouse databases and tables
#!/bin/bash
#should set command alias
#alias cclient="clickhouse-client -u {default or user} --password '{well-escaped-password}' "
OUTDIR=.
while read -r db ; do
while read -r table ; do
if [ "$db" == "system" ]; then
echo "skip system db"
continue 2;
fi
if [[ "$table" == ".inner."* ]]; then
echo "skip materialized view $table ($db)"
continue;
fi
echo "export table $table from database $db"
# dump schema
cclient -q "SHOW CREATE TABLE ${db}.${table}" > "${OUTDIR}/${db}_${table}_schema.sql"
# dump
cclient -q "SELECT * FROM ${db}.${table} FORMAT TabSeparated" | gzip > "${OUTDIR}/${db}_${table}_data.gz"
done < <(cclient -q "SHOW TABLES FROM $db")
done < <(cclient -q "SHOW DATABASES")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment