Skip to content

Backup Strategies

Backup to Amazon S3 Bucket

If you have your own AWS account, you can send files and backups directly to an S3 bucket.

Install the AWS CLI inside your Stratus instance using the Bundled Install. Use the Install the AWS CLI without Sudo (Linux, macOS, or Unix) method

$ curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip"
$ unzip awscli-bundle.zip
$ ./awscli-bundle/install -b ~/bin/aws

Run export to make the ~/bin path available

export PATH=~/bin:$PATH

Add the same to the ~/.bash_profile file so it will be available every time you log in. By default the ~/.bash_profile file does not exist.

Run aws configure and follow the prompts entering the appropriate region and access key for your AWS account.

Now you can run commands like aws s3 ls to show your existing buckets and manage other AWS resources. In this example, we will create a new one named my-magento-backup with:

magemojo@svc-magentoperformance-io:~$ aws s3 mb s3://my-magento-backup
make_bucket: my-magento-backup
magemojo@svc-magentoperformance-io:~$ aws s3 ls
2019-03-19 13:23:50 my-magento-backup

Now we can start uploading a backup, as a reminder, you can create backups in Magento 2 with the built-in system. Here we'll simply create a database dump and tar up the web root. If you have a very large site creating an archive can take a long time

#from document root
$n98-magerun2 db:dump s3_backup.sql
$tar -zcvf s3_backup_3_19_2019.tar.gz /srv/public_html/

Now we have an archive, we can upload it to the S3 bucket.

magemojo@svc-magentoperformance-io:~$ aws s3 ls
2019-03-19 13:23:50 my-magento-backup
magemojo@svc-magentoperformance-io:~$ aws s3 cp s3_backup_3_19_2019.tar.gz s3://my-magento-backup
upload: ./s3_backup_3_19_2019.tar.gz to s3://my-magento-backup/s3_backup_3_19_2019.tar.gz

You will see it upload with progress details. Once uploaded, you should also see the archive within the bucket in the AWS Console or web interface. S3 Bucket

Well done! Learn more about S3 commands from Amazon. From here you can throw together a script that creates a backup and uploads it to S3. Then set a cron to automatically run it, in a script similar to below:

#!/bin/bash
#S3 Backup Example

date="$(date "+%m-%d-%Y")"
bucket_name="my-magento-backup"
magento_root="/srv/public_html"

#create databasedump
echo "Creating database dump..."
/usr/local/bin/n98-magerun2 --root-dir=$magento_root db:dump db-backup-$date.sql

#create archive of webroot, excluding var
echo "Creating tar archive of files and database dump..."
tar --exclude=/srv/public_html/var/* -zcf $date-backup.tar.gz /srv/public_html/

#upload to s3
echo "Uploading to S3..."
aws s3 cp $date-backup.tar.gz s3://my-magento-backup

#clean up
echo "Removing local files and cleaning up..."
rm $date-backup.tar.gz
rm $magento_root/db-backup-$date.sql

echo "Done!"

The above would output:

magemojo@svc-magentoperformance-io:~$ ./backup.sh
Creating database dump...


  Dump MySQL Database


Start dumping database db_cded1u2ypqu to file db-backup-03-19-2019.sql
Finished
Creating tar archive of files and database dump...
tar: Removing leading `/' from member names
Uploading to S3...
upload: ./03-19-2019-backup.tar.gz to s3://my-magento-backup/03-19-2019-backup.tar.gz
Removing local files and cleaning up...
Done!

Dropbox backups

The official Dropbox CLI utility from Dropbox is not currently supported on Stratus. But you can use this 3rd party script to push files to a Dropbox folder with the proper access tokens.  See https://github.com/andreafabrizi/Dropbox-Uploader for more details.