Last active
June 10, 2020 23:05
-
-
Save gburgett/4f15ee5ec70a5ea9f095b6dcf39f7909 to your computer and use it in GitHub Desktop.
A bash script to do a contentful export of every space you have access to, and upload it to AWS S3. Can be run as a cron job.
This file contains hidden or 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 | |
# Copyright 2020 Watermark Community Church | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
COLOR_NC='\033[0m' # No Color | |
COLOR_LGREEN='\033[1;32m' | |
COLOR_GRAY='\033[1;30m' | |
COLOR_LGRAY='\033[0;37m' | |
COLOR_RED='\033[0;31m' | |
logv() { | |
[[ -z "$VERBOSE" ]] && return 0; | |
>&2 echo -e "${COLOR_GRAY}$*${COLOR_NC}" || true | |
} | |
logerr() { | |
>&2 echo -e "${COLOR_RED}$*${COLOR_NC}" | |
} | |
log() { | |
>&2 echo -e "${COLOR_LGREEN}$*${COLOR_NC}" | |
} | |
panic() { | |
logerr "$@" | |
exit -1; | |
} | |
curlv() { | |
execv curl "$@" | |
} | |
execv() { | |
logv "$@" | |
"$@" | |
} | |
notify() { | |
if command -v osascript >/dev/null 2>&1; then | |
osascript -e "display notification \"$1\" with title \"Contentful Export\"" | |
else | |
log "$1" | |
fi | |
} | |
timestamp() { | |
date +"%T" | |
} | |
# Write your usage | |
usage() { | |
echo "$0 [space ID] [environment] | |
Downloads all your data from Contentful and uploads it to AWS s3. | |
If space ID and optionally environment are given, it downloads only that space. | |
If no arguments are given, it downloads every space you have access to. | |
" && \ | |
grep " .)\ #" $0; exit 0; | |
} | |
# Parse additional args here | |
while getopts ":hv" arg; do | |
case $arg in | |
v) # Verbose mode - extra output | |
VERBOSE=true | |
FLAGS="$FLAGS -v" | |
;; | |
h | *) # Display help. | |
usage | |
exit 0 | |
;; | |
esac | |
done | |
shift $(($OPTIND - 1)) | |
set -e | |
command -v contentful >/dev/null 2>&1 || npm i -g contentful-cli | |
command -v contentful >/dev/null 2>&1 || panic "contentful-cli must be installed!" | |
command -v aws >/dev/null 2>&1 || panic "aws must be installed!" | |
if cat ~/.contentfulrc.json | grep -q "managementToken"; then | |
logv "Already logged in to contentful" | |
else | |
if [[ -z "$CONTENTFUL_MANAGEMENT_TOKEN" ]]; then | |
execv contentful login | |
else | |
execv contentful login --management-token "$CONTENTFUL_MANAGEMENT_TOKEN" | |
fi | |
fi | |
do_export() { | |
local space="$1" | |
local environment="$2" | |
local filename="contentful-export-${space}-latest.json" | |
[[ -z "$environment" ]] || filename="contentful-export-${space}-${environment}-latest.json" | |
local dirname="contentful-export/${space}" | |
mkdir -p "$HOME/$dirname" | |
local environment_flags="" | |
[[ -z "$environment" ]] || environment_flags="--environment-id \"$environment\"" | |
( | |
cd "$HOME/$dirname" | |
execv contentful space export --space-id "$space" \ | |
$environment_flags \ | |
--content-only --download-assets \ | |
--content-file "$filename" > /dev/null | |
) | |
# No delete - retain old assets. | |
execv aws s3 sync --size-only "$HOME/$dirname" "s3://wcc-prod-backups/$dirname" 1>&2 | |
echo "$HOME/$dirname" | |
} | |
do_full_export() { | |
SPACES=($(contentful space list | egrep -o '[[:alnum:]]{12}')) | |
SPACE_COUNT="${#SPACES[@]}" | |
notify "Begin full export of ${SPACE_COUNT} spaces" | |
for space in "${SPACES[@]}"; | |
do | |
bash "$0" $FLAGS "$space" & | |
done | |
JOBS=$(jobs -p) | |
while read -r job; do | |
[[ -z "$job" ]] && continue; | |
logv "waiting for $job..." | |
wait $job | |
SPACE_COUNT=$((SPACE_COUNT-1)); | |
notify "Export complete - ${SPACE_COUNT} exports pending" | |
done <<< "$JOBS" | |
} | |
if [[ -z "$1" ]]; then | |
time do_full_export | |
notify "Full export complete" | |
else | |
do_export "$@" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment