Improve Nextcloud backup shell scripting

This commit is contained in:
Tibo De Peuter 2023-06-28 18:07:43 +02:00
parent 1a1d6685b8
commit c492bd08d6

View file

@ -1,24 +1,40 @@
#!/bin/bash
# Backup script for Nextcloud in a kubernetes cluster
BACKUP_DEST=/mnt/APPS/git/backups
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
nextcloud_container=$( docker ps --format "{{.Names}}" | grep hugocloud-nextcloud_hugocloud-nextcloud )
database_container=$( docker ps --format "{{.Names}}" | grep hugocloud-postgresql_hugocloud-postgresql )
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 "${nextcloud_container}" php occ maintenance:mode --on
docker exec --user www-data "${base_container}" php occ maintenance:mode --on
# Database backup
>&2 echo "Backing up database"
docker exec --env-file ./evars.txt "${database_container}" pg_dump "nextcloud" -cwv -h "localhost" -U "nextcloud" -f /tmp/nextcloud-sqlbkp_`date +"%Y%m%d"`.bak
docker cp "${database_container}":/tmp/nextcloud-sqlbkp_$(date +"%Y%m%d").bak /mnt/APPS/git/backups
>&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 "${nextcloud_container}" php occ maintenance:mode --off
docker exec --user www-data "${base_container}" php occ maintenance:mode --off
>&2 echo "Done"
# 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'