Using AWSCLI with Nextcloud

written by ushills on 2018-05-13

Amazon s3 is a cheap and secure place to store files and therefore ideal for off-site secure backups.

There is a convenient python command line interface for linux that you can install with the following. console

sudo pip install awscli

Go to IAM Management Console and create a new user and create access keys

sudo aws configure

AWS Access Key ID [None]: {enter access key}
AWS Secret Access Key [None]: {enter secret key}
Default region name [None]: eu-west-1
Default output format [None]: 

To list your buckets use

sudo aws s3 ls

Now create a script that you can call to backup your files to AWS s3. I use the example below, and store the script in /etc/s3backup/

sudo nano s3_backup_nextcloud.sh

#!/bin/bash
ocdatapath='/var/www/owncloud/data'
s3backuppath='s3://{s3 bucket name for backup}'
day=`date +%Y-%m-%d:%H:%M:%S`
echo 'Starting s3 backup for' ${day}

#command to backup to s3 server
#exclude the tmp directory and .* files
aws s3 sync ${ocdatapath} ${s3backuppath} --exclude "/tmp" --exclude "updater-backup" --exclude "lost+found" --exclude ".*" --exclude "~*" --acl private --sse > /var/log/s3backup_${day}.log 

Note that the suffix -sse adds server-side encryption to all uploaded files and therefore keeps your files secure but easily accessible without the need to store encryption keys and the logs of each backup are maintained under /var/log/s3backup_date.log.

Finally, make sure to make it executable with:

sudo chmod +x /etc/s3backup/s3_backup_nextcloud.sh
I have a crontab with the following which runs the backup every 6 hours.
0 0,6,12,18 * * * /etc/s3backup/s3_backup_nextcloud.sh