Machine snapshots are the "big" backup: the whole disk, for when something breaks. But sometimes you want something finer and cheaper: a daily copy of one folder (the website uploads, the documents, a database dump) somewhere off the machine, from which you can recover a single file without restoring anything. That is S3, and the AWS command-line tool has a command, aws s3 sync, that copies only what has changed. Scheduled every night, it is the cheapest and most reliable file backup you can have.
Before you start: what each thing means
- S3 and bucket
- S3 is Amazon's file store; a bucket is the "root folder" where you keep them, with a name that is unique worldwide.
- AWS CLI
- The AWS command-line tool: a program you install on the machine that lets you give AWS orders by typing commands.
- sync
- Compare a folder with the bucket and copy only what is new or changed. The first time it copies everything; after that, very little.
- Access key
- The username and password for programs. Here the machine will use it to talk to S3; it must have permission only for that bucket.
- Cron
- The Linux task scheduler: it runs a command at the time you tell it, every day.
- Versioning
- An S3 option that keeps the previous versions of a file. It protects you from a nightly copy overwriting something good with something broken.
Step by step
Create the bucket
S3 console, Create bucket, unique name (
copias-miempresa-web), same region as the machine (so transfer between the two does not use up your allowance or get charged), Block all public access on, versioning on and default encryption. Create.Create a key that can only write to that bucket
In IAM, create a policy with this content (change the bucket name) and a user
copias-webwith that policy and an access key, as explained in the guide on an IAM user with minimal permissions:{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": ["s3:ListBucket"], "Resource": "arn:aws:s3:::copias-miempresa-web" }, { "Effect": "Allow", "Action": ["s3:PutObject", "s3:GetObject", "s3:DeleteObject"], "Resource": "arn:aws:s3:::copias-miempresa-web/*" } ] }If that key ever leaked, the only thing anyone could do is touch that bucket. Nothing else.
Install the AWS tool on the machine
Over SSH, on Ubuntu or Debian:
sudo apt update && sudo apt install -y awscli(On Amazon Linux it comes preinstalled.) Configure it with the key from the previous step:
aws configureIt asks for the key ID, the secret, the region (
eu-west-1, for example) and the output format (leavejson). They are saved in the user's folder, readable only by that user.Test the command by hand
aws s3 sync /var/www/html/uploads s3://copias-miempresa-web/uploads --deleteThe first time takes a while, because it uploads everything; you will see each file go by. The following runs only upload what has changed. The
--deleteoption deletes from the bucket what is no longer in the folder, so the copy is a mirror; with versioning on, what was deleted is still recoverable. Check in the S3 console that the files are there.Add the database if you have one
So the backup also includes the database, dump it first to a file in the same folder that gets synchronised, for example with a small script
/usr/local/bin/copia.sh:#!/bin/bash mysqldump -u usuario -pCONTRASEÑA nombre_bd | gzip > /var/copias/bd-$(date +%F).sql.gz find /var/copias -name "bd-*.sql.gz" -mtime +7 -delete aws s3 sync /var/copias s3://copias-miempresa-web/bd aws s3 sync /var/www/html/uploads s3://copias-miempresa-web/uploads --deleteGive it execute permission (
chmod 700) and test it withsudo /usr/local/bin/copia.sh. Thefindline deletes dumps older than a week from the machine; in S3 they stay.Schedule it every night
sudo crontab -eAdd a line at the end for 3:30 in the morning (machine time, which is usually UTC):
30 3 * * * /usr/local/bin/copia.sh >> /var/log/copia.log 2>&1Save. The next day check
/var/log/copia.logand the date of the files in S3. Also set a lifecycle rule on the bucket that deletes old versions after 30 or 90 days, so it does not grow forever (guide in this section).
What can go wrong
"Access Denied" when synchronising
The policy does not cover what you are asking for: check that the bucket name is exact on both Resource lines, and that the first one (ListBucket) does not have /* and the second one does.
The backup takes hours
First run with many small files: normal. If it takes long every night, there are files that always change (caches, logs): exclude them with --exclude "cache/*".
The database password is in the script
Better to put it in the root user's ~/.my.cnf file with 600 permissions, and remove -pCONTRASEÑA from the command. And make the script readable only by root.
I deleted a file by mistake and the nightly backup deleted it too
That is what versioning is for: in the S3 console, turn on "Show versions" in the folder and recover the previous version. Without versioning it would have been lost: that is why it is turned on in step 1.
In Simplio
In Simplio you launch the nightly backup to S3 from the script catalogue: you choose the folder on the machine and the bucket, and Simplio creates the key with permissions only for that bucket, installs what is needed and schedules the task, without opening a terminal. The bucket appears in Simplio as a folder, with its versions, so you can recover a file in two clicks. And if one night the backup does not appear, it emails you.
The same task, in AWS and in Simplio
| In the AWS console | In Simplio | |
|---|---|---|
| Setting up the nightly backup | Bucket, IAM policy, key, install CLI, script, cron: 6 steps and a terminal, 25 minutes | "Backup to S3" script from the catalogue: you choose the folder and the bucket, Simplio creates the limited key and schedules the task |
| Knowing whether it ran last night | Look at the log on the machine | Email alert if a scheduled backup does not appear |
| Recovering a file | S3 console, search, versions | Open the bucket in Simplio as a folder and download |