Skip to content

Instantly share code, notes, and snippets.

@morganestes
Last active April 19, 2022 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morganestes/0596ffc9b7119b1ca452796de2d3e7d5 to your computer and use it in GitHub Desktop.
Save morganestes/0596ffc9b7119b1ca452796de2d3e7d5 to your computer and use it in GitHub Desktop.
Restore all backups on Pantheon
#!/usr/bin/env bash
##
# Generates the site.env from a Pantheon site URL.
##
function getPantheonSiteEnv() {
if [[ "${1}" == *"pantheonsite.io"* ]]; then
./pantheon-sitename.sh "${1}"
else
printf "Could not convert %s to a site.env" "${1}"
fi
}
##
# Restore backups one at a time for a Pantheon site.
#
# @param SITE_ENV The site name or Pantheon URL (e.g. https://dev-site-name.pantheonsite.io);
##
function panrestore() {
local SITE_ENV="${1}"
local _SITE="${SITE_ENV%%.*}"
local _ENV="${SITE_ENV##*.}"
# Convert URLs to site.env format
if [[ "${SITE_ENV}" == "http"* ]]; then
SITE_ENV="$(getPantheonSiteEnv "${SITE_ENV}")"
fi
# Set default environment to development since that's what's most often used.
if [[ "${SITE_ENV}" != *"."* ]]; then
_ENV="dev"
SITE_ENV="${_SITE}.${_ENV}"
fi
printf "Restoring backups for %s" "${SITE_ENV}"
for element in code files database; do
terminus backup:restore "${SITE_ENV}" --element $element --yes;
done
}
export getPantheonSiteEnv
panrestore "$@"
#!/usr/bin/env php
### Put this in the same directory as panrestore.sh and make it executable ###
<?php
/* Gets a <site>.<env> from a Pantheon site URL for use in commands. */
if (empty($argv[1]) || ! is_string($argv[1])) {
die('No URL provided.');
}
$url = $argv[1];
$_url = filter_var(trim($url), FILTER_VALIDATE_URL);
if (!$_url) {
die(sprintf('The URL "%s" is invalid.', $url));
}
$url = parse_url($_url, PHP_URL_HOST);
$re = "#^(?'env'dev|test|live)-(?'site'[\w\d-]+).pantheonsite.io#i";
$subst = '\2.\1';
$result = preg_replace($re, $subst, $url, 1);
echo (null === $result ? $url : $result) . PHP_EOL;
exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment