AWS S3 Backup Guide 2, aws-cli

If you already have aws-cli installed and want to send a file to Amazon S3 it’s really easy:

aws s3 cp ARCHIVE_TO_SEND s3://BUCKET_NAME/ARCHIVE_TO_SEND

 

Sending files ‘by hand’ is all good, but if you are serious about backups you need to automate the process, here is a quick and dirty backup script.
It lists, tars, bzips and individually uploads all the directories under the path you specify o the script.
 
You just need to set the variables at lines 5, 6, 7 and 8.


#!/bin/bash
CURRENT_DATE=$(date +%Y-%m-%d)
CURRENT_DIR=$(pwd)

BUCKET_NAME="<NameOfTheAwsBucket>"
AWS_ACCESS_KEY_ID="<AWSAccessUser>"
AWS_SECRET_ACCESS_KEY="<AWSAccessKey>"
BACKUPROOT="<FULL_PATH_TO_BACKUP_ROOT_FOLDER>"

# Exporting so aws-cli have access to the values
export AWS_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY

cd $BACKUPROOT
DIR_LIST=$(ls -d */ | tr -d "/")
for i in $DIR_LIST
do
    FILE_NAME=./$SUFIX-$i-$CURRENT_DATE.tar.gz
    tar czf  $FILE_NAME $i
    aws s3 mv $FILE_NAME s3://$BUCKET_NAME/$CURRENT_DATE/$FILE_NAME
done

cd $CURRENT_DIR
echo -e "Backup $CURRENT_DATE Done\n"