From b5d159e163dd4a5c02cd9d128ea60fd28724ce2d Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Fri, 17 Apr 2020 16:09:22 +1000 Subject: [PATCH] Add cache to mirror_db script Also added some better error handling around image syncing. --- script/mirror_db.sh | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/script/mirror_db.sh b/script/mirror_db.sh index ede932c821..c69db3490d 100755 --- a/script/mirror_db.sh +++ b/script/mirror_db.sh @@ -10,6 +10,7 @@ HOST="$1" BUCKET="$2" : ${DB_USER='ofn_user'} : ${DB_DATABASE='openfoodnetwork'} +: ${DB_CACHE_FILE="tmp/$HOST.sql"} DB_OPTIONS=( --exclude-table-data=sessions @@ -32,20 +33,30 @@ fi # -- Mirror database echo "Mirroring database..." -dropdb -h localhost -U ofn open_food_network_dev +dropdb -h localhost -U ofn open_food_network_dev --if-exists createdb -h localhost -U ofn open_food_network_dev -ssh -C "$HOST" "pg_dump -h localhost -U $DB_USER $DB_DATABASE ${DB_OPTIONS[@]}" | psql -h localhost -U ofn open_food_network_dev + +if [ -s "$DB_CACHE_FILE" ]; then + echo "Using cached dump '$DB_CACHE_FILE'." + psql -h localhost -U ofn open_food_network_dev < "$DB_CACHE_FILE" +else + echo "Downlowding dump to '$DB_CACHE_FILE'." + ssh -C "$HOST" "pg_dump -h localhost -U $DB_USER $DB_DATABASE ${DB_OPTIONS[@]}"\ + | tee "$DB_CACHE_FILE"\ + | psql -h localhost -U ofn open_food_network_dev +fi # -- Disable S3 echo "Preparing mirrored database..." $RAILS_RUN script/prepare_imported_db.rb # -- Mirror images -if [ -n "$BUCKET" ] && hash aws 2>/dev/null; then +if [ -n "$BUCKET" ]; then + if hash aws 2>/dev/null; then echo "Mirroring images..." aws s3 sync "s3://$BUCKET/public public/" - -else + else echo "Please install the AWS CLI tools so that I can copy the images from $BUCKET for you." echo "eg. sudo easy_install awscli" + fi fi