Hugo/backups/create_nextcloud_backup.sh
2023-06-28 18:08:24 +02:00

40 lines
1.4 KiB
Bash
Executable file

#!/bin/bash
# Backup script for Nextcloud in a kubernetes cluster
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
>&2 echo '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
>&2 echo 'Clean up old database backups'
find "${BACKUP_DEST}" -name '*sqlbkp*' -type f -mtime +30 -print -delete
>&2 echo 'Done'