Skip to content

Instantly share code, notes, and snippets.

@aczietlow
Created August 4, 2017 17:32
Show Gist options
  • Save aczietlow/7dd899df16b4d44c93f060e61f53374a to your computer and use it in GitHub Desktop.
Save aczietlow/7dd899df16b4d44c93f060e61f53374a to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
## Creates a new docker database image using the backup of the live environment.
##
## Usage: fin update-database-image
# Abort if anything fails
set -e
SITE_DIRECTORY="default"
DOCROOT_PATH="${PROJECT_ROOT}/${DOCROOT}"
SITEDIR_PATH="${DOCROOT_PATH}/sites/${SITE_DIRECTORY}"
ARTIFACT_PATH="${PROJECT_ROOT}/artifacts"
TERMINUS="${PROJECT_ROOT}/bin/terminus"
# TODO set terminus authentication
# Console colors
red='\033[0;31m'
green='\033[0;32m'
green_bg='\033[42m'
yellow='\033[1;33m'
NC='\033[0m'
echo-red () { echo -e "${red}$1${NC}"; }
echo-green () { echo -e "${green}$1${NC}"; }
echo-green-bg () { echo -e "${green_bg}$1${NC}"; }
echo-yellow () { echo -e "${yellow}$1${NC}"; }
# Fetch the date of the most recent backup on the live environment.
# TODO make work on Linux too. BSD ships with a different date.
latest_backup_date=$(${TERMINUS} backup:info pied-piper.live --field date)
latest_backup_timestamp=$(date -j -f "%Y-%m-%d %H:%M:%S" "${latest_backup_date}" "+%s")
# If latest backup is older than 24 hours get a new one.
if [[ $(( $latest_backup_timestamp - 60*60*24 )) < $(date -j -v-1d +%s) ]]
then
echo -en "${green_bg} Fetching new backup ${NC}"
$TERMINUS backup:create pied-piper.live
fi
# Fetch a new db.
$TERMINUS backup:get pied-piper.live --element=db --to=$ARTIFACT_PATH/$latest_backup_timestamp.gz
gunzip $ARTIFACT_PATH/$latest_backup_timestamp.gz
# Import db into Drupal.
echo -en "${green_bg} Importing db into Drupal ${NC} "
# TODO make mysql_import faster.
fin db import $ARTIFACT_PATH/$latest_backup_timestamp.gz
fin drush rr
echo -en "${green_bg} Sanitize the database ${NC} "
fin docker exec $(fin docker-compose ps -q db) mysql -uroot -proot default -e "UPDATE users SET mail = CONCAT(name, '@localhost'), init = CONCAT(name, '@localhost'), pass = MD5(CONCAT('MILDSECRET', name)) WHERE mail NOT LIKE '%mindgrub.com' AND mail NOT LIKE '%piedpiper.c%';"
# TODO Make minor tweaks for local development.
cd $DOCROOT_PATH
fin drush en diff stage_file_proxy devel -y
# Seperate docksal solr command to configure solr to use local server.
fin solr
# Truncate the tables we don't actually need.
fin docker exec $(fin docker-compose ps -q db) mysql -uroot -proot default -e "TRUNCATE TABLE watchdog;"
fin docker exec $(fin docker-compose ps -q db) -uroot -proot default -e "TRUNCATE TABLE queue;"
fin drush rr --fire-bazooka
echo -en "${green_bg} Committing and push a new db image ${NC} "
fin stop db
fin docker commit $(fin docker-compose ps -q db) czietlow/pied-db
fin docker push czietlow/pied-db
fin up
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment