Skip to content

Instantly share code, notes, and snippets.

@bkuri
Last active July 1, 2020 04:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bkuri/36cef71bcf30a5416b1c353cb81f38ed to your computer and use it in GitHub Desktop.
Save bkuri/36cef71bcf30a5416b1c353cb81f38ed to your computer and use it in GitHub Desktop.
#!/bin/bash
# seed-storage
# ============
# Seed an existing firebase cloud storage emulator instance. Run it inside your
# project's firebase folder, ie: ./seed-storage.sh <project-name> <dest-folder>
name=firebase_export # firebase export dir name
project=$1 # project name
root=$2 # export root path
uri=gs://$project.appspot.com/$name # cloud storage URI
trap "kill 0" EXIT
echo '[1/5] Preparing local/remote filesystems for seeding…'
# check if a remote file exists and wipe parent dir if needed
gsutil -q stat $uri/$name.overall_export_metadata
[ $? -eq 0 ] && gsutil -q rm -f ${uri}**
# create root path if needed
mkdir -p $root
echo '[2/5] Starting emulators in the background…'
# start emulator if port isn't open
lsof -i TCP:4000 &> /dev/null
[ $? -eq 0 ] || screen -S $name -d -m firebase emulators:start &
# wait until server is detected
lsof -i TCP:4000 &> /dev/null
while [ $? -ne 0 ]; do sleep 5; done
# exit on any error from now on
trap "exit" INT ERR
# create importable file/dir structure
firebase --non-interactive --project $project emulators:export --force $root &> /dev/null
echo "[3/5] Exporting firestore snapshot to $uri"
gcloud --verbosity=none firestore export $uri &> /dev/null
echo "[4/5] Downloading exported data to $root"
gsutil -q cp -r $uri $root
echo '[5/5] Cleaning up…'
screen -S $name -X quit
echo '\nDone! Use the following command to run the emulator:'
echo "firebase emulators:start --import=$root"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment