Skip to content

Instantly share code, notes, and snippets.

@adubovikov
Forked from inkrement/clickhousedump
Last active September 28, 2021 08:21
Show Gist options
  • Save adubovikov/9ef3aac41b0ad186d0377ea1eabd5aaf to your computer and use it in GitHub Desktop.
Save adubovikov/9ef3aac41b0ad186d0377ea1eabd5aaf to your computer and use it in GitHub Desktop.
dump all clickhouse databases and tables
#!/bin/bash
PASSWORD="--password=password"
DBLIST="db1 db2 db3"
OUTDIR=.
while read -r db ; do
while read -r table ; do
if [[ "$table" == ".inner."* ]]; then
echo "skip materialized view $table ($db)"
continue;
fi
echo "export table $table from database $db"
# dump schema
clickhouse-client ${PASSWORD} -q "SHOW CREATE TABLE ${db}.${table}" > "${OUTDIR}/${db}_${table}_schema.sql"
# dump
clickhouse-client ${PASSWORD} -q "SELECT * FROM ${db}.${table} FORMAT TabSeparated" | gzip > "${OUTDIR}/${db}_${table}_data.tsv.gz"
done < <(clickhouse-client ${PASSWORD} -q "SHOW TABLES FROM $db")
done < <(echo ${DBLIST})
@vladget
Copy link

vladget commented Sep 24, 2021

Recommended to use TabSeparatedRaw format for schema migration:
clickhouse-client ${PASSWORD} -q "SHOW CREATE TABLE ${db}.${table}" --format=TabSeparatedRaw > "${OUTDIR}/${db}_${table}_schema.sql"

@adubovikov
Copy link
Author

TabSeparatedRaw

Differs from TabSeparated format in that the rows are written without escaping.

@vladget
Copy link

vladget commented Sep 28, 2021

I dont know why, but TabSeparated broke my migration, when TabSeparatedRaw didn't.

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