Skip to content

Instantly share code, notes, and snippets.

@xandout
Created August 13, 2020 15:42
Show Gist options
  • Save xandout/cae454a8e8d866ff131e1e89ebd6b899 to your computer and use it in GitHub Desktop.
Save xandout/cae454a8e8d866ff131e1e89ebd6b899 to your computer and use it in GitHub Desktop.
Python script using boto3 to copy ElastiCache to S3
#!/bin/env python
import boto3
import os
import time
from datetime import datetime
cluster_name = os.environ["CLUSTER_NAME"]
dest_bucket = os.environ["DEST_BUCKET"]
env_name = os.environ["ENV"]
aws_region = os.environ["AWS_REGION"]
start_time = datetime.now()
backup_time = start_time.strftime("%Y-%m-%d-%H-%M-%S")
elasticache = boto3.client('elasticache', region_name=aws_region)
backup_name = f'{env_name}-{cluster_name}-{backup_time}'
print(f'Backing up {cluster_name} to backup named {backup_name}')
response = elasticache.create_snapshot(
CacheClusterId=cluster_name,
SnapshotName=backup_name
)
while True:
time.sleep(10)
response = elasticache.describe_snapshots(
SnapshotName=backup_name,
ShowNodeGroupConfig=False
)
status = response['Snapshots'][0]['SnapshotStatus']
print(f'{backup_name} is {status}')
if status == "available":
response = elasticache.copy_snapshot(
SourceSnapshotName=backup_name,
TargetSnapshotName=f'{env_name}/{backup_name}',
TargetBucket=dest_bucket
)
break
time_to_complete = datetime.now() - start_time
print(f'{backup_name} was copied to s3://{dest_bucket}/{env_name}/{backup_name} in {time_to_complete}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment