Skip to content

Instantly share code, notes, and snippets.

@rcrowe
Created August 29, 2018 10:43
Show Gist options
  • Select an option

  • Save rcrowe/e06c73c911d937d4e748609ad0958ebd to your computer and use it in GitHub Desktop.

Select an option

Save rcrowe/e06c73c911d937d4e748609ad0958ebd to your computer and use it in GitHub Desktop.
Dump a copy of all databases in CockroachDB
#!/bin/bash
set -e
# ---------
# Variables
# ---------
dbHost=$COCKROACH_HOST
if [[ -z $dbHost ]]; then
dbHost=localhost
fi
dbPort=$COCKROACH_PORT
if [[ -z $dbPort ]]; then
dbPort=26257
fi
dbUser=$COCKROACH_USER
if [[ -z $dbUser ]]; then
dbUser=root
fi
timestamp=$(date +%Y-%M-%d_%T)
# --------
# Commands
# --------
# List of databases
databasesCmd="psql
-h ${dbHost}
-p ${dbPort}
-U ${dbUser}
-d system
-c \"SELECT datname FROM pg_database WHERE datistemplate = false AND datname != 'system'\" -A -t"
eval $databasesCmd | while read db; do
echo Backing up $db
file=$db-$timestamp.sql.gz
# Dump SQL to file
cockroach dump --insecure $db | gzip > $file
# Cleanup
rm $file
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment