Skip to content

Instantly share code, notes, and snippets.

@marcelaraujo
Created June 4, 2024 10:27
Show Gist options
  • Save marcelaraujo/124a02399b69cb7c243406cb9dfc7bb9 to your computer and use it in GitHub Desktop.
Save marcelaraujo/124a02399b69cb7c243406cb9dfc7bb9 to your computer and use it in GitHub Desktop.
#!/bin/env bash
if ! [ -x "$(command -v aws)" ]; then
echo "[couchbase-backup] Error: git is not installed." >&2
exit 1
fi
if ! [ -x "$(command -v curl)" ]; then
echo "[couchbase-backup] Error: curl is not installed." >&2
exit 1
fi
if ! [ -x "$(command -v docker)" ]; then
echo "[couchbase-backup] Error: docker is not installed." >&2
exit 1
fi
if [ -z "${CLUSTER_ORG}" ]; then
echo "[couchbase-backup] Error: the CLUSTER_ORG is not set." >&2
exit 1
fi
AWS_REGION=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone | sed 's/\(.*\)[a-z]/\1/')
COUCHBASE_CREDENTIALS=$(aws secretsmanager get-secret-value --secret-id "couchbase/${CLUSTER_ORG}/credentials" --region=$AWS_REGION --query SecretString --output text)
COUCHBASE_USERNAME=$(echo $COUCHBASE_CREDENTIALS | jq -r '.username')
COUCHBASE_PASSWORD=$(echo $COUCHBASE_CREDENTIALS | jq -r '.password')
if ! [ -d "/tmp/couchbase/" ]; then
mkdir -p /tmp/couchbase/
fi
rm -rf /tmp/couchbase/*
TEMP_FOLDER=$(mktemp --directory --tmpdir=/tmp/couchbase)
docker run \
--rm \
-i \
--network host \
--privileged \
--platform=linux/amd64 \
--volume "${TEMP_FOLDER}:/tmp/couchbase" \
couchbase:community -- /bin/bash -s <<EOF
cbbackupmgr config -r couchbase -a /tmp/couchbase/ --disable-gsi-indexes --disable-ft-indexes --disable-ft-alias --disable-eventing --disable-analytics --disable-cluster-analytics
cbbackupmgr backup -r couchbase -a /tmp/couchbase/ -c localhost -u "${COUCHBASE_USERNAME}" -p "${COUCHBASE_PASSWORD}"
EOF
HOSTNAME=$(echo $HOSTNAME | sed 's/\./-/g')
COUCHBASE_FILE_BACKUP=$(date +"couchbase-${HOSTNAME}-%Y-%m-%d-%H-%M.tar.gz")
tar -czvf "${COUCHBASE_FILE_BACKUP}" "${TEMP_FOLDER}/"
S3_BUCKET_FQDN=$(aws secretsmanager get-secret-value --secret-id "couchbase/${CLUSTER_ORG}/s3-bucket-backups" --region=$AWS_REGION --query SecretString --output text | jq -r '.FQDN')
aws s3 cp "${COUCHBASE_FILE_BACKUP}" "${S3_BUCKET_FQDN}/${COUCHBASE_FILE_BACKUP}"
if [ -f "${COUCHBASE_FILE_BACKUP}" ]; then
rm -rf "${COUCHBASE_FILE_BACKUP}"
fi
if [ -d "${TEMP_FOLDER}" ]; then
rm -rf "${TEMP_FOLDER}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment