If you have a database on an EC2 instance, the question that comes up frequently is “how do I backup my database and where?”. The easiest option is to backup to Amazon’s S3 storage. This post shows you how to achive an automated database backup to S3 using a simple shell scripts that can be run on the database server.
Here’s a checklist of things that need to be in place for this to work:
- An IAM user with permissions to upload data into the S3 bucket
- The IAM user’s Access Key and Secret Key
- awscli (AWS Command line tool) installed and configured on the server
- An S3 bucket created to store the dB backups
1. Install awscli
Install awscli dependencies (if they do not already exist)
Run pip –version to see if your version of Linux already includes Python and pip
$ pip --version
If you don’t have pip, install pip as follows:
$ curl -O https://bootstrap.pypa.io/get-pip.py $ sudo python get-pip.py
Verify pip is successfuly installed:
$ pip --version pip 9.0.3 from /usr/local/lib/python2.7/dist-packages (python 2.7)
For detailed installation and troubleshooting go here: https://pip.pypa.io/en/stable/installing/
Installing the AWS CLI with Pip
Now use pip to install the AWS CLI:
$ pip install awscli --upgrade
Verify that the AWS CLI installed correctly.
$ aws --version aws-cli/1.14.63 Python/2.7.12 Linux/4.4.0-1049-aws botocore/1.9.16
Configuring AWS CLI
The aws configure command is the fastest way to set up your AWS CLI installation.
$ aws configure AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY Default region name [None]: us-east-1 Default output format [None]: json
For detailed AWS CLI configuration and installation options go here: https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html
Now that we have all the dependencies set up, lets go ahead and create the bash script to back up our database: