#!/bin/bash # Backup script for Nextcloud in a Kubernetes cluster PROGRAM_NAME='backup_nextcloud' printlog () { printf '%s: %s\n' "${PROGRAM_NAME}" "${1}" } BACKUP_DEST='/mnt/PRIVATE_DOCS/home/backup' PASSFILE='./nextcloud_pass.txt' # Create filename for database database_backupfile="nextcloud-sqlbkp_$( date +'%Y%m%d' ).bak" # Retrieve container names base_container="$( docker ps --format '{{.Names}}' | grep hugocloud-nextcloud_hugocloud-nextcloud )" database_container="$( docker ps --format '{{.Names}}' | grep hugocloud-postgresql_hugocloud-postgresql )" # Abort entire script if any command fails set -e # Turn on maintenance mode docker exec --user www-data "${base_container}" php occ maintenance:mode --on # Database backup printlog 'Backing up database' internal_database_backupfile="/tmp/${database_backupfile}" docker exec --env-file "${PASSFILE}" "${database_container}" pg_dump 'nextcloud' -cwv -h 'localhost' -U 'nextcloud' -f "${internal_database_backupfile}" docker cp "${database_container}":"${internal_database_backupfile}" "${BACKUP_DEST}" # Files backup for file in 'config' 'themes'; do >&2 printf 'Copying %s\n' "${file}" docker cp -a "${base_container}":"/var/www/html/${file}" "${BACKUP_DEST}" done # Turn off maintenance mode docker exec --user www-data "${base_container}" php occ maintenance:mode --off # Backup cleanup # Only keep 30 days of backups printlog 'Clean up old database backups' find "${BACKUP_DEST}" -name '*sqlbkp*' -type f -mtime +30 -print -delete printlog 'Done'