Skip to content

Instantly share code, notes, and snippets.

@kylebradshaw
Forked from bkuri/seed-storage.sh
Created July 1, 2020 04:26
Show Gist options
  • Save kylebradshaw/c897f4b28c2baa2a57625f3210dd976d to your computer and use it in GitHub Desktop.
Save kylebradshaw/c897f4b28c2baa2a57625f3210dd976d to your computer and use it in GitHub Desktop.

Revisions

  1. Bernardo Kuri revised this gist Jun 8, 2020. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions seed-storage.sh
    Original file line number Diff line number Diff line change
    @@ -2,12 +2,12 @@

    # seed-storage
    # ============
    # Seed an existing firebase cloud storage emulator instance. Run inside your project's firebase folder.
    # syntax: ./seed-storage.sh <project-name> <dest-folder>
    # 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 for both firestore & cloud storage
    root=$2 # export root path
    uri=gs://$project.appspot.com/$name # cloud storage URI

    trap "kill 0" EXIT
  2. Bernardo Kuri revised this gist Jun 8, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion seed-storage.sh
    Original file line number Diff line number Diff line change
    @@ -45,5 +45,5 @@ gsutil -q cp -r $uri $root
    echo '[5/5] Cleaning up…'
    screen -S $name -X quit

    echo 'Done! Use the following command to run the emulator:'
    echo '\nDone! Use the following command to run the emulator:'
    echo "firebase emulators:start --import=$root"
  3. Bernardo Kuri revised this gist Jun 8, 2020. 1 changed file with 1 addition and 4 deletions.
    5 changes: 1 addition & 4 deletions seed-storage.sh
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,6 @@
    # Seed an existing firebase cloud storage emulator instance. Run inside your project's firebase folder.
    # syntax: ./seed-storage.sh <project-name> <dest-folder>

    # variables
    name=firebase_export # firebase export dir name
    project=$1 # project name
    root=$2 # export root path for both firestore & cloud storage
    @@ -41,12 +40,10 @@ 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 -m cp -r $uri $root
    gsutil -q cp -r $uri $root

    echo '[5/5] Cleaning up…'
    screen -S $name -X quit

    echo 'Done! Use the following command to run the emulator:'
    echo "firebase emulators:start --import=$root"

    exit 0
  4. Bernardo Kuri revised this gist Jun 7, 2020. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions seed-storage.sh
    Original file line number Diff line number Diff line change
    @@ -46,4 +46,7 @@ gsutil -q -m cp -r $uri $root
    echo '[5/5] Cleaning up…'
    screen -S $name -X quit

    echo 'Done! Use the following command to run the emulator:'
    echo "firebase emulators:start --import=$root"

    exit 0
  5. Bernardo Kuri created this gist Jun 7, 2020.
    49 changes: 49 additions & 0 deletions seed-storage.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    #!/bin/bash

    # seed-storage
    # ============
    # Seed an existing firebase cloud storage emulator instance. Run inside your project's firebase folder.
    # syntax: ./seed-storage.sh <project-name> <dest-folder>

    # variables
    name=firebase_export # firebase export dir name
    project=$1 # project name
    root=$2 # export root path for both firestore & cloud storage
    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 -m cp -r $uri $root

    echo '[5/5] Cleaning up…'
    screen -S $name -X quit

    exit 0