Skip to content

Instantly share code, notes, and snippets.

@EarthmanWeb
Last active November 14, 2024 03:01
Show Gist options
  • Select an option

  • Save EarthmanWeb/b4216c2000ea0a1c5b38d693ebe58649 to your computer and use it in GitHub Desktop.

Select an option

Save EarthmanWeb/b4216c2000ea0a1c5b38d693ebe58649 to your computer and use it in GitHub Desktop.
BASH script to sync WPMS DB in Pantheon - Live to Dev
#!/usr/bin/env bash
# Description: Sync Remote LIVE DB to dev
## Example: bash /path/to/dbsync-live-to-dev // will sync the latest backup DB
## Example: bash /path/to/dbsync-live-to-dev url "https://pantheon-backups.com/backup-url" // will sync from URL provided
## Example: bash /path/to/dbsync-live-to-dev clone // will clone the Live site
# change ENV to your target env ID
ENV=dev
# change 'siteID' to your Pantheon site ID
SITE='siteID'
PANTHEONENV="$SITE.$ENV"
# replace this with the ID of your site from the Dashboard URL
SITE_UUID=3872g783-4323-8s73-hj62-jd8927329894
# replace these with your live(old) and target(new) domains
OLDDOMAIN='mydomain.com'
OLDDOMAINWWW='www.mydomain.com'
NEWDOMAIN="$SITE-$ENV.devdomain.com"
NEWDOMAINWWW="$SITE-$ENV.devdomain.com"
# set -x
set -eu -o pipefail
echo -e
echo 'Logging into terminus:'
echo -e
terminus auth:login
if [ -z "${1:-}" ]
then
echo -e
echo -e "No param set, so we'll import the latest backup of the live site"
set -- "latest"
echo -e
fi
case "$1" in
bypass)
echo -e
echo -e "...bypassing import of db to $1"
echo -e
;;
# clone will export fresh, but not good to do on live site
# do this only if the param is set to 'clone'
clone)
echo -e
echo -e "...cloning live db to $ENV"
terminus env:clone-content --db-only $SITE.live $ENV
;;
# use a manual backup url from the Pantheon dashboard if param is 'url'
url)
backupUrl=$2
# if url is not set, then exit
if [ -z "$backupUrl" ]
then
echo -e
echo -e "You need to set the url of the backup you want to import as the second param"
echo -e
exit 1
fi
echo -e
echo -e "...importing backup from url to $ENV"
echo -e
terminus import:database $PANTHEONENV $backupUrl --yes --verbose
;;
# this will get the url for the last backup of the live site, and import it, if param is omitted
*)
echo -e
echo -e "...importing latest backup of live db to $PANTHEONENV"
echo -e
backupUrl=$(terminus backup:get $SITE_UUID.live --element=db)
terminus import:database $PANTHEONENV $backupUrl --yes --verbose
;;
esac
echo -e
echo -e 'cc & set site url:'
echo -e
# search replace ref's in target DB
terminus remote:wp $PANTHEONENV -- network meta update 1 siteurl "https://$NEWDOMAINWWW" --url=$OLDDOMAINWWW
terminus env:cc $PANTHEONENV
echo -e
echo -e 'search replace urls:'
echo -e
terminus remote:wp $PANTHEONENV -- search-replace $OLDDOMAIN $NEWDOMAIN wp_site --url=$OLDDOMAINWWW --skip-plugins
terminus remote:wp $PANTHEONENV -- search-replace $OLDDOMAINWWW $NEWDOMAINWWW wp_blogs wp_sitemeta --url=$OLDDOMAINWWW --skip-plugins
terminus remote:wp $PANTHEONENV -- search-replace .$OLDDOMAIN .$NEWDOMAIN wp_blogs wp_sitemeta --url=$NEWDOMAINWWW --skip-plugins
terminus remote:wp $PANTHEONENV -- search-replace $OLDDOMAINWWW $NEWDOMAINWWW 'wp_*options' --include-columns=option_value --all-tables-with-prefix --url=$NEWDOMAINWWW --skip-plugins
terminus remote:wp $PANTHEONENV -- search-replace .$OLDDOMAIN .$NEWDOMAIN 'wp_*options' --include-columns=option_value --all-tables-with-prefix --url=$NEWDOMAINWWW --skip-plugins
### For large DB's - these ones below usually time out - to just get things up and running, they are not required
# terminus remote:wp $PANTHEONENV -- search-replace $OLDDOMAINWWW $NEWDOMAINWWW 'wp_posts' --include-columns=post_content --url=$NEWDOMAINWWW --skip-plugins
# terminus remote:wp $PANTHEONENV -- search-replace $OLDDOMAINWWW $NEWDOMAINWWW 'wp_posts' --include-columns=guid --url=$NEWDOMAINWWW --skip-plugins
# terminus remote:wp $PANTHEONENV -- search-replace .$OLDDOMAIN .$NEWDOMAIN 'wp_posts' --include-columns=post_content --url=$NEWDOMAINWWW --skip-plugins
# terminus remote:wp $PANTHEONENV -- search-replace .$OLDDOMAIN .$NEWDOMAIN 'wp_posts' --include-columns=guid --url=$NEWDOMAINWWW --skip-plugins
# terminus remote:wp $PANTHEONENV -- search-replace $OLDDOMAINWWW $NEWDOMAINWWW 'wp_*_posts' --include-columns=post_content,guid --all-tables-with-prefix --url=$NEWDOMAINWWW --skip-plugins
# terminus remote:wp $PANTHEONENV -- search-replace .$OLDDOMAIN .$NEWDOMAIN 'wp_*_posts' --include-columns=post_content,guid --all-tables-with-prefix --url=$NEWDOMAINWWW --skip-plugins
# terminus remote:wp $PANTHEONENV -- search-replace $OLDDOMAINWWW $NEWDOMAINWWW 'wp_postmeta' --include-columns=meta_value --url=$NEWDOMAINWWW --skip-plugins
# terminus remote:wp $PANTHEONENV -- search-replace .$OLDDOMAIN .$NEWDOMAINWWW 'wp_postmeta' --include-columns=meta_value --url=$NEWDOMAINWWW --skip-plugins
# terminus remote:wp $PANTHEONENV -- search-replace $OLDDOMAINWWW $NEWDOMAINWWW 'wp_*_postmeta' --include-columns=meta_value --all-tables-with-prefix --url=$NEWDOMAINWWW --skip-plugins
# terminus remote:wp $PANTHEONENV -- search-replace .$OLDDOMAIN .$NEWDOMAINWWW 'wp_*_postmeta' --include-columns=meta_value --all-tables-with-prefix --url=$NEWDOMAINWWW --skip-plugins
# terminus remote:wp $PANTHEONENV -- search-replace www.$NEWDOMAINWWW $NEWDOMAINWWW 'wp_*postmeta' --include-columns=post_content,guid --all-tables-with-prefix --url=$NEWDOMAINWWW --skip-plugins
# terminus remote:wp $PANTHEONENV -- search-replace www.$NEWDOMAINWWW $NEWDOMAINWWW 'wp_*posts' --include-columns=post_content,guid --all-tables-with-prefix --url=$NEWDOMAINWWW --skip-plugins
# echo -e
# echo -e 'deactivate SSO plugin':
# echo -e
# terminus remote:wp $PANTHEONENV -- plugin deactivate 'miniorange-saml-20-single-sign-on' --network --url=$NEWDOMAINWWW
echo -e
echo -e 'clear remote cache'
echo -e
terminus remote:wp $PANTHEONENV -- cache flush
terminus remote:wp $PANTHEONENV -- rewrite flush --url=$NEWDOMAINWWW
timestamp=$(date +%s)
echo -e
echo -e "Next, clear your local cookies and files, and Login at: https://$SITE-$ENV.$ENV.$NEWDOMAIN/wp-login.php?nocache=$timestamp"
echo -e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment