Backing Up Trellis Site to AWS s3 Bucket

As a freshman in Roots.io University, I’m still flying by the seat of my pants.

Backing up website database and files to Amazon s3 as Ben Word does seems like a more interesting approach than using a service like Updraft Plus, Jetpack or Infinite WP, so I’m giving it a go.

Beginning with Ben’s configuration updates:

Continuing the discussion from Handling backups with Trellis:

[quote=”benword, post:5, topic:9045″]
This is my solution for backing up sites to an S3 bucket:

site/scripts/backup.sh:

!/bin/bash

eval $(cat ../.env | sed ‘s/^/export /’)
export AWS_CONFIG_FILE=”/home/web/.aws/config”
SITE=”${DB_USER//_/.}”
ENVIRONMENT=”$WP_ENV”
TIMESTAMP=env TZ=America/Denver date +%Y-%m-%d-%H%M
ARCHIVE_PATH=/tmp/$SITE-$ENVIRONMENT-$TIMESTAMP
ARCHIVE_FILENAME=$SITE-$ENVIRONMENT-$TIMESTAMP.tar.gz
mkdir -p $ARCHIVE_PATH &&
cd /srv/www/$SITE/current && wp db export $ARCHIVE_PATH/db.sql &&
rsync -kavzP –exclude web/wp/ –exclude web/wp-config.php /srv/www/$SITE/current/web $ARCHIVE_PATH &&
rsync -kavzP /srv/www/$SITE/shared/uploads $ARCHIVE_PATH/web/app &&
tar -C $ARCHIVE_PATH -czf /tmp/$ARCHIVE_FILENAME . &&
/usr/local/bin/aws s3 cp /tmp/$ARCHIVE_FILENAME s3://my-unique-site-backups-bucket/$SITE/$ARCHIVE_FILENAME &&
rm -rf $ARCHIVE_PATH &&
rm /tmp/$ARCHIVE_FILENAME

Add to wordpress-setup/tasks/main.yml:

  • name: Setup nightly cron for S3 backup
    cron:
    name: “{{ item.key }} nightly S3 backup”
    minute: “0”
    hour: “12”
    user: “{{ web_user }}”
    job: “cd {{ www_root }}/{{ item.key }}/{{ item.value.current_path | default(‘current’) }}/scripts && ./backup-to-s3.sh > /dev/null 2>&1”
    cron_file: “backup-nightly-{{ item.key | replace(‘.’, ‘_’) }}”
    with_dict: “{{ wordpress_sites }}”
    when: env != ‘development’

Add to trellis/requirements.yml:

  • name: aws-cli
    src: https://github.com/dstil/ansible-aws-cli
    version: 5c1c14d9b0e6264605bd475c86d4ea67e7d5896f

Add to trellis/server.yml:

- { role: aws-cli, tags: [aws-cli] }

Add to trellis/group_vars/all/vault.yml:

vault_aws_access_key_id: xxxxxxx
vault_aws_secret_access_key: “xxxxxxx”
[/quote]

Leave a Reply

Your email address will not be published. Required fields are marked *