Skip to content

Instantly share code, notes, and snippets.

@autoize
Created September 8, 2017 17:19
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 14 You must be signed in to fork a gist
  • Save autoize/d39dd9d00150ed3eac69bbfa4dd1b21b to your computer and use it in GitHub Desktop.
Save autoize/d39dd9d00150ed3eac69bbfa4dd1b21b to your computer and use it in GitHub Desktop.
NextCloud Backup to Amazon S3
#!/bin/sh
# NextCloud to Amazon S3 Backup Script
# Author: Autoize (autoize.com)
# This script creates an incremental backup of your NextCloud instance to Amazon S3.
# Amazon S3 is a highly redundant block storage service with versioning and lifecycle management features.
# Requirements
# - Amazon AWS Account and IAM User with AmazonS3FullAccess privilege
# - Python 2.x and Python PIP - sudo apt-get install python && wget https://bootstrap.pypa.io/get-pip.py && sudo python get-pip.py
# - s3cmd installed from PyPI - sudo pip install s3cmd
# Name of Amazon S3 Bucket
s3_bucket='s3_bucket_name'
# Path to NextCloud installation
nextcloud_dir='/var/www/nextcloud'
# Path to NextCloud data directory
data_dir='/media/external/CloudDATA'
# MySQL/MariaDB Database credentials
db_host='localhost'
db_user='nextclouduser'
db_pass='secret'
db_name='nextcloud'
# Check if running as root
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
echo 'Started'
date +'%a %b %e %H:%M:%S %Z %Y'
# Put NextCloud into maintenance mode.
# This ensures consistency between the database and data directory.
sudo -u www-data php $nextcloud_dir/occ maintenance:mode --on
# Dump database and backup to S3
mysqldump --single-transaction -h$db_host -u$db_user -p$db_pass $db_name > nextcloud.sql
s3cmd put nextcloud.sql s3://$s3_bucket/NextCloudDB/nextcloud.sql
rm nextcloud.sql
# Sync data to S3 in place, then disable maintenance mode
# NextCloud will be unavailable during the sync. This will take a while if you added much data since your last backup.
# If upload cache is in the default subdirectory, under each user's folder (Default)
s3cmd sync --recursive --preserve --exclude '*/cache/*' $data_dir s3://$s3_bucket/
# If upload cache for all users is stored directly as an immediate subdirectory of the data directory
# s3cmd sync --recursive --preserve --exclude 'cache/*' $data_dir s3://$s3_bucket/
sudo -u www-data php $nextcloud_dir/occ maintenance:mode --off
date +'%a %b %e %H:%M:%S %Z %Y'
echo 'Finished'
@DoloresHA
Copy link

Thank you for this amazing script! Would have literally never been able to figure this out on my own.

I've made a few changes on my fork. Will you consider incorporating them?
I had to run b2 authorize-account inside the script in order to get it to work. I also gather that you need to run b2 authorize-account each day that you use the CLI anyways.

@TobjasR
Copy link

TobjasR commented Jun 1, 2021

@autoize do you think you could add support for OpenSSL encryption? =)

@Rid-lin
Copy link

Rid-lin commented Jul 16, 2022

@autoize do you think you could add support for OpenSSL encryption? =)

@TobjasR
What's the point?
If the S3 from amazon already has built-in encryption.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment