Created
November 29, 2017 16:50
-
-
Save crobertwatson/03fd5ac07a6838e82fa81dbbc2d87c87 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# pantheon-backup-to-gdrive.sh | |
# Script to backup Pantheon sites and copy to Google Drive | |
# | |
# Requirements: | |
# - Pantheon terminus cli | |
# - Valid terminus machine token | |
# - Google Drive folder ID | |
# - gdrive CLI by Petter Rasmussen (https://github.com/prasmussen/gdrive) | |
# The path to Terminus | |
TERMINUSPATH="/Users/username/vendor/bin" | |
# The Pantheon terminus user (email address) | |
TERMINUSUSER="john.doe@example.com" | |
# Site names to backup (e.g. 'site-one site-two') | |
SITENAMES="site-1 site-2 site-3" | |
# Site environments to backup (any combination of dev, test and live) | |
SITEENVS="dev live" | |
# Site elements to backup (any combination of files, database and code) | |
SITEELEMENTS="code database files" | |
# Local backup directory (must exist, requires trailing slash) | |
BACKUPDIR="/Users/username/Desktop/Backups/" | |
# Add a date and unique string to the filename | |
BACKUPDATE=$(date +%Y%m%d%s) | |
# This sets the proper file extension | |
FILEEXTENSION="tar.gz" | |
DBEXTENSION="sql.gz" | |
# The gdrive folder ID to save the backups to (must already exist) | |
GDRIVEFOLDERID="3MxyQqBH6BlAd5nfxWJAzIm3EAjUiNSHS" | |
# Set up a log file for output and errors | |
LOGFILE="backup."$BACKUPDATE".log" | |
exec &> $LOGFILE 2>&1 | |
# Clean out backup directory of last backup. We're only keeping one day's worth of files here. | |
rm -f $BACKUPDIR*.gz | |
# connect to terminus | |
#echo "Connecting to Terminus as " $TERMINUSUSER | |
#$TERMINUSPATH"/"terminus auth:login --email=$TERMINUSUSER | |
# iterate through sites to backup | |
for thissite in $SITENAMES; do | |
# iterate through current site environments | |
for thisenv in $SITEENVS; do | |
# iterate through current site elements | |
for thiselement in $SITEELEMENTS; do | |
if [ $thiselement == 'database' ] | |
then | |
#echo "DATABASE download command is:$TERMINUSPATH"/"terminus backup:get "$thissite"."$thisenv" --element="$thiselement" --to="$BACKUPDIR$thissite"."$thisenv"."$thiselement"."$BACKUPDATE"."$DBEXTENSION | |
$TERMINUSPATH"/"terminus backup:get $thissite.$thisenv --element=$thiselement --to=$BACKUPDIR$thissite.$thisenv.$thiselement.$BACKUPDATE.$DBEXTENSION | |
else | |
#echo "FILE download command is:#TERMINUSPATH"/"terminus backup:get "$thissite"."$thisenv" --element="$thiselement" --to="$BACKUPDIR$thissite"."$thisenv"."$thiselement"."$BACKUPDATE"."$FILEEXTENSION | |
$TERMINUSPATH"/"terminus backup:get $thissite.$thisenv --element=$thiselement --to=$BACKUPDIR$thissite.$thisenv.$thiselement.$BACKUPDATE.$FILEEXTENSION | |
fi | |
done | |
done | |
done | |
#echo "Syncing backup from" $BACKUPDIR "to Google Drive Folder ID" $GDRIVEFOLDERID | |
# upload the local backup directory contents to gdrive, deleting what was there prior to only keep one day's worth of files. | |
cd $BACKUPDIR | |
/usr/local/bin/gdrive sync upload --keep-local --delete-extraneous . $GDRIVEFOLDERID | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment