Backup files to Amazon S3

29 || 0  || 2018-04-14 06:01:56 || root || || Bash

The script below allows you to upload backup of your system to Amazon S3. There are several snippets related to this topic:

  1. Incremental backups (duplicity + rsync + gpg)
  2. Full backups (duplicity + rsync + gpg)
  3. Backup to Amazon S3
  4. Configuring GPG keys

Please check the code to find links for other snippets.

#!/bin/bash

# Incremental backups: https://snippets.yoctocloud.cc/snippets/2b0b3e2bc4714366b4e7afbc1ba555c4/
# Full backups: https://snippets.yoctocloud.cc/snippets/df3feac1d1e84945a5dd906219adfd55/
# Backups to S3: https://snippets.yoctocloud.cc/snippets/6c351cfa605a41c2bccd4588f4ee9b5a/
# Configuring GPG: soon

# Local directory with files
local_dir="/mnt/2tb/backups/hp_laptop/"

# Check and modify the lines below. There are credentials of Amazon S3, email and several other options (mail server, port, S3 bucket name, S3 region, etc)
s3_bucket="s3://hp_laptop_backups/"
s3_access_key="MY_S3_ACCESS_KEY"
s3_secret_key="MY_S3_SECRET_KEY"
s3_region="eu-west-1"
ses_username="MY_EMAIL_USERNAME"
ses_password="MY_EMAIL_PASSWORD"
ses_host="email-smtp.eu-west-1.amazonaws.com"
ses_port=587
ses_from="Backups <backups@example.com>"
ses_to="Firstname Lastname <firstname_lastname@gmail.com>"
ses_subject="Failed backup from server to S3"
ses_message="Latest attempt to copy backup from server to S3 was unsuccessful. Please find details below:"

# Remove old credentials and generate new file
rm -rf /root/.aws
mkdir -p /root/.aws
chown root:root /root/.aws
chmod 700 /root/.aws
echo "[default]" > /root/.aws/config
echo "region = $s3_region" >> /root/.aws/config
echo "[default]" > /root/.aws/credentials
echo "aws_access_key_id = $s3_access_key" >> /root/.aws/credentials
echo "aws_secret_access_key = $s3_secret_key" >> /root/.aws/credentials

# Copy files to S3
s3_sync=$(/usr/local/bin/aws s3 sync "$local_dir" "$s3_bucket" --delete --storage-class STANDARD_IA 2>&1)
exit_code=$?

# If backup is finished with errors for some reason, script will send email about that
if [ $exit_code != 0 ]
then
    ses_message="$ses_message\n\n\n$s3_sync"
    /usr/bin/sendEmail -f "$ses_from" -t "$ses_to" -u "$ses_subject" -m "$ses_message" -s "${ses_host}:${ses_port}" -o message-charset=utf-8 -o tls=yes -xu "$ses_username" -xp "$ses_password"
    /usr/bin/python2.6 /root/altel_disable.py
    rm -rf /root/.aws
    sleep 10
    exit 1
fi

# Remove credentials
rm -rf /root/.aws
sleep 10

exit 0