Skip to content

Instantly share code, notes, and snippets.

@diegofcornejo
Last active June 11, 2024 23:14
Show Gist options
  • Save diegofcornejo/5f361df9091925bea2b6d5e4cc7a0d82 to your computer and use it in GitHub Desktop.
Save diegofcornejo/5f361df9091925bea2b6d5e4cc7a0d82 to your computer and use it in GitHub Desktop.
Backup Redis dump to Cloudflare R2 using AWS CLI
#!/bin/bash
# Constants
HOST_NAME="$(hostname)"
DATE=$(date +%F-%H%M%S) # Format as 'YYYY-MM-DD-HHMMSS'
AWS_PROFILE="<YOUR_AWS_PROFILE>"
R2_ACCOUNT_ID="<YOUR_R2_ACCOUNT_ID>"
R2_BUCKET="<YOUR_BUCKET_NAME>"
R2_ENDPOINT="https://${R2_ACCOUNT_ID}.r2.cloudflarestorage.com"
REDIS_HOST="<YOUR_REDIS_HOST>"
REDIS_PORT="<YOUR_REDIS_PORT>"
REDIS_PASSWORD="<YOUR_REDIS_PASSWORD>"
REDIS_DUMP_FILE="redis.dump"
COMPRESSED_REDIS_DUMP_FILE="${REDIS_DUMP_FILE}.gz"
# Download the upstash-redis-dump binary from https://github.com/upstash/upstash-redis-dump/releases
UPTASH_REDIS_DUMP_BIN="/scripts/redis/upstash-redis-dump"
# AWS CLI command setup for Cloudflare R2
AWS_CLI="docker run --rm -v /tools/aws:/root/.aws -v $(pwd):/aws -e AWS_PROFILE=${AWS_PROFILE} amazon/aws-cli"
# Upload to R2 and cleanup
upload_and_cleanup() {
# Define R2 KEY
r2_key="${HOST_NAME}/redis/${REDIS_HOST}/${COMPRESSED_REDIS_DUMP_FILE}"
# Upload to R2
echo "Uploading ${COMPRESSED_REDIS_DUMP_FILE} to S3..."
$AWS_CLI s3api put-object --bucket "${R2_BUCKET}" --key "${r2_key}" --body "${COMPRESSED_REDIS_DUMP_FILE}" --endpoint-url ${R2_ENDPOINT}
# Cleanup local files
rm -f "${REDIS_DUMP_FILE}" "${COMPRESSED_REDIS_DUMP_FILE}"
}
# Backup Redis
echo "Backing up Redis..."
${UPTASH_REDIS_DUMP_BIN} -db 0 -host ${REDIS_HOST} -port ${REDIS_PORT} -pass ${REDIS_PASSWORD} > ${REDIS_DUMP_FILE}
# Check if backup was successful
if [ $? -eq 0 ]; then
echo "Backup completed successfully. File saved as ${REDIS_DUMP_FILE}."
else
echo "Backup failed."
exit 1
fi
# Compress the dump file
echo "Compressing ${REDIS_DUMP_FILE}..."
gzip -c ${REDIS_DUMP_FILE} > ${COMPRESSED_REDIS_DUMP_FILE}
# Check if compression was successful
if [ $? -eq 0 ]; then
echo "Compression completed successfully. File saved as ${COMPRESSED_REDIS_DUMP_FILE}."
else
echo "Compression failed."
rm -f "${COMPRESSED_REDIS_DUMP_FILE}"
exit 1
fi
# Upload and manage backups on S3
upload_and_cleanup || exit 1
echo "Backup process completed."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment