Skip to content

Instantly share code, notes, and snippets.

@harshil07
Last active March 18, 2021 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save harshil07/441f4782b2504b4044b4 to your computer and use it in GitHub Desktop.
Save harshil07/441f4782b2504b4044b4 to your computer and use it in GitHub Desktop.
AWS RDS backups
#!/bin/bash -
export AWS_ACCESS_KEY=<your aws access key>
export AWS_SECRET_KEY=<your aws secret>
date_current=`date -u +%Y-%m-%d`
aws rds describe-db-snapshots --snapshot-type "automated" --db-instance-identifier <db_instance_name> | grep `date +%Y-%m-%d` | grep rds | tr -d '",' | awk '{ print $2 }' > /tmp/sandbox-snapshot.txt
snapshot_name=`cat /tmp/<db_instance_name>-snapshot.txt`
target_snapshot_name=`cat /tmp/<db_instance_name>-snapshot.txt | sed 's/rds://'`
aws rds copy-db-snapshot --source-db-snapshot-identifier $snapshot_name --target-db-snapshot-identifier $target_snapshot_name-copy > /home/ubuntu/rds-snapshot-$date_current.log 2>&1
echo "-------------" >> /home/ubuntu/$date_current-results.txt
cat /home/ubuntu/rds-snapshot-$date_current.log >> /home/ubuntu/$date_current-results.txt
cat /home/ubuntu/$date_current-results.txt | mail -s "[Daily RDS Snapshot Backup] $date_current" <email@foo.com>
rm /home/ubuntu/$date_current-results.txt
rm /home/ubuntu/rds-snapshot-$date_current.log
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1427229307000",
"Effect": "Allow",
"Action": [
"rds:CreateDBSnapshot",
"rds:CopyDBSnapshot",
"rds:DeleteDBSnapshot",
"rds:DescribeDBInstances",
"rds:DescribeDBSnapshots",
"rds:DescribeReservedDBInstances"
],
"Resource": [
"arn:aws:rds"
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment