Backing Up My Logic Pro Projects to AWS S3 (Part 1)
Tags: python, aws, s3
Published: 2020-02-23 16:08:15

On a whim, just to play with Python and AWS, I thought of building a script to backup my Logic Pro projects to AWS S3. The premise of the script is that it will check a directory on my Macbook and upload any files that have been modified since the last time the file was uploaded to S3. This will run via a cron job (which, yes, only runs if my Macbook is awake). Anyway, let’s get started.

As for how I built it, some prerequisites:
<ul>
<li>PyCharm or any IDE</li>
<li>Python 3.0+</li>
<li>AWS account (<a href="https://aws.amazon.com/free/">free tier</a> should suffice for testing)</li>
<li>Basic understanding of Python/programming</li>
</ul>

This post won’t go into every code snippet as you can find it on my GitHub (coming soon), but will walk through the steps I took to assemble the components needed.

<h2>Objective</h2>
Compare files in a local folder with the respective files in S3 and zip and upload the local file if the local file is later than the one on S3 (last modified date time)

<h1>Part 1</h1>

In Part 1 of implementing this, we will simply go over how to connect to S3 via Python and how to get the last modified date time for a later comparison.

So, we’ll go through the snippets for these steps and/or you can skip all of this and just check out the code on GitHub (coming soon).

<h2>Connecting to S3</h2>

How to connect to AWS S3? Fortunately, AWS offers a SDK for Python called boto3, so you simply need to install this module and then import it into your script.

I am using Python 3.7 and pip3 so my command was <code>pip3 install boto3</code>

Import the following:
<code>
```python
# import AWS modules
import boto3
import logging
from botocore.exceptions import ClientError
```
</code>

You also need to install the aws cli module (AWS command line interface) so go ahead and do that with a <code>pip3 install aws cli</code>. 

Now, you will need to configure this aws cli tool to connect to your AWS account, so you should have an IAM user and group for the cli. Go to the IAM service in your AWS console to create that. 

<h3>Creating IAM User and Group for AWS CLI</h3>
First, create a Group and attach the AdministratorAccess policy. Then, create a User and assign the User to the Group you just created. Continue to the end of the wizard and eventually, you will see a button to download a CSV which will contain your credentials. These will be the credentials you use to configure your aws cli.

Have the Access Key ID and Secret Access Key ready and in your Terminal, type <code>aws configure</code>. You will then be asked for the Key ID and Access Key so simply paste the values from the CSV. You will also be asked for region name (input the region where your S3 buckets are). The last input field asks for output format (you can just press Enter).

Great! Now, your aws cli should be configured to connect to your AWS S3 bucket(s).

<h3>Create S3 Client and Resource</h3>
So, next we need to create a client and a resource for accessing S3 methods (the client and resource offer different methods based on usage, you can read more <a href="https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#s3">here</a>).

<code>
```python
# CREATE CLIENT AND RESOURCE FOR S3
s3Client = boto3.client('s3')
s3Resource = boto3.resource('s3')

# object for all s3 buckets
bucket_name = '' # NAME OF BUCKET GOES HERE, HARD CODED FOR NOW

# CREATE BUCKET OBJECT FOR THE BUCKET OF CHOICE
bucket = s3Resource.Bucket(bucket_name)
```
</code>

<h2>Retrieve object's last modified date time (upload time)</h2>

Ok, so what needs to be done to retrieve an object’s last modified date time?

I’ve written a method to loop through the objects in a bucket and call an AWS method to get the last modified time, shown below (yes, this can be written other ways as well).

{% gist https://gist.github.com/nakulkurane/87faa97651a98cba1e0ecb3c88cecf4e %}

The <code>bucket.objects.all()</code> returns a Collection which you can iterate through (read more on Collections <a href="https://boto3.amazonaws.com/v1/documentation/api/latest/guide/collections.html">here</a>).

So you can see I began looping through that and only calling the <b>last_modified</b> method if the S3 object contained <i>Logic_Projects</i> and <i>.zip</i> in the key name. 

If you’re not familiar, the key is simply how S3 identifies an Object. I am looking for <i>Logic_Projects</i> because I made a folder with that name and am also checking for <i>.zip</i> just in case I upload something else in the folder by accident (but this script will only upload zips to that folder, so it’s just a safety check).

If it passes those checks, then I proceed to the date time conversions. 

You’ll notice I am calling two other methods in this loop, called <b>utc_to_est</b> and stamp_to_epoch before I finally return a value. That is to make datetime comparisons easier later on. The <b>last_modified</b> method returns a date in UTC format so it could look like this: <i>2019-12-07 19:47:36+00:00</i>. 

When I am comparing modification times, I’d rather just compare numbers. The date AWS returns is in UTC, also referred to as GMT (Greenwich Mean Time).

So I added a function to convert this datetime to Eastern Standard Time, shown below:

{% gist https://gist.github.com/nakulkurane/ab23ba9d2615c9f7ac150aa55225a250 %}

Now that we’re in the right timezone, I want to convert it to a number, so I converted it to an Epoch time, which is the number of seconds that have elapsed since 00:00:00 UTC on January 1, 1970 (Why Epoch time is this date is not in the scope of this post).

{% gist https://gist.github.com/nakulkurane/220454e6281647024d5b686f659a12eb %}

OK, that's all for Part 1! In this post, we've <b>connected to AWS via Python (boto3)</b> and we've created a method to <b>get the last modified time (in seconds since Epoch and in EST) of an object in S3.</b>

In Part 2, we will go over comparing the last modified time of a file in S3 with the last modified time of that file locally.

<i>This post also appears on Medium (link below).</i>

{% medium https://medium.com/@nakulkurane/backing-up-my-logic-pro-projects-to-aws-s3-part-1-671d408dd39c?source=friends_link&sk=a1330cb7d10d45f48226960f4a955b99 %}